• 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

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following :

String s1 = "hello";
String s2 = "lo";

System.out.println(s1 == ("hel"+"lo")); // true
System.out.println(s1 == ("hel"+s2)); // false

When + operator is used, internally a string buffer is used and a new string is returned. So why is this strange behavior...i mean since s2=="lo" both should be true. Or if a new string is being returned both should be false.why are they different?
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s1 = "hello";//1
String s2 = "lo";//2
System.out.println(s1 == ("hel"+"lo")); // true//3
System.out.println(s1 == ("hel"+s2)); // false//4
1.creat new string object in pool.
2.creat new string object in pool.
"hel"+"lo"=hello hello is already in pool so it point to
the same object,
"hel"+s2=hello but create a new object because strings are
immutable so it is false.
 
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason is in second case, ("hel"+s2), s2 is not a constant. So, it creates a new String object although the same is available within the pool.
HTH,
Paul.
------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus
 
rajani peddi
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul Anil ,

can you be more clear on this. you are saying that s2 is not a constant and hence a new string is being created. But s2="lo" which is in the pool. so will s2 not be replaced by "lo" and the effect be the same in both?
rajani
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi rajani,
check the post made by Tom Yang today for the explanation of mostt of string related problems
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you create a String using new of when the String is the result of a runtime operation java creates a new object. In the first case "hel"+"lo" the string literal is decided at compile time. In the second case "hel"+s2 a new String object is created at runtime.
-Aj
 
reply
    Bookmark Topic Watch Topic
  • New Topic