The Object Class in Java
Every class inherits from Object, which is where toString, equals, hashCode and getClass come from.
Object, equals and hashCode, packages, nested classes, enums, records and annotations.
10 notes
Every class inherits from Object, which is where toString, equals, hashCode and getClass come from.
Two objects that are equal must have the same hash code. Breaking that contract quietly breaks every hash based collection.
final means cannot be reassigned, cannot be overridden or cannot be extended, depending on where it is written.
instanceof tests the runtime type, and since Java 16 it can bind a typed variable in the same step.
Packages give classes a unique name and a visibility boundary, and imports simply save you from writing that full name every time.
A class declared inside another can be static, inner, local or anonymous, and the difference is mainly what each one can see.
An enum is a class with a fixed set of instances, which makes an invalid value impossible rather than merely unlikely.
A record declares an immutable data carrier, and the compiler generates the constructor, accessors, equals, hashCode and toString.
An annotation attaches metadata to code. It changes nothing by itself; a compiler, a tool or a framework reads it and acts.
Wrapper classes turn primitives into objects so they can be used with generics and collections, and the conversion is automatic but not free.