• 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

strings

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
String hel="hel";
String lo="lo";
String s1="hello";
String s2="hello";
String s3="hel"+"lo";
String s4="hel".concat("lo");
String s5=s+lo;
System.out.println((s1==s2)+" "+(s1==s3)+" "+(s1==s4)+" "+(s1==s5));

in the above code why (s1==s3) true and why (s==s5) false ?
explain any one.i am confusing about strings provide me some link to that gives clarity on string.

thanks in advance
 
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where is String s defined?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
tirapa

JVM maintain Pool for String Class, In this Pool if JVM find same string then it will not create new object and point to same old object because of that you are getting true

So the references of both the string are equal, since the == compare references not value for s1==s3 will be true
 
tirapa reddy
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry i modified here
String s5=hel+lo;
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's because they are "string literals" -- means "compile time constants". they are treated as constants during the compile time itself. To be clear, those string literals are loaded when the class is loaded -- much before the runtime.

Whereas even though you have the String reference variables 'hel' and 'lo' contain the equivalent string as their content, they are the values of a variable, which is not a constant.

So the output is false in your case.


Taken from Strings section of JLS, second edition:

# Literal strings within the same class (�8) in the same package (�7) represent references to the same String object (�4.3.1).
# Literal strings within different classes in the same package represent references to the same String object.
# Literal strings within different classes in different packages likewise represent references to the same String object.
# Strings computed by constant expressions (�15.28) are computed at compile time and then treated as if they were literals.
# Strings computed at run time are newly created and therefore distinct.
# The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.



Hope this helps! :cheers:

For example, try chaging the variables 'hel' and 'lo' as final variables
[ April 08, 2008: Message edited by: Raghavan Muthu ]
 
The knights of nee want a shrubbery. And a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic