This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach 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 Murach's Java Servlets and JSP this week in the Servlets 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
Author

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

Sarah Marshall
Greenhorn

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")* ?

hth
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37941
    
  22
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: 477
http://blog.taragana.com/index.php/archive/how-to-compare-strings-with/
Sarah Marshall
Greenhorn

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: http://aspose.com/file-tools
 
subject: I compared two Strings with == and it worked????
 
Similar Threads
javax.awt.event does not exist!!!
NullPointerException occured
JButton button
unable to clear text box in Multi Screen JFrame GUI Application
Problem with custom painting and multiple JPanel