Browse by Topic

Popular

Latest

Latest Java Posts

Diagram showing a Java object being cloned into a shallow copy and a deep copy, with references to mutable objects highlighted.
Java

Java Clone Method: How to Use It Correctly

Learn how the java clone method works, why shallow copies often fail, and how to implement deep copying correctly or choose safer alternatives like copy constructors.

JavaCloneableObject CloningDeep Copy
A Java code editor showing a toString method override with a magnifying glass over an object to represent debugging and object inspection.
Java

Java toString Override: Implementation and Pitfalls

Override toString in Java to produce clear, readable object descriptions. Learn null-safe formatting, performance tradeoffs, records, Lombok, and common mistakes to avoid.

toString overrideJava object representationdebugging outputString.format
Diagram showing two object references compared with == and equals() in Java, illustrating reference vs value equality.
Java

Java equals vs ==: Reference vs Value Equality

Understand the difference between == and equals() in Java, how to override equals() correctly, and when to use each comparison for reliable code.

Java equalityequals methodreference comparisonobject comparison
Illustration of two equal Java objects converging into the same hash bucket in a HashMap, representing the equals and hashCode contract.
Java

Java equals hashCode Contract: Rules and Implementation

Java classes that override equals() must also override hashCode() to satisfy the equals-hashCode contract. This article explains why HashMap and HashSet depend on that contract, how to implement both methods, and common pitfalls to avoid.

equals()hashCode()HashMapJava records
Illustration of a Java object with a hash code being placed into a hash bucket
Java

Understanding the Java hashCode Method

How the Java hashCode method works, why it must align with equals, and how to implement it correctly for hash-based collections.

hashCodeequalsHashMapJava Collections
Illustration of Java equals method comparing two objects with a balance scale, representing value equality.
Java

Java equals Method: Contract, Implementation, and Pitfalls

Learn how to correctly override the Java equals() method, understand the equals and hashCode contract, and avoid common pitfalls that break collections.

JavaequalshashCodeObject Contract
Diagram contrasting the unpredictable finalize method with the structured Cleaner and try-with-resources approaches in Java.
Java

Java Object finalize: Why It's Deprecated and What to Use

Learn why java object finalize is deprecated, how the JVM invokes it, and why Cleaner and try-with-resources are safer for resource cleanup.

JavafinalizeCleanerresource management
Illustration of Java object cloning showing a source object splitting into two independent copies with a shallow and deep branch.
Java

Java Object Cloning: Shallow vs Deep Copy

Understand Java object cloning: shallow vs deep copy, implementing Cloneable correctly, common pitfalls, and practical alternatives.

JavaObject CloningShallow CopyDeep Copy
A magnifying glass over a Java object showing its runtime class label, illustrating getClass() type inspection.
Java

Java Object getClass: Runtime Type Inspection

Learn how to use Java's Object.getClass() to inspect runtime types, compare classes, and work with reflection while avoiding common pitfalls.

Java ReflectionRuntime TypeObject ClassType Checking
Diagram showing a Java object being converted into a readable string representation with field names and values.
Java

Java Object.toString(): Overriding Default Behavior

Learn how Java's Object.toString() works by default, how to override it for readable output, and how records, null handling, and performance affect the result.

JavatoStringObject classJava records