0% found this document useful (0 votes)
31 views1 page

Reference of The Object If They Point To The Same Location Then Two Objects Are Equals and It Is Defined in Object Class

The == operator compares the memory location or address of objects in the heap, while the equals() method compares the state or content of an object. By default, equals() works like == and checks if two objects point to the same memory location. The equals() method can be overridden to change the criteria for object equality. For String objects, string literals are interned and stored in a central location, so two literal strings with the same value will refer to the same object. But two String objects created with new have different states and locations even if they have the same value.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views1 page

Reference of The Object If They Point To The Same Location Then Two Objects Are Equals and It Is Defined in Object Class

The == operator compares the memory location or address of objects in the heap, while the equals() method compares the state or content of an object. By default, equals() works like == and checks if two objects point to the same memory location. The equals() method can be overridden to change the criteria for object equality. For String objects, string literals are interned and stored in a central location, so two literal strings with the same value will refer to the same object. But two String objects created with new have different states and locations even if they have the same value.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

== operator: compare to memory location or address of object in HEAP

Equal() method: compare to state or content of object.


o default implementation of equals() method work like == means it will check the memory
reference of the object if they point to the same location then two objects are equals and it is
defined in Object class

string
o

o
o
o
o
o
o
o

equal is defined in Object class. Equality operator is one type of


operator => compare both primitive and object. => cant change
behavior. But equal method can override the criteria for proper object
literal vs string object
string literal: is interned string object. Its value will be stored in central
place. That mean, whenever this value is used again, the JVM wont
create a new string but use reference of cached string
string object is created by new operator: it has state and location.
Eg. String s1 =abc
String s2 = new String(abc).
String s3 =abc.
S1==s2 // false.
S1==s3 // true. S1, s3 point to same location and have same value
S1.equal(s2) // true

You might also like