Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between == and equals()

 
paresh doshi
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code will give

1: Byte b1 = new Byte("127");
2:
3: if(b1.toString() == b1.toString())
4: System.out.println("True");
5: else
6: System.out.println("False");

A) Compilation error, toString() is not avialable for Byte.
B) Prints "True".
C) Prints "False".

The answer is False. Can anyone please explain why it is False. I am confused in the concept of == and equals(). Please help
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Search this forum, and you will find many topics where this has been discussed.

in a nutshell, equals() is a method someone (often YOU) implements to do the comparison you want 99.999% of the time.

== compares the references to see if they point to the same object in memory.

when you call "toString()", you are creating a new object. since you call toString twice, you have two distinct String objects, both with the value of "127". so, == returns false, but .equals() would return true.
 
Javier Sanchez Cerrillo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use the "==" operator, you are asking if the references are the same, look at this snippet:

String pepe = new String("Pepe");
String pepe2 = new String("Pepe");

if you ask

pepe == pepe2?

the result is false

Although the Strings have the same contents, they are not the same String.

However if you call the method:

pepe.equals(pepe2);

the result is true

They are "meaningful equal".


---------------------------------------------------------------

There is one last thing you need to know about this. "The String constant pool". You can search in the forum for it.
 
Faisal Ahmad
Ranch Hand
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very good question.
Before explaining why, let me remind you, that was not the proper way to post your code! Be sure to use code tags while posting code. Ok..that said..let us dive into our question..
1. Not just Byte b1 = new Byte("127"); even for new Byte("0") it gives false.
2. Why? Simple! b1.toString() OR toString() methods always throw a String, right? And, when you are calling b1.toString() twice, that means two String objects were created. The == operator checks whether the bits are equal. When comparing objects, == actually compares the references and not what's inside the objects! In your code example, you were checking whether two String objects created were ==(means, referring to same object). Since they are not referring to same object, the comparison resulted in false.
3. To actually check if two objects are equal, use equals()!

Now, I have one question:
Normally, Strings are placed on String pool. JVM doesn't creates new Strings but simply refers to the existing ones on the pool. Now, considering the above example code, there should actually be only one String object created right? In that case, the if test should result in true, yeah? Please clarify.
Thanks in advance!
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had expected True, because normally when a string is created it will reference to the "String constant pool".

I have learnt from this that the toString() function always create a new String object in the normal memory.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A string will refer to the constant pool if you do it like this:


however, if you do this:

you will get a "Fred" in the string pool, but you will ALSO get an object created, and myString will NOT refer to the one in the pool.
 
Faisal Ahmad
Ranch Hand
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow Fred!
Thanks for your help. That was simple, straightforward and easy to remember.
 
paresh doshi
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot for all your help and instant replies. Now i am clear with the concept of == and equals().

Thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic