← Back to Core Java Course | Chapter 13: Advanced Topics & Reference | Lesson 9 of 10

Java Common Mistakes

This lesson lists slips that many new Java learners make, like a map of common potholes. Knowing them ahead of time helps you avoid them.

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 ==

java
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
	}
}

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

java
// 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");
		}
	}
}

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

java
// 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");
		}
	}
}

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

java
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");
		}
	}
}

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

java
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
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Comparing strings with == instead of equals().
  2. Calling a method on a null reference, which throws NullPointerException.
  3. Using 5 / 2 and expecting 2.5, when integer division gives 2.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.