Java Object Class and Methods with Examples
Java.lang.Object classFor any java object whether it is predefine or customized the most commonly required methods are encapsulated into a separate class which is nothing but an object class.
As object class acts as a root (or) parent (or) super for all java classes, by default its methods are available to every java class.
If our class doesn't extends any other class then it is the direct child class of object If our class extends any other class then it is the indirect child class of Object.
There is a total of 11 methods present in java.lang Object class.
1). public String toString()
2). public boolean equals(Object o)
3). public native int hashCode()
4). protected native Object clone()throws CloneNotSupportedException
5). public final Class getClass()
6). protected void finalize()throws Throwable
7). public final void wait() throws InterruptedException
8). public final native void wait()throws InterruptedException
9). public final void wait(long ms,int ns)throws InterruptedException
10). public final native void notify()
11). public final native void notifyAll()
1). public String toString()
We can use this method to get a string representation of an object. Whenever we are trying to print any object reference internally toString() method will be executed. If our class doesn't contain the toString() method then the Object class toString() method will be executed. e.g
System.out.println(s1); => super(s1.toString());
public class Student
{
String name;
int rollno;
Student(String name, int rollno)
{
this.name=name;
this.rollno=rollno;
}
public static void main(String args[]){
Student s1=new Student("saicharan",101);
Student s2=new Student("ashok",102);
System.out.println(s1);
System.out.println(s1.toString());
System.out.println(s2);
}
}
Output:
Student@3e25a5
Student@3e25a5
Student@
In the above program Object class toString() method got executed which is implemented as follows.
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
Here getClass().getName() => classname@hexa_decimal_string_representation_of_hashCode.
To provide our own String representation we have to override toString() method in our class.
public String toString() {
return name + " " + rollno;
}
In String class, StringBuffer, StringBuilder, wrapper classes, and in all collection classes toString() method is overridden for meaningful string representation. Hence in our classes also highly recommended to override the toString() method.
public class MyClass {
public String toString() {
return "Hello";
}
public static void main(String[] args) {
Integer i = new Integer(111);
String s = new String("Sara");
MyClass myClass = new MyClass();
System.out.println(i);
System.out.println(s);
System.out.println(myClass);
}
}
output:
111
Sara
Hello
2). public boolean equals(Object o)
- We can use this method to check the equivalence of two objects.
- If our class doesn't contain .equals() method then object class .equals() method will be executed which is always meant for reference comparison[address comparison]. i.e., if two references pointing to the same object then only .equals( ) method returns true.
public class Student {
String name;
int rollno;
Student(String name, int rollno) {
this.name = name;
this.rollno = rollno;
}
public static void main(String[] args) {
Student s1 = new Student("Sara", 101);
Student s2 = new Student("Abhijit", 102);
Student s3 = new Student("Neelam", 101);
Student s5 = new Student("Vaman", 101);
Student s4 = s1;
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s1.equals(s4));
System.out.println(s1.equals(s5));
}
}
output:
false
false
true
false
In the above program Object class .equals() method got executed which is always meant for reference comparison that is if two references pointing to the same object then only .equals(() method returns true.
In object class .equals() method is implemented as follows which is meant for reference comparison.
public boolean equals(Object obj) {
return (this == obj);
}
When ever we are overriding .equals() method we have to consider the following things
- Based on our programming requirement we can override .equals() method for content comparison purpose.
- Meaning of content comparison i.e., whether we have to check the names are equal (or) roll numbers (or) both are equal.
- If we are passing different type of objects (heterogeneous object) our .equals() method should return false but not ClassCastException i.e., we have to handle ClassCastException to return false.
- If we are passing null argument our .equals() method should return false but not NullPointerException i.e., we have to handle NullPointerException to return false.
Simplified version of .equals() method
public boolean equals(Object o) {
if (this == o) return true;
if (o instanceof Student) {
Student s2 = (Student) o;
if (name.equals(s2.name) && rollno == s2.rollno) return true;
else return false;
}
return false;
}
3). public native int hashCode()
- For every object, JVM will generate a unique number which is nothing but hashCode.
- Jvm will use hashCode while saving objects into hashing related data structures like HashSet, HashMap, and Hashtable, etc.
- If the objects are stored according to hashCode searching will become very efficient (The most powerful search algorithm is hashing which will work based on hashCode).
- If we didn't override the hashCode() method then the Object class hashCode() method will be executed which generates hashCode based on the address of the object but it doesn't mean hashCode represents the address of the object.
- Based on our programming requirement we can override the hashCode() method to generate our own hashcode.
- Overriding hashCode() method is said to be proper if and only if for every object we have to generate a unique number as hashcode for every object
public class Test {
int i;
Test(int i) {
this.i = i;
}
public static void main(String[] args) {
Test t1 = new Test(999);
Test t2 = new Test(1000);
System.out.println(t1);
System.out.println(t2);
}
}
output:
Test@6d06d69c ==== Object class toString() called.
Test@7852e922 ==== Object==>hashCode() called.
In this case Object class toString( ) method got executed which is internally calls Object class hashCode( ) method.
public class ObjectTest {
int i;
ObjectTest(int i) {
this.i = i;
}
public int hashCode() {
return i;
}
public static void main(String[] args) {
ObjectTest t1 = new ObjectTest(10);
ObjectTest t2 = new ObjectTest(100);
System.out.println(t1);
System.out.println(t2);
}
}
output:
ObjectTest@a ==== Object==>toString() called.
ObjectTest@64 ==== Test==>hashCode() called.
In this case Object class toString( ) method got executed which is internally calls ObjectTest class hashCode( ) method.
Note :
1). If we are giving the opportunity to Object class toString() method it internally calls hashCode() method. But if we are overriding the toString() method it may not call the hashCode() method.
2). We can use the toString() method while printing object references and we can use the hashCode() method while saving objects into HashSet or Hashtable or HashMap.
4). protected native Object clone()throws CloneNotSupportedException
5). public final Class getClass()
6). protected void finalize()throws Throwable
7). public final void wait() throws InterruptedException
8). public final native void wait()throws InterruptedException
9). public final void wait(long ms,int ns)throws InterruptedException
10). public final native void notify()
11). public final native void notifyAll()
Post/Questions related to Java Object Class and Methods with Examples
Override equals method in java
In this article, we have seen Java Object Class and Methods with Examples.
0 Comments
Post a Comment