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,void return type,causes the current thread to wait for the specified milliseconds, until another thread notifies (invokes notify() or notifyAll() method).
Java: toString()
Description
The ToString() method of an object class returns a string representation of the object. The string representation format is fully qualified class name.
The toString() method can be invoked explicitly or implicitly.
Whenever toString() method is called using “dot” operator then it is known as explicit call.
When reference variable is printed, java implicitly calls toString() method of that Object,return value of the toString() method will be printed.
A subclass can override the toString() method of object class in such a case in the object user get overrided implementation.
Example
Object Class in Java, Following is an example to understand the toString() method.
ObjectClass.java
[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";
}
}
[/java]
In the above code value of toString() is going to store in str.
[java]String str=obj.toString();[/java]
Output:
When compile the code following is the result will be generated.
[c]
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[/c]
Java: hashCode()
Description
The hashCode() method of an object class is developed to return hash value of the object.
The hash value is a unique integer number generated based on the object number.
A subclass can override the hashCode() method of object class in such case the hashCode() method returns an integer number based on the overrided implementation.
The hashCode() method is generally used to check whether two objects residing at the same address or not.
Example
Object Class in Java, Following is an example to understand the hashcode() method.
HashCodeEx.java
[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;
}
}
[/java]
Output:
When compile the code following is the result will be generated.
[c]
Program starts
retVal1=30758157
retVal2=12773951
retVal3=10542297
retVal4=10542297
Program ends
[/c]
Java: equals()
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.
Comparison is done based on the address of the object.
If current object is compared with another object based on the object fields, then equals() method is overrided.
Example
Object Class in Java, Following is an example to understand equals() method.
[java]
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);
}
}
[/java]
Output:
When compile the code result will be as follows.
[c]
Program starts
Objects are not same
---------------------------------
Objects are same
Program ends[/c]
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.
[java]
s1 = new String("splessons");
s2 = new String("splessons");
[/java]
Now the following is the way to check equivalence of the above two objects.
[java]
if(s1.equals(s2))
System.out.println("s1.equals(s2) is TRUE");
else
System.out.println("s1.equals(s2) is FALSE");
[/java]
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.
[java]
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");
[/java]
In the above case the result will be as FALSE i.e “==” operator compares the objects location in memory.
Summary
Key Points
Object Class in Java, cloning() is used to for less processing task.