java.lang.Object Class
🔹 Introduction
Object is a class which is available inside java.lang package.
It is by default a class e.g. without extending explicitly.
It is the immediate super class for all types of classes in java by default.
As it is the super class for all the class, all the member of object class can be accessed inside any class.
🔹 Object Class Members
Public class object{
1. public final native Class getClass();
2. public native inthashCode();
3. public Boolean equals(Object);
4. protected native Object clone() throws CloneNotSupportedException;
5. public String toString();
6. public final native void notify();
7. public final native void notifyAll();
8. public final native void wait(long)throws InterruptedException;
9. public final native void wait(long,int)throws InterruptedException;
10. public final void wait() throws InterruptedException;
11. protected void finalize() throws Interrupted Exception;
}🔹 getClass() Method
public final native class getClass() methodgetClass() is a method which is available inside the java.lang.Object class.
getClass() method is a native method which means it is a non-java programming implementation.
It cannot be overridden inside the subclass because it is a final method.
It is public method which can be accessed inside or outside the package.
That class method is used to get the class name which object reference is available inside corresponding reference variable.
The return type of this method is java.lang.Class
🔹 Example
package com.p1;
class Hello {
}
class A {
}
class B extends A {
}
public class MainObj {
public static void main(String[] args) {
System.out.println("in main");
Hello h1 = new Hello();
Class cls = h1.getClass();
System.out.println(cls);
String cname = cls.getName();
System.out.println(cname);
Hello h2 = null;
// System.out.println(h2.getClass().getName());
// java.lang.NullPointerException
System.out.println("*******'");
A a1 = new A();
A a2 = new B();
Object o = new String("abc");
System.out.println(a1.getClass().getName());
System.out.println(a2.getClass().getName());
System.out.println(o.getClass().getName());
}
}🔹 hashCode() Method
public native int hashCode() methodWhen you are creating any object, for all the objects hash code will be assigned internally by the JVM.
On the basis of that hash code, JVM identifies each object uniquely.
For each object there will be unique generation of hashCode(), and this method is mainly responsible to generate the hashCode() and retunes in form of integer.
According to requirements you can override hashCode method inside your class and provide your own implementation to generate the hashCode().
🔹 Syntax
public int hashCode() {
//own implementation
return…;
}🔹 Types of Implementation
I. Always returns the unique hashCode.
II. Static implementation which returns same hashCode again and again.
It is always recommendable to write the hashCode() method implementation in such a way that return a unique hashCode for each object.
🔹 equals(Object) Method
public boolean equals(Object) method:• Equals() method which is available inside java.lang.Object class.
It is used to compare two objects in the following ways:
a) On the basis of reference
b) On the basis of contents
• If you are using equals(Object) method default implementation which is available inside java.lang.Object class, then it will compare on the basis of Reference.
• But if you want to compare two Objects on the basis of contents then override the equals(Object) method in the class with your own implementation with the following signature:
public boolean equals(Object o){
return…;
}🔹 Contract between equals() and hashCode()
Two equivalent Object should be placed in the same bucket but all the object available in the same bucket many not be equals also.
Two equivalent Object: must have same hashCode()
i.e h.equals (h2) is true the h1.hashCode()==h2.hashCode() should return true.If two objects are not equal by equals(..)method then there is no restriction on their hashCode, may be same or may not be same.
If hashcode of two object are equals then these object may or may not equal by .equals method.
If hashCode of two object are not equals then these object are always not equal by .equals method.
🔹 Example
Class Hello{
Public boolean equals(Object o){
If(o instanceof Hello){
Hello h=(Hello)o;
If(name.equals(p.name)&& age==p.age)
Return true;
}else false;
Return false;
}
}🔹 toString() Method
public String toString() method:• toString() is the method which is available inside java.lang.Object class.
It is used to give some information about the class.
• By default internal implementation of toString() method in a class is giving the hash code in the form of hexadecimal implementation.
• If you want to give some other information using toString() method then you can override the toString() method by writing your own Implementation.
🔹 Default Implementation
public String toString(){
return this.getClass().getName()+"@"+Integer.toHexString(hashCode());
}🔹 Example (Student Class)
package com.p2;
class Student {
int sid;
String name;
String city;
Student() { }
Student(int sid, String name, String city) {
this.sid = sid;
this.name = name;
this.city = city;🔹 Methods
void show() {
System.out.println("show in Student");
System.out.println(sid);
System.out.println(name);
System.out.println(city);
}
/*
public String toString(){
return this.getClass().getName()+"@"+Integer.toHexString(hashCode());
}
*/
public String toString() {
return "sid\t:" + sid + "Name\t:" + name + "city\t:" + city;
}
public int hashCode() {
return 33;
}
public boolean equals(Object o) {
Student s1 = (Student) o;
if ((s1.sid == this.sid) &&
(s1.name.equals(this.name) &&
(s1.city.equals(this.city))))
return true;
return false;
}
}🔹 Main Class
public class Main {
public static void main(String arg[]) {
Student s1 = new Student(101, "Lokesh", "noida");
Student s2 = new Student(101, "Lokesh", "noida");
Student s3 = new Student(102, "Lokesh", "noida");
s1.show();
s2.show();
s3.show();
System.out.println("********\n");
System.out.println(s1);
System.out.println(s2.toString());
System.out.println(s3);
System.out.println("***COMPARE***\n");
/*
boolean bn=s1.equals(s2);
System.out.println(bn);
*/
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
System.out.println(s1.hashCode() == s2.hashCode());
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
}
}