This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes difference between == and equals() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "difference between == and equals()" Watch "difference between == and equals()" New topic

difference between == and equals()

paresh doshi
Ranch Hand

Joined: Nov 20, 2006
Posts: 81
The following code will give

1: Byte b1 = new Byte("127");
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

Thanks.<br />Paresh Doshi<br />SCJP 1.4
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11883

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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Javier Sanchez Cerrillo
Ranch Hand

Joined: Aug 02, 2006
Posts: 152
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:


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.

SCJP 5.0 95%<br /> <br />The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge.
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

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

Joined: Dec 28, 2006
Posts: 893
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.

Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11883

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

Joined: Aug 31, 2006
Posts: 355

Wow Fred!
Thanks for your help. That was simple, straightforward and easy to remember.
paresh doshi
Ranch Hand

Joined: Nov 20, 2006
Posts: 81
thanks a lot for all your help and instant replies. Now i am clear with the concept of == and equals().

Thanks again.
I agree. Here's the link:
subject: difference between == and equals()
jQuery in Action, 3rd edition