Updates
  • Starting New Weekday Batch for Java Full Stack Developer on 24th June 2024 @ 02:00 PM to 04:00 PM
  • Starting New Weekend Batch for Java Full Stack Developer on 06th July 2024 @ 11:00 AM to 02:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 08th July 2024 @ 04:00 PM to 06:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 29th July 2024 @ 10:00 AM to 12:00 PM
Join Course

java.lang.Object

• java.lang.Object class is considered an important class in Java language, because it contains methods which are used frequently in a Java program.
• java.lang.Object class is a super class of all the classes by default, It means when a class is not extending any other class than java.lang.Object class will be the super class of that class.
• Java.lang.Object class members :-

1. public java.lang.Object()
2. public final native java.lang.Class<?> getClass();
3. public native int hashCode();
4. public boolean equals(java.lang.Object);
5. protected native java.lang.Object clone() throws java.lang.CloneNotSupportedException;
6. public java.lang.String toString();
7. public final native void notify();
8. public final native void notifyAll();
9. public final native void wait(long) throws java.lang.InterruptedException;
10. public final void wait(long, int) throws java.lang.InterruptedException;
11. public final void wait() throws java.lang.InterruptedException;
12. protected void finalize() throws java.lang.Throwable;

            
class JTC{
	public static void main(String arg[]){
		A a1 = new A();
		System.out.println(a1.hashCode());
		System.out.println(a1.toString());
	}
}
class A{
}
    339924917
    A@1442d7b5
            

In the above program we have an A class and JTC class and as we can see class A is not extending any other class. So java.lang.Object class is a super class of class A as we discussed, and we know that using sub class object we can access the super class member. We can see the application of this same feature of Java in the last section of the example where we are creating an object of A class A a1 = new A() and using it to invoke hashCode() method and toString() method of java.lang.Object class.

• Now we will discuss different members of java.lang.Object class: -

1. public java.lang.Object();

Member Type: - Default Construction of java.lang.Object class
Access Modifier: - public
Functionality: - It is a default constructor of java.lang.Object class and in Object class there is no other constructor, so we use that constructor to create an object of java.lang.Object class.

2. public final native java.lang.Class<?> getClass();

Member Type: - Final Instance Method
Access Modifier: - public
Return Type: - java.lang.Class<?> (? :- wildcard character which is representing unknown type)
Method Name: - getClass()
Parameter: - No-Parameter
Functionality: - When we invoke getClass() method along with an object of sub class of java.lang.Object class then it returns an object of java.lang.Class which we can use to get different information about the current working object .class file via java.lang.Class methods. Since we can see that getClass() method is a final type of method so we can’t override into the sub class to provide custom implementation. getClass() method is a native method, it means their execution codes have been written in some other native language not in Java.

            
package com.P1;

interface I1{
}
interface I2{
}
interface I3{
}

class A implements I1,I2,I3{

}
class JTC{
	public static void main(String arg[]){
		A a1 = new A();
		Class c1 = a1.getClass();

		System.out.println("----A.class file information----");
		System.out.println("Class Name :- "+c1.getName());
		System.out.println("Class Simple Name :- "+c1.getSimpleName());
		System.out.println("Package Name :- "+c1.getPackage().getName());
		System.out.println("Super Class Name :- "+c1.getSuperclass().getName());
		System.out.println("Super Class Package Name :- "+c1.getSuperclass().getPackage().getName());
		
		System.out.println("---A.class interface information----");

		Class ar1[] = c1.getInterfaces();
		for(int i = 0; i <= ar1.length-1; i++){
			System.out.println("Interface implemented by class A :- "+ar1[i].getName());
		}
	}
}
        
    ----A.class file information----
    Class Name :- com.P1.A
    Class Simple Name :- A
    Package Name :- com.P1
    Super Class Name :- java.lang.Object
    Super Class Package Name :- java.lang
    ---A.class interface information----
    Interface implemented by class A :- com.P1.I1
    Interface implemented by class A :- com.P1.I2
    Interface implemented by class A :- com.P1.I3
            

In the above example we can see that we have three interfaces I1, I2 and I3 and two classes A and JTC. A class is implementing I1, I2 and I3 and there is no super class of A. This means that java.lang.Object class is the super class of A by default and finally we can see that all the classes and interfaces are available in com.P1 package.

In JTC class main method first we are creating an Object of A class which is A a1 = new A() and using a1 we are invoking getClass() of java.lang.Object class, so it will return an Object of java.lang.Class type and because current working object of getClass() method is of invocation, so using java.lang.Class type object we can get the different informations about A.class file.

In the next section of the code we are invoking getName() (c1.getName()) and it returns fully qualified name which is com.P1.A, then we are invoking c1.getSimpleName() and it gives simple name which is A, so you can see we are getting all the information which are related to A.class files and similarly in the further code we are gathering some different informations about A.class file like their package name, super class name, super class package name and finally all the interfaces name which is implemented by A.class file.

3. public native int hashCode();

Member Type: - Instance Method
Access Modifier: - public
Return Type: - int
Method Name: - hashCode()
Parameter: - No-Parameter
Functionality: - As we know an Object is memory allocation in heap which contains a reference which we can store in the compatible type reference variables and in this case we cannot print or we cannot see the reference of an object; at the same time, as per the business logic, we need to track an object in the program and for this we have to use hashCode() method.

When we invoke hashCode() method along with an object then hashCode() of java.lang.Object class returns unique numbers which is known as hash code of the current working object and through which we can easily track an object.

On demand we can easily override the hashCode() into a sub class to provide a custom implementation.

            
class JTC{
	public static void main(String arg[]){
		A a1 = new A();
		A a2 = new A();
		A a3 = new A();
		System.out.println("a1 hashcode :- "+a1.hashCode());
		System.out.println("a2 hashcode :- "+a2.hashCode());
		System.out.println("a3 hashcode :- "+a3.hashCode());
		System.out.println("a1 hashcode :- "+a1.hashCode());
	}
}
class A{
}
        
    a1 hashcode :- 37380050
    a2 hashcode :- 2023938592
    a3 hashcode :- 231977479
    a1 hashcode :- 37380050
            

In the above example we can see that we are creating three different objects of A class which are A a1 = new A(), A a2 = new A(), A a3 = new A() and then we are invoking hashCode() method of java.lang.Object class and here we will get different hash code for each object; finally we are again invoking hashCode() along with a1 and we will get same hash code as we had got for the first invocation with a1; this proves the default implementation feature of hashCode() of java.lang.Object class, that if hashCode() method creates a hash code for an object then it will be the same object until the object is destroyed.

            
class JTC{
	public static void main(String arg[]){
		Student s1 = new Student();
		Student s2 = new Student();
		Student s3 = new Student();
		System.out.println("s1 hashCode :- "+s1.hashCode());
		System.out.println("s2 hashCode :- "+s2.hashCode());
		System.out.println("s3 hashCode :- "+s3.hashCode());
		System.out.println("s1 hashCode :- "+s1.hashCode());
	}
}
class Student{
	static int i;
	
	// overriding hashCode() of java.lang.Object class into the Student class.

	public int hashCode(){
		return ++i;
	}
}
    s1 hashCode :- 1
    s2 hashCode :- 2
    s3 hashCode :- 3
    s1 hashCode :- 4
            

In this example we are working on a requirement and we will understand how we can override hashCode() of java.lang.Object class into a sub class. As per the current requirement we need hash code in a sequence at every invocation of hashCode() via Student class object whether the object is same or not.

4. public boolean equals(java.lang.Object);

Member Type: - Instance Method
Access Modifier: - public
Return Type: - boolean
Method Name: - equals()
Parameter: - java.lang.Object
Functionality: - equals(Object) method of java.lang.Object is basically used to compare two objects on the basis of reference by default, if both reference variables contain same object reference then it returns true otherwise it returns false.

As we can see equals(Object) method is not a final method so we can easily override equals() in sub classes to implement the custom comparison.

            
class JTC{
	public static void main(String arg[]){
		A a1 = new A();
		A a2 = new A();
		System.out.println(a1.equals(a2)); // false
		a1 = a2;
		System.out.println(a1.equals(a2)); // true
	}
}
class A{
}
    false
    true
            

In this example we will see how we can compare two objects using equals (Object) method of java.lang.Object class. As we can see that first we are comparing two different objects of A class A a1 = new A() and A a2 = new A() using equals(Object) method, so we are getting false and in the next line we are assigning a2 reference into a1; it means a1 and a2 are having the same object reference and that is the reason in the last comparison we are getting true.

            
class JTC{
	public static void main(String arg[]){
		Student s1 = new Student(101,"Vivek Sareen", "Noida");
		Student s2 = new Student(102,"Rahul","Delhi");
		System.out.println(s1.equals(s2)); // false
		Student s3 = new Student(102,"Amit","Mumbai");
		System.out.println(s2.equals(s3)); // true
	}
}
class Student{
	int sid;
	String sname;
	String scity;

	Student(int sid, String sname, String scity){
		this.sid = sid;
		this.sname = sname;
		this.scity = scity;
	}
	
	// Overriding equals() method of java.lang.Object class 

	public boolean equals(Object o){
		Student s1 = (Student) o;
		if(this.sid == s1.sid){
			return true;
		}else{
			return false;
		}
	}
	
}
    false
    true
            

In this example we are comparing two students on the basis of their sid, so here we have an overridden equals(Object) method in student class which is comparing two student objects on the basis of their sid; if both student objects have different sid then the method returns false; only if both the objects have the same sid value, the return is displayed as true. This example helps us to understand that we can easily override the equals() method as per the business logic of the module.

5. protected native java.lang.Object clone() throws java.lang.CloneNotSupportedException;

Member Type: - Instance Method
Access Modifier: - protected
Return Type: - java.lang.Object
Method Name: - clone()
Parameter: - No parameter
Functionality: - In Java we have a Cloning feature in which we create the clone or copy of an existing object and clone() of java.lang.Object class for creating a clone of a specific object.

By default in Java we have clone() method of performing Shallow Cloning but as per the requirement we can override the clone() to achieve Deep Level Cloning.

6. public java.lang.String toString(): -

Member Type: - Instance Method
Access Modifier: - public
Return Type: - java.lang.String
Method Name: - toString()
Parameter: - No parameter
Functionality: - In Java generally we use objects and we store their reference into the reference variable; however as per our previous discussion we can’t print or see an object reference, so whenever we print the reference variable then it internally invokes the toString() method of java.lang.Object class and we get an output like: - <classname>@hashCode in hexa-decimal format which is the default implementation of toString() in java.lang.Object class and on demand we can override the toString() in sub class to provide some meaningful output.

            
class JTC{
	public static void main(String arg[]){
		A a1 = new A();
		B b1 = new B();
		System.out.println("a1 :- "+a1);
		System.out.println("a1.toString() :- "+a1.toString());
		System.out.println("b1 :- "+b1);
		System.out.println("b1.toString() :- "+b1.toString());
	}	
}
class A{
}
class B{
}
    a1 :- A@516be40f
    a1.toString() :- A@516be40f
    b1 :- B@3c0a50da
    b1.toString() :- B@3c0a50da
            

In the above example we have three classes class A, class B and JTC class. In JTC class we have an object of A class which is A a1 = new A() and an object of class B which is B b1 = new B().
In the next line we are printing the value of a1and thereafter we are invoking the toString() of java.lang.Object class along with a1, and you can see that we are getting the same output in both lines ; this same feature we are implementing along with an object of B class and this proves that internally the toString() method works when we print an object reference or an object.

            
class JTC{
	public static void main(String arg[]){
		Student s1 = new Student(101,"Vivek","Noida");
		Student s2 = new Student(102,"Rahul","Delhi");
		System.out.println(s1);
		System.out.println(s2);
	}
}
class Student{
	int sid;
	String sname;
	String scity;

	Student(int sid, String sname, String scity){
		this.sid = sid;
		this.sname = sname;
		this.scity = scity;
	}
	
	public String toString(){
		return "[ Sid :- "+sid+" | Sname :- "+sname+" | Scity :- "+scity+" ]";
	}
}
    [ Sid :- 101 | Sname :- Vivek | Scity :- Noida ]
    [ Sid :- 102 | Sname :- Rahul | Scity :- Delhi ]
            

In the above example we can see that in Student class we are overriding toString() to provide some meaningful output; as you can see that in Student class we have overridden toString() which is thereafter returning Student details in a specific format like: -

[ Sid :- 101 | Sname :- Vivek | Scity :- Noida ]
[ Sid :- 102 | Sname :- Rahul | Scity :- Delhi ]

7. public final native void notifyAll();

Member Type: - Instance Method
Access Modifier: - public
Return Type: - void
Method Name: - notifyAll()
Parameter: - No parameter
Functionality: - In Java when we invoke notifyAll() of java.lang.Object class all the threads move from waiting state into ready to run state in a particular sequence. The longest waiting thread moves first followed by the second longest thread and so on.

8. public final native void wait() throws java.lang.InterruptedException;

Member Type: - Instance Method
Access Modifier: - public
Return Type: - void
Method Name: - wait()
Parameter: - No parameter
Functionality: - We use wait() method of java.lang.Object class to send current working thread to wait state for unspecified duration of time period. Once a thread moves to the wait state for unspecified duration of time period then it comes out from the waiting state only when we call notify() or notifyAll() method.

9. public final void wait(long) throws java.lang.InterruptedException;

Member Type: - Instance Method
Access Modifier: - public
Return Type: - void
Method Name: - wait()
Parameter: - long
Functionality: - When we use wait(long) method then the current working thread moves to wait statement for a specified duration of time, We can specify the time in milliseconds via long type argument, once the specified time duration is completed than automatically thread will move to the ready to run state. On the basis of business logic we can notify the thread before completing that time duration.

10. public final void wait(long, int) throws java.lang.InterruptedException;

Member Type: - Instance Method
Access Modifier: - public
Return Type: - void
Method Name: - wait()
Parameter: - long, int
Functionality: - When we use wait(long,int) method then the current working thread moves to wait statement for a specified duration of time. We can specify the time in milliseconds via long type argument and nanoseconds via int type argument. Once the specified time duration is completed then automatically the thread moves to the ready state and thereafter to the run state. On the basis of business logic, we can notify the thread before the completion of the time duration.

11. protected void finalize() throws java.lang.Throwable;

Member Type: - Instance Method
Access Modifier: - protected
Return Type: - void
Method Name: - finalize()
Parameter: - No-Parameter
Functionality: - In Java we have very important feature called Garbage Collection ¬¬which is deployed to clean unused or unreferenced objects from the Heap and its execution is carried out by JVM. Hence there is no need to write any code in order to clean up the Heap memory allocation. JVM uses finalize() method internally to release the unused or unreferenced objects from the Heap.