Get ready for your next interview with our comprehensive question library
Primitive types store actual values directly in memory and include: byte, short, int, long, float, double, boolean, char. They are stored on the stack and have fixed memory allocation.
Reference types store references (memory addresses) to objects located in heap memory. Examples include classes, interfaces, arrays, and enums. When you assign a reference variable, you're copying the reference, not the actual object.
int x = 10; // primitive - stores value 10
String str = "Hello"; // reference - stores memory address of String object
Autoboxing is automatic conversion of primitive types to their corresponding wrapper classes. Unboxing is the reverse process.
// Autoboxing
Integer num = 100; // int to Integer
List<Integer> list = new ArrayList<>();
list.add(50); // int automatically boxed to Integer
// Unboxing
Integer wrapper = 200;
int primitive = wrapper; // Integer to int
String s1 = new String("hello");
String s2 = new String("hello");
String s3 = "hello";
String s4 = "hello";
System.out.println(s1 == s2); // false (different objects)
System.out.println(s1.equals(s2)); // true (same content)
System.out.println(s3 == s4); // true (string pool)
String str = "Hello";
str += " World"; // Creates new String object
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World"); // Modifies existing buffer
Static members belong to the class itself, not to any instance. They're loaded when the class is first loaded and shared among all instances.
Instance members belong to specific object instances and require object creation to access.
class Example {
static int staticVar = 0; // Class variable
int instanceVar = 0; // Instance variable
static void staticMethod() { // Can't access instance members
// staticVar++; // OK
// instanceVar++; // Compilation error
}
void instanceMethod() { // Can access both
staticVar++;
instanceVar++;
}
}
The super
keyword refers to the immediate parent class object and is used to:
class Parent {
String name = "Parent";
void display() { System.out.println("Parent display"); }
}
class Child extends Parent {
String name = "Child";
Child() {
super(); // Call parent constructor
}
void display() {
super.display(); // Call parent method
System.out.println("Parent name: " + super.name);
}
}
ArrayList:
List<String> arrayList = new ArrayList<>();
List<String> linkedList = new LinkedList<>();
// ArrayList better for
String element = arrayList.get(100); // O(1)
// LinkedList better for
linkedList.add(0, "first"); // O(1)
linkedList.remove(0); // O(1)
Checked Exceptions: Must be caught or declared in method signature. Checked at compile-time.
Examples: IOException, SQLException, ClassNotFoundException
Unchecked Exceptions: Runtime exceptions that don't need to be caught or declared.
Examples: NullPointerException, ArrayIndexOutOfBoundsException, IllegalArgumentException
// Checked exception - must handle
public void readFile() throws IOException {
FileReader file = new FileReader("file.txt");
}
// Unchecked exception - optional handling
public void divide(int a, int b) {
int result = a / b; // May throw ArithmeticException
}
Throwable
├── Error (unchecked)
│ ├── OutOfMemoryError
│ └── StackOverflowError
└── Exception
├── Checked Exceptions
│ ├── IOException
│ └── SQLException
└── RuntimeException (unchecked)
├── NullPointerException
└── ArrayIndexOutOfBoundsException
throw: Used to explicitly throw an exception from method or code block.
throws: Used in method signature to declare that method might throw certain exceptions.
public void validateAge(int age) throws IllegalArgumentException {
if (age < 0) {
throw new IllegalArgumentException("Age cannot be negative");
}
}
Yes, multiple catch blocks are allowed. They are evaluated in order, so more specific exceptions should come before general ones.
try {
// risky code
} catch (FileNotFoundException e) {
// handle file not found
} catch (IOException e) {
// handle other IO exceptions
} catch (Exception e) {
// handle any other exception
}
Upgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumAccess all premium content - interview questions, and other learning resources
We regularly update our features and content, to ensure you get the most relevant and updated premium content.
1000 monthly credits
Cancel anytime