Why Using == on Java Objects Can Crash Your Service: Hidden Pitfalls and Fixes
This article explains how mistakenly using the == operator for Java object comparison, especially with enums and boxed types, can cause runtime failures after framework changes, and also shows how unchecked log growth can fill disks, degrade performance, and how to resolve both issues.
== and equals
Many developers think using the == operator for object comparison in Java is harmless, but it often leads to subtle bugs, especially when enums or boxed types are involved.
During a recent RPC framework migration, an interface that had worked for two years started throwing errors because the new framework created new Byte objects instead of reusing cached instances, causing == comparisons to fail.
The original code compared enum values by id using ==, which worked when the framework used valueOf and autoboxing, but broke after switching to new Byte().
Test results confirmed the failure:
Running the Alibaba Java Coding Guidelines plugin uncovered another issue: the enum’s code field was declared as a primitive byte, so == comparisons succeeded because primitives are compared by value.
Another pitfall occurs when the field is a Byte wrapper but the method getEnumByCode(byte code) expects a primitive; the comparison still passes because of autounboxing.
These examples illustrate that relying on == for wrapper types is fragile; always use equals to avoid unexpected behavior.
Log filling
After a recent technical upgrade, the service’s success rate dropped to zero and GC time spiked. Investigation revealed that the disk was full because log files grew uncontrollably.
Using ls -lht showed the large log file, and deleting it with rm -rf did not free space because the file was still held open by a running process.
In Linux, a file consists of a pointer part (metadata) and a data part (actual bytes). If a process keeps the file open, the pointer is not removed from metadata, so disk space remains occupied.
Two solutions are provided:
Run lsof -n | grep delete to identify the process (in this case the Java application) that is still writing to the log, then restart the process to release the file.
Truncate the log file directly with echo "" > /service.log, which immediately frees space while allowing the process to continue logging.
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.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
