Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Java / Classes, Objects and Memory

    Interview Questions

    Real interview questions on Classes, Objects and Memory, answered the way you would say them out loud.

    Extra: Interview prep5 min read

    Interview questions

    What's the difference between == and equals() in Java?

    How I'd answer: "== compares values for primitives, but for objects it compares references — whether both variables point to the same object. equals is a method meant to compare logical equality. The default implementation in Object is just ==, but classes like String, Integer and the collections override it to compare contents. So for strings and wrappers I always use equals: == can appear to work because of the String pool and the Integer cache for −128 to 127, and then fail with runtime values. For my own classes, equals only compares contents if I override it — together with hashCode."

    Combines: Ch 6, 8, 9.

    Why are Strings immutable in Java? What is the String pool?

    How I'd answer: "The String pool is an area of the heap where the JVM keeps one shared instance of each string literal, so identical literals refer to the same object and memory is saved. That sharing is only safe because strings can't change. Other reasons for immutability: security — strings hold file paths, URLs, class names, credentials, so a value can't be altered after it's validated; performance — String caches its hash code, which makes it a fast, reliable HashMap key; and thread safety — immutable objects can be shared across threads without synchronisation. The cost is that every modification creates a new object, which is why we use StringBuilder for repeated edits."

    Combines: Ch 5, 6, 7.

    String vs StringBuilder vs StringBuffer?

    How I'd answer: "String is immutable — every change creates a new object. StringBuilder is mutable with an internal resizable buffer, so appends are amortised constant time; building a string of n characters with += in a loop is O(n²) because each step copies everything, while StringBuilder is O(n). I measured 50,000 appends at about 133 ms with += versus under a millisecond with StringBuilder. StringBuffer is the older, synchronised version of StringBuilder — thread-safe but slower — and rarely needed, because a builder is usually local to one method."

    Combines: Ch 6, 7.

    What's autoboxing? Can you show a bug it causes?

    How I'd answer: "Autoboxing is the compiler automatically converting a primitive to its wrapper — int to Integer — when an object is needed, like when adding to a List<Integer>, and unboxing is the reverse. Two classic bugs: first, Integer a = 128, b = 128; a == b is false, because == compares references and only −128 to 127 are cached by Integer.valueOf; compare with equals or unbox. Second, unboxing a null throws a NullPointerException — for example int count = map.get(key) when the key is missing; use getOrDefault. There's also a performance cost: a million boxed Integers took about 21 MB versus 3 MB for an int[]."

    Combines: Ch 8.

    What's the difference between a static and an instance method? Can a static method call an instance method?

    How I'd answer: "An instance method runs on a specific object and can use its fields through this. A static method belongs to the class — there's no this — so it can only use static fields and its parameters. It can't call an instance method directly, because there's no object to call it on; it has to create or be given an object and call the method on that, which is exactly what main does. I make methods static when they don't depend on object state, like utilities or factory methods, and keep anything describing one object's behaviour as instance methods. Static mutable fields need care because they're shared by all threads."

    Combines: Ch 3, 4.