Java Notes
Learn the java.lang.Object class — the parent of every Java class, its key methods toString(), equals(), hashCode(), and getClass(), and how to override them correctly.
Every class in Java automatically extends java.lang.Object. You never write extends Object — Java adds it silently. This means every object you ever create already has a set of built-in methods.
Understanding these methods — and knowing which ones to override — is an important part of writing good Java code.
Methods in Object Class
| Method | What it does |
|---|---|
toString() | Returns a String description of the object |
equals(Object o) | Checks if two objects are equal |
hashCode() | Returns an integer hash of the object |
getClass() | Returns the Class object representing the runtime type |
clone() | Creates a copy of the object |
toString() — String Representation
By default, toString() returns something like ClassName@7ef88735 — the class name and a hex hash. That is not useful for debugging. Override it to print meaningful information.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
System.out.println(s)— Java automatically callss.toString(). Without the override you get a memory address. With it, you get readable output."Student: " + s— string concatenation also callstoString().- Override
toString()in every non-trivial class. It saves hours of debugging.
equals() — Content Comparison
By default, equals() uses == — it checks if two references point to the same object. For value comparison, you must override it.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
if (this == obj) return true;— fast path: same object is always equal.if (!(obj instanceof Point)) return false;— wrong type means not equal.(Point) obj— cast after checking type.- Compare the meaningful fields —
xandy.
hashCode() — Must Match equals()
When you override equals(), you must also override hashCode(). The rule: objects that are equal must have the same hash code.
If you override equals() but not hashCode(), HashMap and HashSet will behave incorrectly.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Without hashCode() override, map.get(p2) would return null even though p1.equals(p2) is true.
getClass() — Runtime Type
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
getClass() returns the actual runtime class of an object, not the declared reference type. Contrast with instanceof which also checks supertypes.
equals() vs == vs instanceof
Object a = "Hello";
Object b = "Hello";
Object c = new String("Hello");
System.out.println(a == b); // true — same String literal (pool)
System.out.println(a == c); // false — different objects
System.out.println(a.equals(c)); // true — same content
System.out.println(a instanceof String); // true — checks type hierarchy
System.out.println(a.getClass() == String.class); // true — exact type== | equals() | instanceof | getClass() | |
|---|---|---|---|---|
| Compares | References | Content (if overridden) | Type + supertypes | Exact type |
| Works with null | Yes | No (throws NPE) | Yes (returns false) | No (throws NPE) |
Common Mistake 1: Overriding equals() Without hashCode()
// WRONG — equals overridden but not hashCode
public class Product {
String id;
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Product)) return false;
return this.id.equals(((Product) obj).id);
}
// Missing hashCode — HashMap/HashSet will NOT work correctly
}The fix: always override both together.
Common Mistake 2: Comparing with == Instead of equals()
String s1 = new String("test");
String s2 = new String("test");
if (s1 == s2) { // false — different objects
System.out.println("equal");
}
if (s1.equals(s2)) { // true — same content
System.out.println("equal");
}Practice Task
Task: Create a class Book with fields isbn (String) and title (String). Override toString() to print Book[isbn=..., title=...]. Override equals() to consider two books equal if they have the same isbn. Override hashCode() to return isbn.hashCode(). In main, create two Book objects with the same isbn but verify that equals() says they are the same and hashCode() values match.
Hint: In equals(), cast the parameter to Book after the instanceof check. In hashCode(), String.hashCode() is already a good hash.
Solution:
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
What to observe: Even though b1 and b2 have different titles, they are the same ISBN. The HashSet treats them as duplicates (size is 1). Without the hashCode() override, the set would have size 2.
Continue the lesson path