Java Object Class
In this tutorial, we will learn about java.lang.Object
class, important methods of it along with significance and working examples of it to understand better.
java.lang.Object Class
In Java, the Object
class is the parent or super class of all classes and declares a set of methods that are available to all objects in the Java runtime environment. Most used methods in Object class are wait()
,notify()
,equals()
,toString
()
,and hashCode
()
and each method of it gives a better control over the objects.
We will explore more commonly used methods with explanation and examples of it.
java.lang.Object class methods
equals(Object
obj
)
: This method is used to check if two objects are equal or not. It returns a boolean value, in-case true
means both the objects are equal and otherwise false
. The equals
()
method is commonly overridden in case of user-defined classes to provide a customized implementation of object equality.
java.lang.Object class equals() method example:
In this example we will use-defined class to compare two objects.
public class Movie {
private String name;
private String language;
public static void main(String[] args) {
Movie movie1 = new Movie("Eagle Eye","English");
Movie movie2 = new Movie("Hacker","English");
//returns true, compared with the same movie object
System.out.println("Both are same movies?:"+movie1.equals(movie1));
//returns false, compared with two different movie objects
System.out.println("Both are same movies?:"+movie1.equals(movie2));
}
public Movie(String name, String language) {
this.name = name;
this.language = language;
}
@Override //Overridden equals method of Object class
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
Movie secondObject = (Movie) obj;
if (language.equals(secondObject.language) && name.equals(secondObject.name))
return true;
return false;
}
}
Output:
Both are same movies?:true
Both are same movies?:false
java.lang.Object class hashCode() method:
hashCode
()
: This method is used to return a hash code value of an object. The hash code is used by hash-based collections to find duplicates, store, and retrieve objects. The hashCode
method is commonly overridden in user-defined classes to provide a customized implementation of object hash code generation but it should same value for equal objects and different values for not equal objects.
Example:
public class Movie {
private String name;
private String language;
public Movie(String name, String language) {
this.name = name;
this.language = language;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((language == null) ? 0 : language.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
}
java.lang.Object class toString() method:
toString
()
: This method is used to return a string representation of an object. The default implementation of the toString
method returns the class name followed by the object's hash code in hexadecimal format.
Example:
public class Movie {
private String name;
private String language;
public Movie(String name, String language) {
this.name = name;
this.language = language;
}
@Override
public String toString() {
return "Movie [name=" + name + ", language=" + language + "]";
}
}
getClass
()
: This method is used to get the class name of an object. The getClass
method is commonly used to check the class of an object at runtime. This method cannot be overridden as its declared as final
.
Example:
public class Movie {
public static void main(String[] args) {
System.out.println(""+(new Movie()).getClass());//class name of Movie
}
}
java.lang.Object class wait () and notify() methods:
wait()
: This method is used to make a thread to wait until another thread gives a signal to resume its processing.
- It should write inside a
synchronized
block or method otherwise causes aIllegalMonitorStateException
. - Will help to provide a solution for famous dead lock problem of threads.
wait()
Method throwsInterruptedException
- This method should use with
notify()
ornotifyAll
()
.
notify
method is used to resume or give a signal to any of one thread is waiting, notifyAll
method is used to resume or give a signal to all the threads which are waiting.
Example:
public class WaitThread implements Runnable {
private final Object lock;
public WaitThread(Object lock) {
this.lock = lock;
}
@Override
public void run() {
synchronized (lock) {
try {
System.out.println(Thread.currentThread().getName() + " is waiting");
lock.wait();
System.out.println(Thread.currentThread().getName() + " is awake");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.notify();
}
}
}
public static void main(String[] args) throws InterruptedException {
Object lock = new Object();
Thread t1 = new Thread(new WaitThread(lock), "Thread:1");
Thread t2 = new Thread(new WaitThread(lock), "Thread:2");
t1.start();
t2.start();
Thread.sleep(1000);
synchronized (lock) {
lock.notify();
}
}
}
Output:
Thread:1 is waiting
Thread:2 is waiting
Thread:1 is awake
Thread:2 is awake
Explanation:
- Created two threads with the names Thread:1 and Thread:2.
- Passed single object as a constructor argument for two threads.
- In the run method, wait method is invoked over an Object class object.
- Thread which first enters run method invokes wait method so that other will enter run method, now both the threads are in waiting state.
- Main thread goes for a sleep of 1s and calls notify method, among two threads(Thread:1 and Thread:2) one will get a signal and resumes processing after completion in finally block it gives signal to the other thread and this also starts resuming.
Note: In the above example, we can use notifyAll in main thread itself, can be removed notify in final block of run method as shown below.
Object lock = new Object();
Thread t1 = new Thread(new WaitThread(lock), "Thread:1");
Thread t2 = new Thread(new WaitThread(lock), "Thread:2");
t1.start();
t2.start();
Thread.sleep(1000);
synchronized (lock) {
lock.notifyAll();
}
Conclusion:
In this tutorial, we have covered java.lang.Object
class and its methods along with working examples of each method associated with it.