[Logo]
Forums Register Login
difference between == and equals()
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
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.
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.
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!
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.
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.
Wow Fred!
Thanks for your help. That was simple, straightforward and easy to remember.
thanks a lot for all your help and instant replies. Now i am clear with the concept of == and equals().

Thanks again.
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards



All times above are in ranch (not your local) time.
The current ranch time is
Nov 19, 2017 03:45:39.