What are Text Blocks?
Text Blocks, standardized in Java 15, provide a more convenient and readable way to write multi-line string literals in Java.
Before Text Blocks:
Creating a multi-line string, like a JSON or SQL query, required a lot of concatenation, new-line characters (`\n`), and escape sequences, making it ugly and hard to read.
String json = "{\n" +
" \"name\": \"John\",\n" +
" \"age\": 30\n" +
"}";
With Text Blocks:
You can write the string in a much more natural way using three double-quote characters (`"""`) as the opening and closing delimiters.
String json = """
{
"name": "John",
"age": 30
}
""";
The compiler automatically handles the line endings and removes incidental leading white space, making the code for embedding formatted text significantly cleaner.
Explain the new `switch` expressions in Java.
Switch Expressions, standardized in Java 14, evolve the traditional `switch` statement into a more powerful and less error-prone expression that can return a value.
Key Improvements:
Example (Traditional `switch` statement):
int numLetters;
switch (day) {
case MONDAY:
case FRIDAY:
case SUNDAY:
numLetters = 6;
break;
case TUESDAY:
numLetters = 7;
break;
default:
throw new IllegalStateException("Invalid day: " + day);
}
Example (Modern `switch` expression):
int numLetters = switch (day) {
case MONDAY, FRIDAY, SUNDAY -> 6;
case TUESDAY -> 7;
default -> throw new IllegalStateException("Invalid day: " + day);
};
What is the `Externalizable` interface?
The Externalizable
interface provides a mechanism for custom serialization. It gives a class complete control over the format and contents of its serialized form.
It extends the `Serializable` interface and declares two methods:
void writeExternal(ObjectOutput out) throws IOException
: The class implements this method to explicitly write the state of its object to the stream.void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
: The class implements this method to explicitly read the data from the stream and restore the state of its object.Differences from `Serializable`:
It is used when the default serialization mechanism is not suitable, for example, if you need to handle complex data structures or ensure compatibility across different versions of a class.
What is a `PreparedStatement` and why is it preferred over a `Statement`?
Both are interfaces in the JDBC API used to execute SQL queries. However, `PreparedStatement` offers significant advantages.
A Statement
is used to execute simple, static SQL queries. The query string is concatenated with variable values, which is inefficient and insecure.
A PreparedStatement
is used to execute a pre-compiled SQL query with parameter placeholders (`?`).
Why `PreparedStatement` is preferred:
// Insecure Statement example
String query = "SELECT * FROM users WHERE username = '" + userInput + "'";
// Secure PreparedStatement example
String query = "SELECT * FROM users WHERE username = ?";
PreparedStatement pstmt = connection.prepareStatement(query);
pstmt.setString(1, userInput);
ResultSet rs = pstmt.executeQuery();
Explain the classloader delegation model in Java.
The classloader delegation model is a core principle of the Java classloading mechanism. It defines how a classloader finds and loads a class.
Java has a hierarchy of classloaders:
The Delegation Model works as follows:
This model ensures that:
What is the difference between PermGen and Metaspace?
Both are memory areas in the JVM used to store class metadata, but Metaspace replaced PermGen in Java 8.
PermGen (Permanent Generation):
Metaspace:
How would you profile a Java application to find a memory leak?
Profiling a memory leak involves using specialized tools to analyze the application's heap usage over time.
A memory leak in Java occurs when objects are no longer being used by the application, but the Garbage Collector is unable to remove them from memory because they are still being referenced. This causes the heap to grow continuously until an `OutOfMemoryError` occurs.
The general steps are: