Forums Register Login

Significant different between == and equals()?

+Pie Number of slices to send: Send
Significant different between == and equals()?
+Pie Number of slices to send: Send
"==" is just check the refrence i.e. two object point to same memory location or not

equal, not only check '==' its also check the content of object

more about equals, open the source code of String class.
+Pie Number of slices to send: Send
== checks whether two references point to a same object or not

equals method determine whether they are meaningfully equivalent.
+Pie Number of slices to send: Send



see this code if you not understand then tell i will explain you................
Ans will be
true true false true.
+Pie Number of slices to send: Send
After analyzing add two line more............



and see the output..............
+Pie Number of slices to send: Send
buddy check out this code..

String s1=new String("testing").intern();
String s2=new String("testing").intern();

System.out.println(s1==s2); //it will print TRUE

Hence in case of intern (first the string is searched for the corresponding string in the pool of string.. if the match is found then the corresponding reference is given to the object.. otherwise new object is created.. if i dont write intern there...
then it wont search in the pool. and will create a new object..
thats the wastage of memory.

if you write String s="testing" // then it will automatically call intern method (implicit call)

but not in the case of String s=new String("testing"); // you have to explicitly provide the intern method
+Pie Number of slices to send: Send
 

pankaj sharma newbie wrote:buddy check out this code..

String s1=new String("testing").intern();
String s2=new String("testing").intern();

System.out.println(s1==s2); //it will print TRUE

Hence in case of intern (first the string is searched for the corresponding string in the pool of string.. if the match is found then the corresponding reference is given to the object.. otherwise new object is created.. if i dont write intern there...
then it wont search in the pool. and will create a new object..
thats the wastage of memory.

if you write String s="testing" // then it will automatically call intern method (implicit call)

but not in the case of String s=new String("testing"); // you have to explicitly provide the intern method



Nice one Pankaj
+Pie Number of slices to send: Send
there are difference between String object and simple objects
BE CAREFUL abaut that.
do you understand me?
+Pie Number of slices to send: Send
 

Vasif Mustafayev wrote:there are difference between String object and simple objects
BE CAREFUL abaut that.
do you understand me?




YES..you are right
+Pie Number of slices to send: Send
Where have you read about the equals() method? You ought to look in the API documentation, in Bloch's Effective Java™ (you may be able to find a "sample chapter" from the 1st edition on the internet, which describes equals()), or Google for Angelika Langer Java equals hashCode. There is a lot of useful information there.
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote: Google for Angelika Langer Java equals hashCode.



That was extremely helpful, thanks!
This is my favorite tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1104 times.
Similar Threads
loop trouble
difference between equals() and == and compareTo()
confusion between == & equals( )
equals method .. comparing objects
unit test for java
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 02:58:44.