• 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
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Java String pool problem

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I run these program, I was expecting answer as true but it gave false?



String s1 and s2 both should refer to same address in pool having String as "abcde" right?
Can some please explain me the correct way of how it works?

Thanks in advance.
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jayesh,

You should get true if you did

System.out.println(s1.equals(s2));

Check how equals method differs from ==

Vishwa
 
Sheriff
Posts: 22207
117
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler doesn't know that the value of s is "abcd", so the new Strings are just that - newly created Strings. They aren't stored in the String pool.
If you would make s final then the compiler would see that the value of s will always be "abcd", and therefore can use the String literal "abcde" for both s1 and s2 and the result will be true.
 
Jayesh Pokar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vishwanath,

As per you

String s="abcd";
String s1="abcd";

System.out.println(s==s1);

This should also give me false right and only s.equals(s1) should give true?

I want to know both s1 and s2 in previously mentioned program contain same string then why two different references, both should refer to same reference and s1==s2 should give me true...
Please correct me....where I am going wrong...

Thanks
 
Marshal
Posts: 72913
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The String s is not a compile-time constant, because the compiler cannot tell whether it has changed between line 5 and line 6. It might appear obvious to us reading it, but it would be very awkward to program the compiler to recognise the absence of changes. So it cannot put s1 and s2 into the String pool. Two possibilities:
  • Label s final; it then becomes a compile-time constant.
  • Use the intern method: (s + "e").intern() which intructs the JVM to attempt to insert that String into the String pool.
  •  
    Campbell Ritchie
    Marshal
    Posts: 72913
    330
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Jayesh Pokar wrote: . . . . . .

    But now "abcd" is a compile-time constant.
     
    Jayesh Pokar
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for explanation...

    My Question is..
    1. So it means executing line s1=s+"e"; puts the value "abcde" into s1 and that s1 value will not go in String pool?
    2. How much String object is made after executing above line?
    3. As per said, that s is not compile time constant, that is fine but value abcde hold by s1 after execution of the above line, that should go in String pool because if now value of s will change so what does it matters?

    Thanks....



     
    Campbell Ritchie
    Marshal
    Posts: 72913
    330
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Try this article first, and see whether it helps you.
     
    You showed up just in time for the waffles! And this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic