0% found this document useful (0 votes)
75 views15 pages

Java Lang Object PDF

The document discusses key methods in the Object class in Java. It covers: 1. The toString() method which returns a string representation of an object. It is overridden to print the object's state rather than its reference. 2. The equals() method which compares two objects by reference by default but can be overridden to compare objects' states. 3. The hashCode() method which returns a unique integer for each object by default based on the object's reference. If equals() is overridden to compare state, hashCode() should also be overridden to generate a hash based on state.

Uploaded by

NANDINI B
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
75 views15 pages

Java Lang Object PDF

The document discusses key methods in the Object class in Java. It covers: 1. The toString() method which returns a string representation of an object. It is overridden to print the object's state rather than its reference. 2. The equals() method which compares two objects by reference by default but can be overridden to compare objects' states. 3. The hashCode() method which returns a unique integer for each object by default based on the object's reference. If equals() is overridden to compare state, hashCode() should also be overridden to generate a hash based on state.

Uploaded by

NANDINI B
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 15

Pg1

Monday, April 27, 2020 8:10 AM

java.lang.Object
➢ Object class is defined in java.lang package.
➢ Object class is the super most class for all the classes in java.
➢ In Object class there are 11 non-static methods.

non-static methods m of java.lang.Object class :

1 public String toString()


2 public boolean equals(Object o )
3. public int hashCode()
4. protected Object clone() throws CloneNotSupportedException
5. protected void finalize() ---> deprecated
6. final public void wait() throws InteruptedException
7 final public void wait( long ) throws InteruptedException
8. final public void wait(long , int) throws InteruptedException
9. final public void notify() throws InteruptedException
10. final public void notifyAll() throws InteruptedException
11. final class getClass()

java.lang.Object Page 1
Pg2
Monday, April 27, 2020 8:32 AM

1. public String toString() :

➢ toString() returns String.


➢ toString() implementation of Object class returns reference of an object in
String format.
➢ toString() of Object class is implicitly called every time, when a programmer
tries to print the reference of an object.

To Override toString() :

purpose:
➢ we override toString() to print state of an object instead of
reference of the object.

java.lang.Object Page 2
for example refer, workspace/object_class/src/pack1/Demo3

Task1 :

java.lang.Object Page 3
Pg3
Tuesday, April 28, 2020 8:13 AM

2. public boolean equals(Object o ) :

➢ the return type of equals method is boolean.


➢ For equals method we can pass reference of any object.
➢ The java.lang.Object class implementation of equals method is used to
compare the reference of two objects. ( it behaves exactly like == operator)

Example :
class Book
{
String title;
Book(String title) {
this.title = title ;
}
}

java.lang.Object Page 4
refer,
workspace/object_class/pack2/
BookDriver1.java
refer,
workspace/object_class/pack2/
BookDriver2.java

Note:

➢ == operator compares reference,


□ if it returns true, it is reference of same object( 1 object ) case1
□ if it returns false, it is reference of different objects( 2 objects) case2
➢ java.lang.Object class design of equals(Object ) is very much similar to
operator == ( as seen in case1 & case2 )

java.lang.Object Page 5
Pg4
Wednesday, April 29, 2020 8:29 AM

java.lang.Object Page 6
java.lang.Object Page 7
To Override equals(object ) :
java.lang.Object Page 8
To Override equals(object ) :

Purpose: We override equals method to compare the state of two objects instead
of comparing reference of two objects.

design tip : ( in equals method compare the state of current(this) object with
passed object by downcasting it )

example refer, workspace/object_class/src/pack2/Laptop.java

Note :
1. if equals method is not overridden it compares the reference of 2 objects
similar to == operator.
2. if equals method is overridden in a class, then it compares the state of
objects and not the reference. in such case if we have to compare the
reference it is possible only with the help of == operator.

Assignment1 :

1. design the class for Book,


2. user should be able to display the state easily
3. user should be able to compare 2 books easily
4. implement your design

Assignment2:

java.lang.Object Page 9
Assignment2:

1. design the class for employee hierarchy,


2. user should be able to display the state easily
3. user should be able to compare 2 employees easily
4. implement your design

java.lang.Object Page 10
Pg5
Friday, May 1, 2020 8:12 AM

3. public int hashCode():

➢ hashCode() returns integer number


➢ java.lang.Object class implementation of hashCode() returns a unique integer
number for every object created.
➢ The unique integer number is generated based on the reference of the object

example:
class Mango
{
int price ;
Mango(int price) {
this.price = price ;
}
}

java.lang.Object Page 11
Note :
1. by observing case1 & case2 it is clear that according to java.lang.Object class
implementation of hashCode() and equals() :

 The hashCode() of two objects is different if equals() method returns


false for them.
 The hashCode() of two objects is same if equals() method returns true
from them

example2 :

class Mango
java.lang.Object Page 12
class Mango
{
int price ;
Mango( int price ) {
this.price = price ;
}
// override equals method

public boolean equals( Object obj ) {


return this.price == ((Mango)obj).price ;
}
}

java.lang.Object Page 13
Note:

java.lang.Object Page 14
➢ From the above observation it is clear that, if equals() method is overridden, it is
strongly recommended to override hashCode() method also.
➢ If equals() method compares the state of an object, hashCode() should be designed
such that it generates integer based on state of an object.

For examples refer, workspace/object_class/src/pack3

java.lang.Object Page 15

You might also like