This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes I compared two Strings with == and it worked???? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I compared two Strings with == and it worked????" Watch "I compared two Strings with == and it worked????" New topic

I compared two Strings with == and it worked????

Sarah Marshall

Joined: Jul 12, 2010
Posts: 3
Hello everybody.

So i've been learning java for a few months and i quickly found out when you try to compare Strings with == it doesn't work, and that to compare objects you have to use the .equals() method.

However I have just started learning about GUIs and actionListeners and i wrote a program and accidentally tried to compare two strings with == but for some reason the program worked. Below i've wrote a simplified example out which demonstrates this.

So the getActionCommand() method returns a String. And when used on a button it returns a String of what is written on the button. Which on my program is "I am a button wahoo". The first if statement compares the String returned by getActionCommand() with "I am a button wahoo", and changes the text on the button.

And the text on the button does changed when i run the program. Can anyone tell me why in this case comparing the strings using == is working? Thanks in advance for any help.

Raj S Kumar
Ranch Hand

Joined: Aug 06, 2006
Posts: 48
Yes Sarah, it works sometimes.

The reason is to save memory String objects are stored in a pool like structure. In some cases, if the content is same, they might refer to the same object. In such cases, '==' operator would work. But, this can't be relied upon.

Its always better to use the equals().

Hope this helps.

Raj S Kumar
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

<edit> welcome to javaranch </edit>

*==* checks whether two references refer to the identical object or not
*equals* checks whether two object's value are same or not.

in your case strings are interned. you can search here/javaranch for *string constant pool*

Hint: can you try this in your example *event.getActionCommand() == new String("I am a button wahoo")* ?

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46429
Because you have written "I am a new button" in each of those places, the compiler can recognise those as identical Strings. The same String object is used for all; comparison with the == operator will return true. If, however, the compiler cannot recognise those Strings as identical, different String objects will be used an the == operator will return false.
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 482
Sarah Marshall

Joined: Jul 12, 2010
Posts: 3
Hi guys, thanks so much for the warm welcome and for taking the time to help. Your replies have all been very helpful; I understand now what was going on. Thanks again.
I agree. Here's the link:
subject: I compared two Strings with == and it worked????
It's not a secret anymore!