Skip to content

shaizCodes/learn-java-data-structures

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Learn Java Data Structures

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).

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.

Check the example here.

Important

Whenever you override the equals method, do override the hashcode as well. It is because generally objects with same hashcode are considered equal.

Caution

finalize() is deprecated, avoid using it. Instead refer to WeakReference or PhantomReference. Read more particularly about alternatives here.


Now, let's begin with Collections class to learn about the Java Collections Framework.

About

This repository contains the lessons I learn through the official documentation and practices.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages