Skip to content

Reference Type

memorylorry edited this page May 13, 2018 · 2 revisions

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

1.1 Object

An object is a _class instance_ or an array.

The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object.

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

1.2 The Class Object

The class Object is a superclass of all other classes.

All class and array types inherit the methods of class Object , which are summarized as follows:

/**
* 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

HOME

  1. Type, Value, Variable
  1. Class
  2. Interface
  3. Modifier
  4. Arrays
  5. Exception
  6. Threads and Locks
Clone this wiki locally