• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String with == operator

 
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String str1 = "a";
String str2 = new String("a");
String str3 = "a";
Why str1 == str2 return false but str1 == str3 return true?
 
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
str1 and str3 are pointing to the same object reference to save memory usage, while str2 is with the new object instance pointing to a new String with value "a"...
Since str1 and str3 are pointing on the same object reference, they result equal... while another does not...
Hope it is clear....
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why str1 == str2 return false but str1 == str3 return true?
What were you expecting instead?
As Ko Ko Naing mentioned, str1 and str3 refer to the same String object. At compile time, since Strings are immutable (cannot be changed once created) Java is able to safely arrange a few special things to allow two different String references to actually refer to the same String object when String literals are used. The double equals operator in Java, ==, is used to test whether two variables refer to the same value. In this case, they do.
Java allows you to specify that no matter if other instances of some String exist, by creating a new String instance with new String("my string") a new String object will be created at runtime, even though it has the same internal value as some String literal. So, str1 is referring to the String object specified at compile time as a String literal, while str2 is referring to a different String object, and so comparing the two references with == results in false since they aren't referring to the same String object.
Now, str1.equals(str2) would be true, since the two different String objects referred to by str1 and str2 actually represent equal values.
Is this getting any clearer?
 
Surasak Leenapongpanit
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dirk Schreckmann:
[i]
Now, str1.equals(str2) would be true, since the two different String objects referred to by str1 and str2 actually represent equal values.


For addition, I just want to conclude that...
 
reply
    Bookmark Topic Watch Topic
  • New Topic