aspose file tools*
The moose likes Java in General and the fly likes == and equal() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "== and equal()" Watch "== and equal()" New topic
Author

== and equal()

S Majumder
Ranch Hand

Joined: Jun 03, 2009
Posts: 244
Hi every body I have a query :

here is my code :




What will be the out put and why ?


thanks and regards,
S
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61657
    
  67

We are not your compiler. What happened when you tried it?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
Begri, as you have been told, you will know the answer by running that piece of code yourself. Regarding why, think in terms of object and literals of strings and keep in mind overriding concept of equals() method.
S Majumder
Ranch Hand

Joined: Jun 03, 2009
Posts: 244
Bear Bibeault wrote:We are not your compiler. What happened when you tried it?


Yes you are correct ,
I have run the code , but I am not able to understand the answer , that why I asked in the forum.

thanks,
S
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61657
    
  67

If you ran the code, post your results and explain what you expected and why. Don't just ask questions like "what will happen" when you already know.

So what about the results surprised you?
S Majumder
Ranch Hand

Joined: Jun 03, 2009
Posts: 244
Rahul.p Kumar wrote:Begri, as you have been told, you will know the answer by running that piece of code yourself. Regarding why, think in terms of object and literals of strings and keep in mind overriding concept of equals() method.


I run the code & it is giving true for:

if(s1==s2) and s1.equals(s2) both , why ?

for this if(s1.equals(s3)) it is also giving true , why ?

regards,
S
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
s begri wrote:

for this if(s1.equals(s3)) it is also giving true , why ?

regards,
S

Ok what happens for s1==s3, why? What is the diff between string literals and string objects? Have you thought from the angle of equals() ?
Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300


Begri, When you create string constant it is stored in a constant pool. In your case s1= "abc" and s2= "abc" both will refer to the same location. Hence when you say s1==s2 it means you are comparing the references and it shows true because referring to the same location.

When you say s3 = new("abc") and s4 = new("abc") it means a new reference is created and hence it does not show true when you say s3==s4( comparing references) but its contents (values) are same so it shows s3.equals(s4) true.

In short == is for comparing the references and equals is for comparing the values.
Hope this helps.

Regards
Patricia.
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Read what equals method is does.
Also read what == operator does.

Also read string object creation.
The strings have been created in two ways one using the new operator and one without it.
That where things differ a bit.

Its something to do with reference comparison and object content comparison.

That should give you the answers.

If still confused post reply explaining where you are stuck.


Priety.
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
Patricia Samuel wrote:

In short == is for comparing the references and equals is for comparing the values.



probably there is more to it. Some one may pick up a clue that equals() always compares the value. It is not the case. what is special about this equals(). So, what you do in overidden equals() to have this behaviour and why then you need to override hashcode() as well to do that? These all will be helpful to know for one's future classes.
Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
Rahul,

We do override equals method but the concept is still the same....comparing the values. What gets changed after overriding the equals method is the way we compare the values. The basic remains the same. Only the way gets changed.

Please correct me if i am wrong.

Regards
Patricia
S Majumder
Ranch Hand

Joined: Jun 03, 2009
Posts: 244
Thanks every body for your reply,
I am confusing why if(s1.equals(s2)) is giving true

and why if(s1.equals(s3)) is giving true.

regards,
S
Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
Is it clear to you now or not? Your message is ambiguous
Vivek Singh
Ranch Hand

Joined: Oct 27, 2009
Posts: 92
s begri wrote:Thanks every body for your reply,
I am confusing why if(s1.equals(s2)) is giving true

and why if(s1.equals(s3)) is giving true.

regards,
S

Check this also will be helpful:-


Output is:-
different reference
meaningfully equal
same object
meaningfully equal
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
Bear Bibeault wrote:We are not your compiler. What happened when you tried it?


This is a misunderstanding. Running a program can never answer a Java language question. Java is defined by the JLS and its API documentation, not by some observed behaviour.
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
s begri wrote:
I am confusing why if(s1.equals(s2)) is giving true
and why if(s1.equals(s3)) is giving true.


Don't be confused. Just read the documentation to the equals method of the String class (for example in the Sun API documentation). In short it states that equals returns true if the two Strings represents the same sequence of characters.

Are you confused about String literals? Then consult the JLS documentation. In summary it states that every String literal, like "abc" is represented by one unique String object. This means that each time "abc" appears in your code you can think of it as a reference to the same String object (the one which represents "abc").

Are you confused about the new operator? Then consult the JLS documentation. It states that each time new is used a new object is created.

String s1 = "abc";
String s2 = "abc";

Above both s1 and s2 are assigned the same reference, namely the reference of the unique String object representing "abc"

String s3 = new String("abc");
String s4 = new String("abc");

Above each of s3 and s4 is assigned a new String object reference. In both cases the String constructor is called with the same String reference, namely the one representing "abc".

The overall result is that s1 and s2 will hold the same reference. s3 and s4 will hold different references and they will not be the same as the reference in s1 and s2.

When you use equals of String it doesn't matter what reference the variables hold because equals compare String content. Whatever combination of s1, s2, s3 and s4 you use with equals the result will be true because all references point to objects with the same content.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: == and equal()