Basic Interview Questions
What are the main features of Java?
- Platform independent: code compiles to bytecode that runs on any JVM ("write once, run anywhere").
- Object-oriented: everything lives in classes; supports encapsulation, inheritance, polymorphism, abstraction.
- Robust and secure: strong typing, no pointer arithmetic, automatic garbage collection, exception handling.
- Multithreaded: built-in threads and a rich concurrency library.
- High performance: the JIT compiler turns hot bytecode into native machine code.
What is the difference between JDK, JRE and JVM?
| Term | What it is | Contains |
|---|---|---|
| JVM | Java Virtual Machine: runs bytecode | class loader, bytecode verifier, interpreter, JIT, GC |
| JRE | Java Runtime Environment: what you need to run Java | JVM + core class libraries |
| JDK | Java Development Kit: what you need to develop Java | JRE + tools: javac, jar, javadoc, jdb |
Note: Since Java 11 there is no separate JRE download; you install a JDK (or build a trimmed runtime with jlink).
Explain public static void main(String[] args).
public: the JVM must be able to call it from outside the class.static: called without creating an object of the class.void: returns nothing to the JVM (useSystem.exit(code)for an exit code).String[] args: command-line arguments.
What are the primitive data types in Java?
| Type | Size | Default |
|---|---|---|
| byte | 1 byte | 0 |
| short | 2 bytes | 0 |
| int | 4 bytes | 0 |
| long | 8 bytes | 0L |
| float | 4 bytes | 0.0f |
| double | 8 bytes | 0.0 |
| char | 2 bytes (UTF-16) | '\u0000' |
| boolean | JVM-dependent | false |
Everything else (String, arrays, objects) is a reference type. Defaults apply only to fields, not local variables.
Is Java pass-by-value or pass-by-reference?
Java is always pass-by-value. For objects, the value passed is a copy of the reference. So a method can change the object's fields, but reassigning the parameter does not affect the caller's variable.
Example
Reassigning the parameter has no effect outside the method.
public class Main {
static void change(StringBuilder sb) {
sb.append(" World"); // modifies the same object
sb = new StringBuilder("New"); // only changes the local copy of the reference
}
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello");
change(sb);
System.out.println(sb);
}
}Output
Hello World
What is the difference between == and equals()?
== compares primitive values, or for objects it checks whether two references point to the same object. equals() compares content when the class overrides it (String, Integer, records, collections do).
Example
String literals are pooled; new always creates a new object.
public class Main {
public static void main(String[] args) {
String a = "java";
String b = "java";
String c = new String("java");
System.out.println(a == b); // same pooled object
System.out.println(a == c); // different objects
System.out.println(a.equals(c)); // same content
System.out.println(a == c.intern());
}
}Output
true false true true
What is the difference between static and instance members?
A static field or method belongs to the class (one copy shared by all objects, loaded with the class). An instance member belongs to each object. Static methods cannot use this or access instance members directly. Static blocks run once when the class is loaded.
What is the use of the final keyword?
| Applied to | Meaning |
|---|---|
| variable | value cannot be reassigned (a constant if also static) |
| method | cannot be overridden |
| class | cannot be extended (e.g. String, Integer) |
Tip: A final reference cannot point to another object, but the object itself can still change (e.g. final List can still add()).
What are wrapper classes and autoboxing?
Wrapper classes (Integer, Double, Boolean...) wrap primitives as objects so they can be used in collections and generics. Autoboxing converts int → Integer automatically; unboxing does the reverse. Unboxing null throws NullPointerException.
Example
Integer caches values from -128 to 127, so == on boxed values is a trap.
public class Main {
public static void main(String[] args) {
Integer a = 127, b = 127;
Integer c = 128, d = 128;
System.out.println(a == b); // cached object
System.out.println(c == d); // two different objects
System.out.println(c.equals(d)); // always compare with equals
}
}Output
true false true