It contains the lessons I learn through the official Java documentation and practices.
Before we begin, let's proceed to Object class as every object has its methods.
Object is the root in class hierarchy. Every object is an Object (extends from it or has its non-private properties and methods).
-
public Object() constructor, which we will probably never use to initialize any object.
-
protected Object clone() throws CloneNotSupportedException method, returns a shallow copy of an object, however, if Cloneable interface is not implemented by that object, CloneNotSupportedException will be thrown. Object class does not implement Cloneable. Check this sample program.
Caution
A shallow copy creates a new object that holds the same top-level properties as the original, but any nested objects are still references to the original. A deep copy, on the other hand, creates a completely independent copy of the object and all its nested objects, ensuring no shared references.
- public String toString() method returns the object information; complete class name, and object hashcode in hexadecimal. The hashcode is useful to verify whether an object is modified or not.
Note
An object hashcode changes when the object is modified, and multiple objects can have same hashcode when they are equal but it is not necessary.
-
public boolean equals(Object obj) method verifies whether two objects are equal by checking the following critera:
Reflexive:
$\quad x \neq \text{null}, \quad x.equals(x) \implies \text{true}$ Non-nullity:
$\quad x \neq \text{null}, \quad x.equals(\text{null}) \implies \text{false}$ Symmetric:
$\quad x, y \neq \text{null}, \quad x.equals(y) \iff y.equals(x)$ Transitive:
$\quad x, y, z \neq \text{null}, \quad x.equals(y) \land y.equals(z) \implies x.equals(z)$ Consistent:
$\quad x, y \neq \text{null}, \quad x.equals(y) \text{ returns the same result unless } x \text{ or } y \text{ is modified}$
Tip
Objects.hash(Object... values) can be used to generate hashcode for objects that have multiple fields.
Important
Whenever you override the equals method, do override the hashcode as well. It is because generally objects with same hashcode are considered equal.
- protected void finalize() throws Throwable method is to define the actions taken by finalization of an object. Such as closing database connection before discarding all the resources.
Caution
finalize() is deprecated, avoid using it. Instead refer to WeakReference or PhantomReference. Read more particularly about alternatives here.
-
public final void wait() throws InterruptedException, IllegalMonitorStateException method causes a thread to wait (pause its operations) on an object until it is not notified (given access to the object's monitor).
-
public final void notify() throws IllegalMonitorStateException method notifies (awakens) a waiting thread to resume its operations on an object.
-
public final void notifyAll() throws IllegalMonitorStateException method notifies (awakens) all waiting threads to resume their operations on an object. However, the object's monitor can be owned by one at a time.
Now, let's begin with Collections class to learn about the Java Collections Framework.