All questions
Showing of 95Explain the difference between primitive and reference data types.
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
A primitive type variable holds the value itself, while a reference type variable holds a reference that points to an object. Java has eight primitives:
- Integer types:
byte,short,int,long - Floating-point types:
float,double booleanfortrueorfalse, andcharfor a single character
Each primitive has a fixed size and can never be null.
Classes, interfaces and arrays are reference types, and so are enums. Their objects are always allocated on the heap. Assigning one reference variable to another copies the reference, not the object, so both variables then point to the same object.
Where a primitive lives depends on where it is declared. A local int sits in the method's stack frame, but an int field sits inside its object on the heap.
int x = 10; // primitive - stores value 10
String str = "Hello"; // reference - stores memory address of String object
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What is autoboxing and unboxing?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
Autoboxing is the automatic conversion of a primitive to its wrapper class, such as int to Integer. Unboxing is the reverse, from Integer back to int. It lets you put primitives into collections, which only hold objects.
The compiler inserts the calls for you. Boxing an int becomes Integer.valueOf(x), and unboxing becomes x.intValue().
Two traps follow from this. Unboxing a null wrapper throws a NullPointerException. Comparing two Integer objects with == compares references, and it only seems to work for small values because Integer.valueOf caches -128 to 127.
// 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
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
Explain the difference between == and equals() method.
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
For objects, == checks reference equality, meaning both variables point to the same object. equals() checks logical equality, meaning two objects hold the same content, as long as the class overrides it.
For primitives, == compares the values directly. For objects, the default Object.equals() behaves exactly like ==. Classes such as String and Integer override it to compare their values, and your own classes should too when two instances can be logically equal.
Identical string literals are interned, so they share one object in the string pool and s3 == s4 is true. A string built with new String is always a separate object, so compare strings with equals(). A class that overrides equals() must also override hashCode().
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)
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What is the difference between String, StringBuilder, and StringBuffer?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
String is immutable, while StringBuilder and StringBuffer are mutable buffers you change in place. The two buffers differ in thread safety. StringBuffer synchronizes its methods, and StringBuilder does not.
String |
StringBuilder |
StringBuffer |
|
|---|---|---|---|
| Mutable | No | Yes | Yes |
| Thread-safe | Yes, because it never changes | No | Yes, through synchronized methods |
| Cost of building text | A new object for every change | Lowest | Higher, it pays for locking |
Every change to a String creates a new object, so += inside a loop copies the text again and again. A builder appends into one growing buffer instead.
StringBuilder is the default choice for building text in one thread. It has the same API as StringBuffer without the synchronization overhead. The Java documentation recommends it wherever possible, because it is faster under most implementations. StringBuffer only makes sense when several threads really append to the same buffer.
String str = "Hello";
str += " World"; // Creates new String object
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World"); // Modifies existing buffer
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What are access modifiers in Java?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
Access modifiers control which other code can use a class or its members. Java has four levels, from most to least restrictive:
private: visible only inside the same class.- No modifier, called package-private or default access: visible to any class in the same package.
protected: visible in the same package, and to subclasses in other packages.public: visible from anywhere.
A top-level class can only be public or package-private.
Start with the most restrictive level that works, such as private fields behind public methods, so a class can change its internals without breaking callers.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What is the difference between static and instance variables/methods?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
A static member belongs to the class itself and is shared by every instance. An instance member belongs to one object and needs that object to exist. There is one copy of a static variable, but every object gets its own copy of each instance variable.
Static variables are initialized once, when the class is first loaded, and you reach static members through the class name, as in Math.max(). A static method has no this, so it cannot use instance fields or call instance methods directly. An instance method can use both kinds.
Use static members for data and behaviour that do not depend on any one object, such as constants and utility methods. Shared static state is harder to test, because every test sees the same copy.
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++;
}
}
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What are the four pillars of OOP?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
The four pillars of object-oriented programming are encapsulation, inheritance, polymorphism and abstraction. Each one is a way to manage complexity in a large code base:
- Encapsulation bundles data with the methods that use it and hides the internal details, usually behind
privatefields. - Inheritance lets a new class reuse and extend an existing class, as when
Dog extends Animal. - Polymorphism lets objects of different types respond to the same method call in their own way.
- Abstraction exposes only the essential features of an object and hides how they work, through abstract classes and interfaces.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What is the purpose of the super keyword?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
The super keyword lets a subclass call its immediate parent's constructor and reach parent members that the subclass has overridden or hidden. It has three uses:
super(...)calls a parent constructor. When a constructor does not callthis(...)orsuper(...)explicitly, the compiler inserts a call tosuper().super.method()calls the parent's version of an overridden method, often to extend that behaviour rather than replace it.super.fieldreads a hidden field, which is a parent field hidden by a subclass field with the same name.
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);
}
}
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What is the difference between ArrayList and LinkedList?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
ArrayList stores elements in a resizable array, while LinkedList is a doubly linked list of nodes that point to their neighbours. That layout decides which operations are cheap.
ArrayList reads any index in O(1), because it jumps straight to the slot. Adding at the end is amortized O(1), meaning an occasional resize copies the array but the average cost per add stays constant. Inserting or removing in the middle is O(n), because the later elements have to shift.
LinkedList adds and removes at either end in O(1), and in the middle only when an iterator already points there. Reaching an index is O(n), because it walks node by node from the nearer end. So add(index, x) in the middle is O(n) as well.
ArrayList is the usual default, since its per-element overhead is lower. For queue work at the ends, ArrayDeque is often faster than LinkedList.
List<String> arrayList = new ArrayList<>(Collections.nCopies(101, "item"));
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)
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What is the difference between checked and unchecked exceptions?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
Checked exceptions must be caught or declared with throws, and the compiler enforces this. Unchecked exceptions need neither, so handling them is optional.
Checked exceptions extend Exception but not RuntimeException. They model failures a caller can expect and recover from, such as IOException, SQLException and ClassNotFoundException. Unchecked exceptions extend RuntimeException or Error. They usually signal bugs, such as NullPointerException, ArrayIndexOutOfBoundsException and IllegalArgumentException.
Use a checked exception when the caller can do something useful about the failure. Use an unchecked one for programming errors, where forcing every caller to catch it only adds noise.
// 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
}
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What is the exception hierarchy in Java?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
Every exception in Java descends from Throwable, which splits into two branches, Error and Exception.
Errorcovers serious problems such asOutOfMemoryErrorandStackOverflowError. A normal application should not try to catch these.Exceptioncovers conditions an application may want to handle. Its subclassRuntimeExceptionand everything below it are unchecked.
Every other subclass of Exception, such as IOException and SQLException, is checked. Error and its subclasses are unchecked too. The "Checked Exceptions" node in the tree is a grouping, not a real class. A catch (Exception e) block never catches an Error.
Throwable
├── Error (unchecked)
│ ├── OutOfMemoryError
│ └── StackOverflowError
└── Exception
├── Checked Exceptions
│ ├── IOException
│ └── SQLException
└── RuntimeException (unchecked)
├── NullPointerException
└── ArrayIndexOutOfBoundsException
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What is the difference between throw and throws?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
throw raises an exception at a specific point in the code, while throws declares in the method signature which exceptions can reach the caller.
throw takes a single exception object, as in throw new IllegalArgumentException("Age cannot be negative"). Execution stops there, and the exception travels up the call stack until a catch block handles it. throws can list several exception types, separated by commas.
A method that can throw a checked exception must either catch it or declare it in throws. For an unchecked exception such as IllegalArgumentException, the throws clause in the example is optional and only documents the behaviour.
public void validateAge(int age) throws IllegalArgumentException {
if (age < 0) {
throw new IllegalArgumentException("Age cannot be negative");
}
}
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
Can we have multiple catch blocks for a single try block?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
Yes. A single try can have several catch blocks, and Java runs the first one whose type matches the thrown exception. At most one of them runs.
try {
// risky code
} catch (FileNotFoundException e) {
// handle file not found
} catch (IOException e) {
// handle other IO exceptions
} catch (Exception e) {
// handle any other exception
}
Because the first match wins, order them from most specific to most general. The compiler enforces this. A catch for a subclass placed after a catch for its superclass could never run, so it is a compile-time error. In the example, FileNotFoundException must come before IOException, and Exception goes last.
One block can also catch several unrelated types joined with |, as in catch (IOException | SQLException e). Use this multi-catch form when the handling is the same for each type. Its parameter is implicitly final.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What is the difference between process and thread?
How can you create a thread in Java?
What is the difference between start() and run() methods?
What is the difference between stack and heap memory?
What is the difference between InputStream/OutputStream and Reader/Writer?
What is Spring Boot and its advantages?
What is the difference between @Component, @Service, @Repository, and @Controller?
What is the difference between @RestController and @Controller?
What is JDBC and its architecture?
What is the difference between unit testing and integration testing?
What are the commonly used testing frameworks in Java?
What is the difference between JDK, JRE and JVM?
What is method overloading and method overriding?
What is the final keyword and its uses?
What is polymorphism and its types?
What is the difference between final, finally, and finalize?
Explain the concept of pass-by-value in Java.
What is the difference between abstract class and interface?
Explain the concept of composition vs inheritance.
What is the difference between method hiding and method overriding?
What is the Java Collections Framework hierarchy?
What is the difference between HashMap and Hashtable?
What is the difference between HashSet and TreeSet?
What is the difference between fail-fast and fail-safe iterators?
What is the difference between Comparable and Comparator?
What features did Java 8 and later versions add to the Collections Framework?
What is try-with-resources and why is it useful?
What is synchronization and why is it needed?
What are the different ways to achieve synchronization?
What is the difference between wait() and sleep()?
What is the volatile keyword?
What is the Executor framework?
Explain the JVM memory structure.
What is garbage collection and how does it work?
What causes OutOfMemoryError and how to handle it?
What is the difference between shallow copy and deep copy?
What is serialization and deserialization?
What is the purpose of serialVersionUID?
What is the Singleton pattern and how do you implement it?
Explain the Factory pattern.
What is the Observer pattern?
What is the Strategy pattern?
What is dependency injection and how does Spring implement it?
What are the different types of Spring bean scopes?
What is Spring Security and its core features?
What is Spring Data JPA and its benefits?
How do you handle database connections in JDBC?
What is the difference between Statement and PreparedStatement?
What is connection pooling and why is it important?
What are database transactions and ACID properties?
What tools can you use for Java application profiling?
What is Test-Driven Development (TDD)?
What is mocking and why is it useful?
What are Java 8 features and their benefits?
What is functional programming in Java?
What is the Stream API and its benefits?
What are annotations and how do you create custom annotations?
What are the key features added in Java since Java 8?
What is the contract between equals() and hashCode() in Java?
Why is String immutable in Java?
What are records in Java?
What are virtual threads in Java, and when should you use them?
What are generics in Java, and what is type erasure?
What is the difference between Association, Aggregation, and Composition?
What is deadlock and how can you prevent it?
What is the Module System introduced in Java 9?
Explain the internal working of HashMap.
What happens if an exception is thrown in a finally block?
What is the difference between CountDownLatch and CyclicBarrier?
What is the Fork/Join framework?
What are the different types of garbage collectors?
What is the difference between NIO and traditional I/O?
What is Spring AOP and when would you use it?
What are the best practices for Java performance optimization?
How do you identify and fix memory leaks?
How does JVM optimize code at runtime?
What are the different types of references in Java?
What is reflection and when should you use it?
What is Spring Boot Auto-Configuration and how does it work?
What are microservices and what are the challenges in implementing them?
How does ConcurrentHashMap achieve thread safety?
How does class loading work in Java?
This answer is part of Pro.
The full written answer, with the trade-offs and follow-ups an interviewer will probe.
No matches
Try a different filter or search term.
Java in pictures.
A quick recap of Java in 33 short animated pictures, one idea at a time. About 8 min to get the whole topic back in your head before you walk in. Pause any picture, slow it down, or step through it.
Pro The first chapter is free to play. The other 30 pictures come with Pro.
This picture is part of Pro. The first chapter is free to play; the other 30 pictures come with Pro.
Get Pro · $10/mo Already Pro? Sign in82 of 95 Java answers are in Pro.
Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.
- Full answers + code
- AI explanations, simpler or deeper
- 1,000 AI credits / month
- Cancel anytime
Change topic
Pick a different technology or stack. Your current topic stays put until you choose a new one.
MEAN
MongoDB, Express, Angular, Node.jsMERN
MongoDB, Express, React, Node.jsDjango
Python Full-Stack DevelopmentRuby on Rails
Convention over ConfigurationServerless on AWS
Serverless Architecture on AWSInterviewers also test these - they're common to every stack, whichever one you picked above.
Flutter Mobile
Flutter Cross-Platform Mobile DevelopmentInterviewers also test these - they're common to every stack, whichever one you picked above.
Spring Boot
Enterprise Java Development.NET
Microsoft EcosystemVue
Vue.js, Vite, TypeScript, Tailwind, Node.jsGo Backend
Golang, gRPC, PostgreSQL, Redis, RabbitMQInterviewers also test these - they're common to every stack, whichever one you picked above.
FastAPI
Python, FastAPI, SQLAlchemy, PostgreSQLReact Native
React, TypeScript, Redux, FirebaseiOS Native
Swift, SwiftUI, UIKit, FirebaseAndroid Native
Java, Jetpack Compose, FirebaseDevOps / Platform
Docker, Kubernetes, Terraform, CI/CDInterviewers also test these - they're common to every stack, whichever one you picked above.
AI Engineer
LLMs, RAG, Agents, EvalsAI-Powered Developer
Claude Code, Copilot, Agentic WorkflowsCore SWE Interview Prep
Data structures, algorithms, OS, concurrency, networking, gitInterviewers also test these - they're common to every stack, whichever one you picked above.
Interviewers also test these - they're common to every stack, whichever one you picked above.