Reference Type - memorylorry/JavaCook GitHub Wiki

Class you define and Array are Reference Type. They have same super Object Class. Let know it:

1.1 Object

...
public static void main(String[] args){
     Object obj;//obj is a reference, default null
} 
...

1.2 The Class Object

/**
* References: Java Language Specification ยง4.3.2
**/
public class TestObject implements Cloneable{

    private String name;
    
    @Override  
    protected TestObject clone() throws CloneNotSupportedException {  
        return (TestObject)super.clone();  
    } 

     public static void main(String[] args){
        TestObject testObject1 = new TestObject();
        TestObject testObject2 = new TestObject();
        TestObject testObject3 = testObject1;
        
        /**
        * The method getClass returns the Class object that represents the class of the object.
        * A class method that is declared synchronized synchronizes on the monitor associated with the Class object of the class.
        **/
        System.out.println(testObject1.getClass());

        //The method hashCode is very useful, together with the method equals , in hashtables such as java.util.HashMap .
        System.out.println(testObject1.hashCode());
        System.out.println(testObject2.hashCode());
        System.out.println(testObject3.hashCode());

        //The method equals defines a notion of object equality, which is based on value,not reference, comparison.
        System.out.println(testObject1.equals(testObject2));
        System.out.println(testObject1.equals(testObject3));

        //The method toString returns a String representation of the object.
        System.out.println(testObject1.toString());
        System.out.println(testObject2.toString());
        System.out.println(testObject3.toString());

        //The method finalize is run just before an object is destroyed.

        //The methods wait , notify , and notifyAll are used in concurrent programming using threads

        //The method clone is used to make a duplicate of an object.(Class must implement Cloneable)
        try{
            Object clonedTestObject = testObject1.clone();
            System.out.println(clonedTestObject.hashCode());
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }

     }
}

1.3 The Class String

1.4 When Reference Types Are the Same