ObjectClass: The object class is the parent class of all the classes in java by default
Its methods
1 toString :
it returns a string representation of an object. It is used to convert an Object "to
String".
import java.util.*;
public class Main1 {
int car_no;
Main1(int car_no){
this.car_no=car_no;
public String toString() {
return car_no + " ";
public static void main(String args[]){
Main1 s = new Main1(16);
System.out.println(s.toString());
System.out.println(s);
2 hashCode() Method
A hash code is an integer value that gets generated by the hashing algorithm.
Hash code is associated with each object in Java and is a distinct value. It converts
an object's internal address to an integer through an algorithm. It is not the
memory address, it is the integer representation of the memory address.
import java.util.*;
public class Main {
int car_no;
Main(int car_no){
this.car_no=car_no;
}
public int hashCode() {
return car_no;
public static void main(String args[])
Main s = new Main(16);
Main ss = new Main(17);
System.out.println(s.hashCode());
System.out.println(ss.hashCode());
3 getClass() Method
It is used to return the class object of this object. Also, it fetches the actual runtime class of
the object on which the method is called.
import java.util.*;
public class Main {
public static void main(String[] args)
Object s = new String("Hi");
Object i= new Integer(19);
Class c = s.getClass();
Class d= i.getClass();
//for the String
System.out.println("Class of Object s is : " + c.getName());
//for the integer
System.out.println("Class of Object i is : " + d.getName());
clone() Method
The clone() method is used to create an exact copy of this object. It creates a new object and
copies all the data of the this object to the new object.
import java.util.*;
class Main implements Cloneable {
// declare variables
String name;
int age;
public static void main(String[] args) {
// create an object of Main class
Main obj1 = new Main();
// initialize name and age using obj1
obj1.name = "xyz";
obj1.age = 19;
// print variable
System.out.print(obj1.name); // xyz
System.out.println(" "+ obj1.age); // 19
try {
// create clone of obj1
Main obj2 = (Main)obj1.clone();
System.out.print(obj2.name); // xyz
System.out.println(" "+ obj2.age); // 19
catch (Exception e) {
System.out.println(e);
notify() Method
The notify() method is used to wake up only one single thread that is waiting on the object,
and that thread starts the execution
import java.util.*;
class Notify1 extends Thread
public void run()
// synchronized block ensures only one thread
// is running at a time.
synchronized(this)
System.out.println("It is the starting of " + Thread.currentThread().getName());
try {
this.wait();
catch (InterruptedException e) {
e.printStackTrace();}
System.out.println(Thread.currentThread().getName() + " is notified");
class Notify2 extends Thread {
Notify1 notify1;
Notify2(Notify1 notify1)
this.notify1 = notify1;
}
public void run()
synchronized(this.notify1)
System.out.println("It is the starting of " + Thread.currentThread().getName());
try {
this.notify1.wait();
catch (InterruptedException e) {
e.printStackTrace();
System.out.println(Thread.currentThread().getName() + " is notified");
class Notify3 extends Thread
Notify1 notify1;
Notify3(Notify1 notify1)
this.notify1 = notify1;
public void run()
{
synchronized(this.notify1)
System.out.println("It is the starting of " + Thread.currentThread().getName());
// call the notify() method
this.notify1.notify();
System.out.println(Thread.currentThread().getName() + " is notified");
public class Main
public static void main(String[] args) throws InterruptedException
Notify1 notify1 = new Notify1();
Notify2 notify2 = new Notify2(notify1);
Notify3 notify3 = new Notify3(notify1);
// creating the threads
Thread t1 = new Thread(notify1, "Thread-1");
Thread t2 = new Thread(notify2, "Thread-2");
Thread t3 = new Thread(notify3, "Thread-3");
// call run() method
t1.start();
t2.start();
Thread.sleep(100);
t3.start();
notifyAll() Method
The notifyAll() method is used to wake up all threads that are waiting on this object. This
method gives the notification to all waiting threads of an object.
import java.util.*;
class Notify1 extends Thread
public void run()
synchronized(this)
System.out.println("It is the starting of " + Thread.currentThread().getName());
try {
this.wait();
catch (InterruptedException e) {
e.printStackTrace();}
System.out.println(Thread.currentThread().getName() + " is notified");
class Notify2 extends Thread {
Notify1 notify1;
Notify2(Notify1 notify1)
this.notify1 = notify1;
public void run()
synchronized(this.notify1)
System.out.println("It is the starting of " + Thread.currentThread().getName());
try {
this.notify1.wait();
catch (InterruptedException e) {
e.printStackTrace();
System.out.println(Thread.currentThread().getName() + " is notified");
class Notify3 extends Thread
Notify1 notify1;
Notify3(Notify1 notify1)
this.notify1 = notify1;
}
public void run()
synchronized(this.notify1)
System.out.println("It is the starting of " + Thread.currentThread().getName());
// call the notifyAll() method
this.notify1.notifyAll();
System.out.println(Thread.currentThread().getName() + " is notified");
public class Main
public static void main(String[] args) throws InterruptedException
Notify1 notify1 = new Notify1();
Notify2 notify2 = new Notify2(notify1);
Notify3 notify3 = new Notify3(notify1);
// creating the threads
Thread t1 = new Thread(notify1, "Thread-1");
Thread t2 = new Thread(notify2, "Thread-2");
Thread t3 = new Thread(notify3, "Thread-3");
// call run() method
t1.start();
t2.start();
Thread.sleep(100);
t3.start();
wait() Method
The wait() method tells the current thread to give up the lock and go to sleep. It happens until
some different thread enters the same monitor and calls the methods
equals()
method to check if two objects obj1 and obj2 are equal.
class Main {
public static void main(String[] args) {
// equals() with String objects
// create objects of string
String obj1 = new String();
String obj2 = new String();
// check if obj1 and obj2 are equal
System.out.println(obj1.equals(obj2)); // true
// assign values to objects
obj1 = "Java Programming";
obj2 = "Python Programming";
System.out.println(obj1.equals(obj2)); // false