25 Java Mistakes Freshers Make in Interviews (and How to Avoid Them)
Javabeginnerinterview-prepjava
Codevity Admin · 10/25/2026
Step 32 of 38 · Java Interview Preparation for Beginners · Stage 7: Interview practice
Codevity Admin · 10/25/2026
Interviewers do not expect perfection from freshers. They look for awareness: do you know the traps that bite everyone, and do you avoid them out of habit? These 25 mistakes come up in interviews, code reviews and bug reports again and again. Each row names the mistake, why it hurts, and the fix.
| # | Mistake | Why it goes wrong | Do this instead |
|---|---|---|---|
| 1 | Comparing strings with == | It compares references; it works for literals only because of the string pool | a.equals(b), or "literal".equals(input) to avoid a NullPointerException |
| 2 | Comparing Integer objects with == | Values from -128 to 127 are cached, so a test passes and then fails at 128 | equals(), or compare after unboxing |
| 3 | Saying Java is "pass by reference" | Java always passes a copy of the value (for objects, a copy of the reference) | Explain with a swap that fails and a setName that works |
| 4 | Forgetting integer division and overflow | 7 / 2 is 3; Integer.MAX_VALUE + 1 wraps to a negative number | Use 7 / 2.0, long, or Math.addExact |
| 5 | Using double for money | 0.1 + 0.2 is not 0.3 | BigDecimal built from a String, or store cents in a long |
| 6 | Concatenating strings in a loop | Each += creates a new object (quadratic time) | StringBuilder |
| 7 | Ignoring char arithmetic | 'a' + 1 is the int 98, not "a1" | Cast with (char), or concatenate to a String first |
| 8 | Calling s.toUpperCase() and ignoring the result | Strings are immutable; the method returns a new string | s = s.toUpperCase(); |
| # | Mistake | Why it goes wrong | Do this instead |
|---|---|---|---|
| 9 | Overriding equals without hashCode | Equal objects land in different buckets, so HashMap and HashSet fail to find them. See the contract | Override both from the same fields (or use a record) |
| 10 | Using mutable objects as map keys | Changing a field changes the hash, so the entry is lost | Use immutable keys: , , records |
| 11 | Removing from a list inside a for-each loop | , an , or build a new list | |
| 12 | on a | It removes the index 1, not the value 1 | |
| 13 | Using raw types ( with no type argument) | Type safety is lost, and appears at run time. See generics | , and if the type is unknown |
| 14 | Returning an internal mutable collection from a getter | Callers can change your object's state | Return or |
| 15 | Using inheritance just to reuse code | It creates tight coupling and breaks encapsulation | Prefer composition: hold a field and delegate. See OOP |
| 16 | Confusing overloading with overriding | Overloads are chosen at compile time by the reference type; overrides at run time by the object | Use so the compiler checks for you |
| 17 | Believing is faster than | Cache locality makes win in nearly every real case | Default to ; use for a queue or stack |
| # | Mistake | Why it goes wrong | Do this instead |
|---|---|---|---|
| 18 | Catching or everywhere | Hides bugs and catches things you cannot handle (like ) | Catch the most specific type you can actually handle |
| 19 | An empty block, or only | Failures vanish silently or only reach a console nobody reads | Log it with context, rethrow, or wrap it, and never swallow it |
| 20 | Not closing resources | Leaked connections and file handles | . See exceptions |
| 21 | Putting or inside | It overrides the real result and can hide an exception | Keep for cleanup only |
| # | Mistake | Why it goes wrong | Do this instead |
|---|---|---|---|
| 22 | Sharing mutable state between threads without protection | is not atomic: updates are lost. See threads | , , or immutable data |
| 23 | Calling instead of | No new thread is created | |
| 24 | Returning for "nothing", and without checking | far from the cause | Return an empty collection or an , and use / |
| 25 | Using for logging | No levels, no timestamps, no control in production | A logger (SLF4J with Logback), with levels and parameters: |
This program demonstrates several of the mistakes above. Predict each result before you read the comment.
Pick any five rows above, write a small program that triggers each mistake, then fix it. Then solve 35 coding problems.
StringIntegerConcurrentModificationExceptionremoveIfIterator.remove()list.remove(1)List<Integer>list.remove(Integer.valueOf(1))ListClassCastExceptionList<String>List<?>List.copyOf(items)Collections.unmodifiableList(items)@OverrideLinkedListArrayListArrayListArrayListArrayDequeExceptionThrowableOutOfMemoryErrorcatche.printStackTrace()try-with-resourcesreturnthrowfinallyfinallycount++AtomicIntegersynchronizedthread.run()thread.start()start()nullOptional.get()NullPointerExceptionOptionalorElseorElseThrowSystem.out.printlnlog.info("saved {}", id)==hashCodeequals==import java.util.*;
public class Traps {
public static void main(String[] args) {
String a = new String("java");
System.out.println(a == "java"); // false
System.out.println(a.equals("java")); // true
Integer x = 127, y = 127, p = 128, q = 128;
System.out.println(x == y); // true
System.out.println(p == q); // false
System.out.println(p.equals(q)); // true
List<Integer> numbers = new ArrayList<>(List.of(10, 20, 30));
numbers.remove(1); // removes the INDEX 1
System.out.println(numbers); // [10, 30]
numbers.remove(Integer.valueOf(10)); // removes the VALUE 10
System.out.println(numbers); // [30]
System.out.println(7 / 2); // 3
System.out.println(7 / 2.0); // 3.5
System.out.println('a' + 1); // 98
System.out.println("" + 'a' + 1); // a1
System.out.println(0.1 + 0.2 == 0.3); // false
System.out.println(Math.abs(0.1 + 0.2 - 0.3) < 1e-9); // true
String s = "hello";
s.toUpperCase(); // the result is thrown away
System.out.println(s); // hello
}
}