object class  in java and method of object class

Hi in this post of core java tutorial i will explain the most important class of java which is Object class and its methods .  Object class is super class of all the classes in java . For Every java class there is a super class, which is object class either in direct way or indirect way. We will see later with program by extending Object class and without extending object class .

Now a common question comes in mind of-course in my mind also is that why Object class is getting extend without any need. What is the need to extend this class by default.

The reason for this is  to give the default identification to the objects , if you aren’t sure about what kind of your objects are , it will be called Object by default .By having the Object as the super class of all Java classes, without knowing the type we can pass around objects using the Object declaration.A collection class like ArrayList allows to store any type of Objects. It was made possible only by Object class hierarchy .In later section of this post we will see how to create our own custom ArrayList with the help of Object classes.

To provide default identification to the objects to any class there  are method provided in Object class .There are nine method in this class which are very useful for any object of any class.

Here are those nine method of object class.We will discuss in detail about every method in this post.

  • protected Object clone() throws CloneNotSupportedException
  • public boolean equals(Object obj)
  • protected void finalize() throws Throwable
  • public final Class getClass()
  • public int hashCode()
  • public String toString()
  • public final void notify()
  • public final void notifyAll()
  • public final void wait()

Now first of all lets see toString(),hashCode() and equals() method of this class. We are having a class Person to override these method.

I am explaining these three method in beginning and together because these three method are overrided in String classes and Wrapper classes.

1. toString() method

toString() method return memory address of any object.There is wrong myth   about toString() method that it convert any object into string.But it is not true for all cases. In case of String classes and Wrapper classes it does the same .I.e in case of String classes and Wrapper classes it convert object into string because in String classes and wrapper classes toString() method has been overrided. So it convert these classes object in to String .I will show you with program. In case of custom classes if you want to perform the same then you need to override toString() method in your own class. If you do not override toString() method in  your custom class and trying to convert it into String then it will return memory address of  the object .I will show you with program.

Return type of toString() method is hashCode of the form com.objectClass.Person@57530551 .

2. hashCode() method

hashCode() method  return memory address of object but its return type is int where as toString() method return memory address in the form of hashCode of the form com.objectClass.Person@57530551.

Return type of hashCode() method is int of the form 1465058641 .

In case of Wrapper classes object hashCode() method return value of the object because it has been overrided in Wrapper classes.

3. equals() method

equals() method compare reference of two object and if both reference point for the same object then it return true otherwise it return false .

But in case of String class and Wrapper class it compare content of the objects as well .So if the content of both the object is same then it return true even if both object point to different object.

Please note that equals() method is overrided only in String class not in StringBuffer and StringBuilder class.

Here is another class PersonManager from which we will call method of this class by creating object .

Here is output

Age : 23 Weight 68.4Height :5.8

In above case i have overrided toString() method ,hashCode() mehod and equals() method in Person class .So output is as expected .

Now lets remove these three method implementation from Person class and then see what output it give.

Here is PersonManager class to test output of toString() ,hashCode() and equals() method.


Output of above program


You can see toString() method has return address of object along with class name .equals() method has return false and hasCode() method has return memory address  in integer from .

Now let see Implementation of these three methods in Wrapper classes and String classes with small program .

Output of above program

toString :401
hashCode :401
equals :true
toString :devender
hashCode :1105824723
equals :true
toString :hello world
hashCode :294576878
equals :false

In String class ,StringBuffer and Wrapper class toString() method return String format of object .

hashCode() method return value of object in Wrapper class while String and StringBuffer class hashCode() method return address of object in integer format.

equals() method compare content of object in String and wrapper class while in StringBuffer class this method has not been overrided so it compare object reference only.

 4. getClass() method

getClass() method return class of an object at runtime .Sometime during program execution when we need to test class of object to which they belong.

Output of above program

class java.lang.Integer

 5.  clone() method

protected Object clone() throws CloneNotSupportedException is used to create copy of an object .This method create copy of a processed object .In many scenario after doing a lot of processing over a object we need copy of this processed object .Then we have two choices one is to create another object and then do same processing over this new object to make it identical other is to use clone method to make copy of processed object .Creating copy of object using clone method is easy one .

Here is simple example of creating copy of an existing object using clone method .

Output of above code

hello clone method
original value :10
cloned value :10

In above program we have created copy of object cloneWorld1 and we see resultant copy is identical .

There are two ways of creating copy of an object .

1. shallow copy

2. deep copy

 In shallow copy after creating copy if we make changes in any object then it will be reflected in other also .

In deep copy after creating copy if we make any changes in any copy then it won’t affect the other .

In later tutorial of Deep Copy And Shallow Copy in java  we will see shallow copy and deep copy in depth with program .

6. Finalize() method

protected void finalize() throws Throwable method is called  before Garbage collector reclaim the Object .In finalize method programmer can write code to release acquired system  resources .This method is can be called with object or it is internally called by garbage collector Garbage collector reclaim the Object.

The below example shows how finalize() method is called like normal function .

Output of above code

finalize is called 

Above code shows how to call finalize() method simply with object .But in most of cases it is called by Garbage collector internally.

Lets see how it is internally called by GC.

Output of above code

Before finalizd called
After finalize called
finalize is called

Here you can see output is not predicted .The reason is that it can not be predicted  exactly when it will be called .Here is small program which shows that garbage collected objects are not always sequential.

Output of above program

Available Free Memory: 60570784
Free Memory before call to gc(): 59648544
Free Memory after call to gc(): 59764416
GC1 object 6149 has been finalized.
GC1 object 2806 has been finalized.
GC1 object 2880 has been finalized.
GC1 object 2949 has been finalized.
GC1 object 3023 has been finalized.
GC1 object 3097 has been finalized.
GC1 object 3171 has been finalized.
GC1 object 3245 has been finalized.
GC1 object 1477 has been finalized.
GC1 object 1547 has been finalized.
GC1 object 2111 has been finalized.
GC1 object 2182 has been finalized. ………..

As you can see we can not predict exactly when finalize() method is called exactly.

There are three more method in this class




We will see these three method in thread tutorial .So that is all about Object class method in java.

I will see you soon with next post very soon .Take care and have fun enjoy programming .



Comments are closed.