File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Java String pool problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java String pool problem " Watch "Java String pool problem " New topic
Author

Java String pool problem

Jayesh Pokar
Greenhorn

Joined: Apr 26, 2011
Posts: 10
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

Joined: Jun 04, 2007
Posts: 331
Hi Jayesh,

You should get true if you did

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

Check how equals method differs from ==

Vishwa


Blog
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jayesh Pokar
Greenhorn

Joined: Apr 26, 2011
Posts: 10
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

Joined: Oct 13, 2005
Posts: 36453
    
  15
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

    Joined: Oct 13, 2005
    Posts: 36453
        
      15
    Jayesh Pokar wrote: . . . . . .
    But now "abcd" is a compile-time constant.
    Jayesh Pokar
    Greenhorn

    Joined: Apr 26, 2011
    Posts: 10
    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

    Joined: Oct 13, 2005
    Posts: 36453
        
      15
    Try this article first, and see whether it helps you.
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Java String pool problem
     
    Similar Threads
    String Pool
    String Objects
    synchronized locker quesion
    Doubt in String class
    String question