Saturday, November 19, 2016

Java Object Class methods

Object class is the parent class of any java class whether it is predefined or user defined. The most commonly used and general purpose classes which are required for any java program are grouped into a package which is nothing but java.lang package.All the classes and interfaces which are available in this package are by default available to any java program ,no need to import this package. Every class in java is a direct or indirect sub class of the Object class. 

Object class:

The most common general methods which are applicable on any object are defined in object class.This class provides methods to compare objects,to covert an object into a string, to notify threads regarding the availability of an object.

Object class defines the fallowing methods:

1) equals(): 

This method is used to compare two object for equality and if they are equal,it returns true otherwise false.Let us see how to compare two objects by using equals() method of Object class.

Generally Object class equals() methods compares the reference of two objects. If both object refer to same object then it gives true otherwise false. But based on our requirement it is recommended to override equals() method for content comparison. 

Example:

class EqualsDemo
{
public static void main(String args[])
{
String s1="HI";
String s2="HI";
String s3="welcome to java";
System.out.println(s1 + " equals " + s2 + " -> " +
s1.equals(s2));
System.out.println(s1 + " equals " + s3 + " -> " +
s1.equals(s3));
}
}


Output:





In the above program Object class equals() methods has executed which is meant for reference comparison. But base on our requirement it is recommended to override equals() method for content comparison.

Example:

class Employee
{
String name;
int rollno;
Employee(String name,int rollno)
{
this.name = name;
this.rollno = rollno;
}
public boolean equals(Object obj)
{
try
{
String name1 = this.name;
int rollno1 = this.rollno;
Employee e2 = (Employee)obj;
String name2 = e2.name;
int rollno2 = e2.rollno;
if(name1.equals(name2) && rollno1 == rollno2)
{
return true;
}
else
{
return false;
}
}
catch (ClassCastException c)
{
return false;
}
catch (NullPointerException e)
{
return false;
}
}
public static void main(String arg[])
{
Employee e1 = new Employee ("pavan", 1);
Employee e2 = new Employee ("mahesh", 2);
Employee e3 = new Employee ("Eeswar", 3);
System.out.println(e1.equals(e2));
System.out.println(e2.equals(e3));
System.out.println(e1.equals(null));
}
}


Output:







Recommended to Read:  Java Command Line Arguments tutorial

2) toString():

This method returns a string representation of an object. 

Syntax:
public String toString()
{
return getClass.getName();
}

Example:

class Employee
{
String name;
int rollno;
Employee(String name,int rollno)
{
this.name = name;
this.rollno = rollno;
}

public static void main(String args[])
{
Employee e1=new Employee("nagaraju",1);
Employee e2=new Employee("prasad",2);
System.out.prinltn(e1);
System.out.prinltn(e2);
}
}
Output:





Whenever we are passing object reference as argument to System.out.println() internally JVM will call toString() on that object. If we are not providing toString() then Object class toString() will be executed which is implemented as fallows:

public String toString()
{
return getClass.getName()+'@'+Integer.toHexString(hashcode);
}
Based on our requirement to provide our own String representation we have to override toString(). Let us rewrite the above program by overriding toString() method


Example:

class Employee
{
String name;
int rollno;
Employee(String name,int rollno)
{
this.name = name;
this.rollno = rollno;
}
public String toString()
{
return name+"-------"+rollno;
}

public static void main(String args[])
{
Employee e1=new Employee("nagaraju",1);
Employee e2=new Employee("prasad",2);
System.out.println(e1);
System.out.println(e2);
}

}

Output:





3) getClass():

This method returns the runtime class of an object. 




4) hashCode():

This method returns a hash code value of the object. The hash code of an object just represents a random number which can be used by JVM while saving objects into hash tables,HashSet and HashMap.

hashCode() of an Object class implemented to return hashCode based on address of an object,but based on our requirement we can override hashCode() to generate our own numbers as hashCode.

Example:

class Demo
{
int a;
Demo(int a)
{
this.a = a;
}
public int hashCode()
{
return a;
}
public static void main(String arg[])
{
Demo d1 = new Demo(10);
Demo d2 = new Demo(20);
System.out.println(d1); 
System.out.println(d2);
}

}
Output:






Observe above program output JVM automated generated numbers displayed. So to get our own number we have to override hashCode(). Let us Rewrite the above program as fallows:

Example:
class Demo
{
int a;
Demo(int a)
{
this.a = a;
}
public int hashCode()
{
return a;
}
public String toString()
{
return a + "";
}
public static void main(String arg[])
{
Demo d1 = new Demo(10);
Demo d2 = new Demo(20);
System.out.println(d1); 
System.out.println(d2);
}
}


Output:





5) clone():

The process of creating exactly duplicate object is called clonning. 

Example:
class Demo implements Cloneable
{
int x = 10;
int y = 20;
public static void main(String arg[])throws CloneNotSupportedException
{
Demo d1 = new Demo();
Demo d2 = (Demo)d1.clone();
d1.x = 100;
d1.y = 200;
System.out.println(d2.x+"----"+d2.y); 
}
}
Output:




6) finalize():

This method is called by garbage collector on an object when garbage collection determine that there are no more reference to the object.

7) notify():

This method is used to wake up single thread that is waiting on the object's monitor.

8) notifyAll():

This method is used to wake up all the threads that are waiting on the object's monitor.

9) wait():

This method causes to waits to be notified by another thread of a change in this object.


Let me know your comments on this post and keep follow me for latest updates

No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...