aspose file tools*
The moose likes Beginning Java and the fly likes == and .equals() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "== and .equals()" Watch "== and .equals()" New topic
Author

== and .equals()

Jimmy Bonds
Ranch Hand

Joined: Jun 05, 2001
Posts: 38
Hi All
String s1 = "String";
String s2 = "String";
boolean test1 = s1==s2;
if(test1==true)
{
System.out.println("test1 works");
}
boolean test2 = s1.equals(s2);
if(test2==true)
{
System.out.println("test2 works");
}
//END
Both lines will print. My question is
What is the main difference between using == and .equals()?
Or is it just 2 different ways to do the same thing?
Thanks
Jimmy-
Mikael Jonasson
Ranch Hand

Joined: May 16, 2001
Posts: 158
The == operator only compares if the reference is equal, that is if it is the same string. equals() compars to se if the two strings are identical. The reason that the == in your example works is because Java saves memory by creating a string and place it in memory the first time you use a constant (the "String" in your example) and then make all other variable-references to constants that are identical point to the same reference.
if you change your code to the following you'll see the difference:

Hope that cleared up your confusion
/Mike
manish paliwal
Greenhorn

Joined: May 18, 2001
Posts: 16
In adding to mickal's answer ,
'==' operator is search for the address.But equals() method search for the value at particular address.
Remember that String variable's value can not be changed.
------------------
manish paliwal(or just mann )


manish paliwal(or just mann )
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

You could also use the new operator to ensure that each string variable gets it's own instance.
fred cook
Greenhorn

Joined: Jun 13, 2001
Posts: 8
Does the '==' operator always test if two addresses are the same, or does it just test that when you are comparing pointers. Also, how could two addresses be the same anyway? Wouldn't you always have to use the 'new' operator?
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

fred,

I can tell you are from a C background.

Thinking about pointers when you think about Java can lead you down a trail of tears.

When someone codes String constants in their code (a string constant is anything between the " marks), then the java runtime will produce a single instance of that string in memory. Since Strings are immutable, this is a safe thing to do. So if you do your assignments like this:
This will produce two trues.

a==b Always compares if the objects are the same object. This is true in this case, because both a and b are 'referring' (or if you must 'pointing') to the same single instance of that string constant "Hello" that your runtime has set aside in memory. (They contain the same address). No new string needs to be constructed (the new keyword is unnecessary) because this String constant is already there to be used.

a.equals(b) is also true because "Hello" does indeed equal "Hello" (ie: the words are the same)

If, though, you do this:
This will produce false, then true.

When you use new keyword you are explicity telling the runtime to create a separate String object. You are using the following constructor String(String s). The runtime uses the previously set aside string constant "Hello", to produce a second and *Different* string object, also containing the word "Hello". It does this again with the b String.

So when you compare a==b -> These are two different object references with two different addresses in them. So they do not point to the same object.

But of course, "Hello" is still the same word as "Hello", so the a.equals(b) still produces true.

So as an interesting aside... all other things being equal (programming requirements don't force you to make new strings each time).. the first code uses about 1/3 the resources that the second code does. In the first code, only one String object is made, (which two references 'point' to), and in the second code, there are three String objects.

Apologies for not always saying uppercase String when I should have.
Mikael Jonasson
Ranch Hand

Joined: May 16, 2001
Posts: 158
Or to put it in another way:
Java has claimed to have removed the 'pointers' that a lot of C/C++-programmers dreaded. Unfortunatly, their way of removing it was to make everything work as pointers, but making it in a way so you don't see it.
The == operator always compares the value of the variables used in the comparison thus:

will work just fine, since the value stored in the variable itself. When it comes to references, like the string in the posts above, what is stored in the variable is a memory adress for where the string is stored. Thus the == still compares the value of the variable, but will only return true if the values are the same, i.e. they contain the same adress (is the same string, not identical strings).
ok?
/Mike
Jimmy Bonds
Ranch Hand

Joined: Jun 05, 2001
Posts: 38
Thanks for all the help
Jimmy-
----------------
yah, yah I'm a newbie
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: == and .equals()
 
Similar Threads
StringBuffer var as an argument
equals ?????
Help please
object Reference Equality
equals() and ==