Wrapper Classes, Autoboxing and Unboxing in Java
Wrapper classes turn primitives into objects so they can be used with generics and collections, and the conversion is automatic but not free.
Notes related to the Java language.
100 notes tagged
Wrapper classes turn primitives into objects so they can be used with generics and collections, and the conversion is automatic but not free.
An annotation attaches metadata to code. It changes nothing by itself; a compiler, a tool or a framework reads it and acts.
A record declares an immutable data carrier, and the compiler generates the constructor, accessors, equals, hashCode and toString.
An enum is a class with a fixed set of instances, which makes an invalid value impossible rather than merely unlikely.
A class declared inside another can be static, inner, local or anonymous, and the difference is mainly what each one can see.
Packages give classes a unique name and a visibility boundary, and imports simply save you from writing that full name every time.
instanceof tests the runtime type, and since Java 16 it can bind a typed variable in the same step.
final means cannot be reassigned, cannot be overridden or cannot be extended, depending on where it is written.
Two objects that are equal must have the same hash code. Breaking that contract quietly breaks every hash based collection.
Every class inherits from Object, which is where toString, equals, hashCode and getClass come from.
From new to unreachable: how objects are created, how long they live, and what actually happens when nothing references them any more.
Most relationships between classes are has-a, not is-a. Composition is usually the more flexible and more honest design.