Core Java - SPLessons

Object Class in Java

Chapter 24

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Object Class in Java

Object Class in Java

shape Description

Object Class in Java, In any object oriented programming language coding will be made up with classes and object, As every one know that object is nothing but an entity that have state, behavior. Object Class is the super most class in Java. Every instance, should inherit Object Class members. Object class is available in java.lang package. The following are few methods provided by Object Class.


Public method,string return type.Returns the object address in string format.
Public method,int return type.Returns hash value of the Object.
Public method, boolean return type.Returns true if objects are same/equal.
Protected method,void return type.Checks a objects contains reference variable.
Protected method,Object return type.Clones an existing object.
Public method,void return type, wakes up single thread, waiting on this object’s monitor.
Public method,void return type, wakes up all the threads, waiting on this object’s monitor.
Public method,void return type,causes the current thread to wait for the specified milliseconds, until another thread notifies (invokes notify() or notifyAll() method).

Java: toString()

shape Description

The ToString() method of an object class returns a string representation of the object. The string representation format is fully qualified class name.

shape Example

Object Class in Java, Following is an example to understand the toString() method.
ObjectClass.java

package com.spl.object;

public class ObjectClass {
	public static void main(String[] args) {
		System.out.println("Program starts");
		Object obj=new Object();
		String str=obj.toString();//value of toString() which is stored in str
		System.out.println("String value="+str);
		Sample1 sRef=new Sample1();
		String str1=sRef.toString();
		System.out.println("str1 value="+str1);
		System.out.println("sRef value="+sRef);
		Sample2 ref=new Sample2();
		System.out.println("ref value="+ref);
		System.out.println("Program ends");
		
	}
}
class Sample1
{
	
}
class Sample2
{
	//overriding toString() method of Object class
	public String toString()
	{
		return "Sample2 Object";
	}
}

In the above code value of toString() is going to store in str.

String str=obj.toString();

Output:
When compile the code following is the result will be generated.

Program starts
String value=java.lang.Object@ififba0
str1 value=com.spl.object.Sample1@1befab0
sRef value=com.spl.object.Sample1@1befab0
ref value=Sample2 Object
Program ends

Java: hashCode()

shape Description

The hashCode() method of an object class is developed to return hash value of the object.

shape Example

Object Class in Java, Following is an example to understand the hashcode() method.
HashCodeEx.java

package com.spl.object;

import java.util.Scanner;

public class HashCodeEx {
	public static void main(String args[]){  
		   System.out.println("Program starts");
		   Sample s1=new Sample();
		   int retVal1=s1.hashCode();
		   Sample s2=new Sample();
		   int retVal2=s2.hashCode();
		   System.out.println("retVal1="+retVal1);
		   System.out.println("retVal2="+retVal2);
		   Sample5 s3=new Sample5();
		   int retVal3=s3.hashCode();
		   Sample5 s4=s3;
		   int retVal4=s4.hashCode();
		   System.out.println("retVal3="+retVal3);
		   System.out.println("retVal4="+retVal4);
		   System.out.println("Program ends");
		 }  
}
class Sample
{
	
}
class Sample5
{
	public int hashcode()
	{
		return 23456;
	}
}

Output:
When compile the code following is the result will be generated.

Program starts
retVal1=30758157
retVal2=12773951
retVal3=10542297
retVal4=10542297
Program ends

Java: equals()

shape Description

The equals() method of an object class compares the current object with argument object.Returns true, if both the object are same else false.

shape Example

Object Class in Java, Following is an example to understand equals() method.

package com.spl.file;

public class EqualsEx {
	public static void main(String args[]){  
		   System.out.println("Program starts");
		   Sample5 s1=new Sample5(54);
		   Sample5 s2=new Sample5(54);
		   //comparing address of object(do not do this type of comparison)
		   if(s1==s2)
		   {
			   System.out.println("Objects are same");
		   }
		   else
		   {
			   System.out.println("Objects are not same");
		   }
		   System.out.println("----------------------------");
		  //compare fields of objects
		   if(s1.equals(s2)){
			   System.out.println("Objects are same");
		   }
		   else
		   {
			   System.out.println("Objects are not same");
		   }
		   System.out.println("Program ends");
		 }  
}
class Sample5
{
	int i;
	Sample5(int i)
	{
		this.i=i;
	}
	public boolean equals(Object arg1){
		Sample5 ref=(Sample5)arg1;
		return (this.i==ref.i);
		
	}
}

Output:
When compile the code result will be as follows.

Program starts
Objects are not same
---------------------------------
Objects are same
Program ends

Difference between equals() and ==

The equals() method and == operator will have more differences such as follows.
The equals() present in the java.lang.Object and it is used to expel equivalence state of an objects. The following is an example.

s1 = new String("splessons");
s2 = new String("splessons");

Now the following is the way to check equivalence of the above two objects.

if(s1.equals(s2))
System.out.println("s1.equals(s2) is TRUE");
else
System.out.println("s1.equals(s2) is FALSE");

When user compile the above code the result will be as TRUE why because the both objects have the same text.
The == operator is utilized to compare 2 objects, it expels to find if the given objects refer to the same place in memory or not, the following is an example.

String obj1 = new String("xyz");
String obj2 = new String("xyz");
if(obj1 == obj2)
   System.out.println("obj1==obj2 is TRUE");
else
  System.out.println("obj1==obj2 is FALSE");

In the above case the result will be as FALSE i.e “==” operator compares the objects location in memory.


Summary

shape Key Points

  • Object Class in Java, cloning() is used to for less processing task.