Object Class
Object Class
Its methods
1 toString :
int car_no;
Main1(int car_no){
this.car_no=car_no;
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.*;
int car_no;
Main(int car_no){
this.car_no=car_no;
}
return car_no;
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.*;
Class c = s.getClass();
Class d= i.getClass();
//for the String
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.*;
// declare variables
String name;
int age;
obj1.name = "xyz";
obj1.age = 19;
// print variable
System.out.print(obj1.name); // xyz
try {
System.out.print(obj2.name); // xyz
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.*;
// is running at a time.
synchronized(this)
try {
this.wait();
catch (InterruptedException e) {
e.printStackTrace();}
Notify1 notify1;
Notify2(Notify1 notify1)
this.notify1 = notify1;
}
synchronized(this.notify1)
try {
this.notify1.wait();
catch (InterruptedException e) {
e.printStackTrace();
Notify1 notify1;
Notify3(Notify1 notify1)
this.notify1 = notify1;
{
synchronized(this.notify1)
this.notify1.notify();
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.*;
synchronized(this)
try {
this.wait();
catch (InterruptedException e) {
e.printStackTrace();}
Notify1 notify1;
Notify2(Notify1 notify1)
this.notify1 = notify1;
synchronized(this.notify1)
try {
this.notify1.wait();
catch (InterruptedException e) {
e.printStackTrace();
Notify1 notify1;
Notify3(Notify1 notify1)
this.notify1 = notify1;
}
synchronized(this.notify1)
this.notify1.notifyAll();
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()
class Main {
System.out.println(obj1.equals(obj2)); // true
System.out.println(obj1.equals(obj2)); // false