• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

== and .equals()

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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-
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 )
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could also use the new operator to ensure that each string variable gets it's own instance.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all the help
Jimmy-
----------------
yah, yah I'm a newbie
 
Honk if you love justice! And honk twice for tiny ads!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic