Top 10 Surprising Java Pitfalls Every Developer Should Avoid
This article ranks the ten most counter‑intuitive Java language features that often trap developers, explains why they behave unexpectedly, and provides clear code examples and fixes to help programmers write safer, more predictable code.
As an object‑oriented language, Java is praised for its simplicity and power, but it also contains many surprising behaviors that can easily trip up developers.
10. switch statements require break to stop
Without a break, execution falls through all subsequent cases, producing unintended output.
int count = 1;
switch(count){
case 1:
System.out.println("one");
case 2:
System.out.println("two");
case 3:
System.out.println("three");
}The code prints one, two, and three instead of just one. Adding break after each case resolves the issue.
9. Short‑circuit behavior of logical operators
Logical operators stop evaluating once the result is known; this can lead to unexpected side effects when the right‑hand expression is never executed.
int num = 1;
System.out.println(false && ((num++)==1));
System.out.println(num);The output is false and 1 because the right side is skipped. Changing false to true makes num become 2.
8. Array indices start at zero
Java arrays are zero‑based, which contradicts everyday counting and can cause off‑by‑one errors.
int[] arr = new int[]{1,3,5,7,9};
for(int i=0;i<arr.length;i++){
System.out.println("the element is:"+arr[i]);
}Similarly, String str="hello world"; System.out.println(str.charAt(1)); prints e, not h, because indexing starts at 0.
7. Removing elements from an ArrayList while iterating
Using a fixed size variable causes IndexOutOfBoundsException. Iterate with list.size() or use an iterator.
public static void main(String[] args){
List<String> list = new ArrayList<>();
list.add("abc");
list.add("bc");
list.add("bc");
list.add("abcd");
list.add("abcdef");
for(int i=0;i<list.size();i++){
if(list.get(i).equals("bc")){
list.remove(i);
}
}
}6. Converting a char to an int yields its ASCII code
Casting '8' to int prints 56, not 8, because the character’s Unicode value is used.
char symbol = '8';
System.out.println((int) symbol);5. Missing braces in a while loop
Without braces, only the first statement belongs to the loop, leading to unexpected output.
int i = 0;
while(i++<3)
System.out.print("A");
System.out.print("B");The program prints AAAB instead of three A s and three B s.
4. Integer cache for values -128 to 127
Integer objects within this range are cached, so Integer a = 100; Integer b = 100; a == b is true, while values outside the range are not cached.
Integer a = 100;
Integer b = 100;
Integer c = 200;
Integer d = 200;
System.out.println(a==b);
System.out.println(c==d);3. Empty loop body causing infinite loops
A stray semicolon after the loop condition creates an empty body, turning the following block into code that runs after the loop, often resulting in an infinite loop.
int i = 1;
while(i<4);
{
System.out.println(i++);
}2. Mistaking "=+" for "+="
Writing a =+ b assigns the positive value of b to a instead of adding; the result can be surprising.
int i = 100;
i =+ 2;
System.out.println(i);This prints 2, not 102.
1. Java comments can contain Unicode escapes
Unicode escape sequences are processed before comment removal, so a line that appears commented can still execute.
public static void main(String[] args){
// \u000d System.out.println("Hello World!");
}The program prints Hello World! because \u000d is interpreted as a carriage return, turning the comment into executable code.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
