Java Common Mistakes
In this page:
Comparing Strings with ==
== compares object references for String, checking whether two variables point to the exact same object in memory, not whether their contents are equal — use .equals() to compare string content correctly.
Example: Comparing Strings with ==
public class Main {
public static void main(String[] args) {
String a = new String("hi");
String b = new String("hi");
System.out.println(a == b); // false: different objects
System.out.println(a.equals(b)); // true: same content
}
}
Login to try C/C++/Java/PHP code in the editor
NullPointerException
Calling a method on a variable that's currently null throws NullPointerException, Java's most common runtime error — it usually means a value you assumed was initialized never actually was.
Example: NullPointerException
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare value and set it to null
String value = null;
// Try code that might throw an exception
try {
value.length();
// Handle NullPointerException if it was thrown
} catch (NullPointerException e) {
// Print a line to the console
System.out.println("Caught NullPointerException");
}
}
}
Login to try C/C++/Java/PHP code in the editor
Modifying List in For-Each Loop
Calling list.remove() directly while iterating with a for-each loop throws ConcurrentModificationException — use an explicit Iterator and its own remove() method instead when you need to delete elements mid-loop.
Example: Modifying List in For-Each Loop
// Import java.util.ArrayList so it can be used by its short name
import java.util.ArrayList;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new ArrayList object and store it in list
ArrayList<Integer> list = new ArrayList<>(java.util.List.of(1, 2, 3));
// Try code that might throw an exception
try {
for (Integer n : list) {
list.remove(n);
}
// Handle java.util.ConcurrentModificationException if it was thrown
} catch (java.util.ConcurrentModificationException e) {
// Print a line to the console
System.out.println("Caught: use Iterator.remove() instead");
}
}
}
Login to try C/C++/Java/PHP code in the editor
Array Index Out of Bounds
Accessing an array or list index that's outside its valid range (including using the size itself, which is always one past the last valid index) throws ArrayIndexOutOfBoundsException — a classic off-by-one bug.
Example: Array Index Out of Bounds
public class Main {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
try {
System.out.println(arr[3]); // size itself is one past the last valid index
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Caught: index out of bounds");
}
}
}
Login to try C/C++/Java/PHP code in the editor
Integer Division Truncation
Dividing two int values in Java performs integer division and truncates any decimal remainder, so 5 / 2 yields 2, not 2.5 — cast at least one operand to double first if you need a fractional result.
Example: Integer Division Truncation
public class Main {
public static void main(String[] args) {
System.out.println(5 / 2); // 2: truncated
System.out.println(5 / (double) 2); // 2.5: cast avoids truncation
}
}
Login to try C/C++/Java/PHP code in the editor
- Comparing strings with
==instead ofequals(). - Calling a method on a
nullreference, which throwsNullPointerException. - Using
5 / 2and expecting2.5, when integer division gives2.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: