File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java String pool problem

 
Jayesh Pokar
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Sheriff
Pie
Posts: 47232
52
  • 0
  • Mark post as helpful
  • send pies
  • 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
    Sheriff
    Pie
    Posts: 47232
    52
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jayesh Pokar wrote: . . . . . .
    But now "abcd" is a compile-time constant.
     
    Jayesh Pokar
    Greenhorn
    Posts: 10
    • 0
    • Mark post as helpful
    • send pies
    • 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
    Sheriff
    Pie
    Posts: 47232
    52
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Try this article first, and see whether it helps you.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic