Confusion in String.intern()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sumittyagi
    Recognized Expert New Member
    • Mar 2007
    • 202

    #1

    Confusion in String.intern()

    Hi All!

    I am confused in the functioning of String.intern() function.
    [code=java]
    public class StringTest
    {
    public static void main(String[] args)
    {
    String test2 = new String("ABC");
    test2.intern();
    String test1 = "ABC";
    System.out.prin tln("test1 == test2 is: " + (test1==test2)) ;
    System.out.prin tln("test1 == test2 is: " + (test1==test2)) ;
    }
    }
    [/code]

    The output is:
    test1 == test2 is: false
    test1 == test2 is: false

    Now the definition of String.intern() function says:
    When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

    There is no String "ABC" in the pool when intern is called, so the same object is added to the pool; i.e. after call to intern, object referenced by test2 is in String pool. Now when test1 is created, then "ABC" exists in string pool so the same is returned from the pool and no new object is created.

    So that means test1 and test2 references to the same object, so I am not able to understand why this strange result is comming.

    Please guide me in this context.
    Thanks in advance for any help.
  • sumittyagi
    Recognized Expert New Member
    • Mar 2007
    • 202

    #2
    Also if I change my program like this:
    [code=java]
    public class StringTest
    {
    public static void main(String[] args)
    {
    String test2 = new String("ABC");
    test2 = test2.intern();
    String test1 = "ABC";
    System.out.prin tln("test1 == test2 is: " + (test1==test2)) ;
    System.out.prin tln("test1 == test2 is: " + (test1==test2)) ;
    }
    }
    [/code]

    Then result is fine:
    test1 == test2 is: true
    test1 == test2 is: true


    But test2.intern() will be returning the same object as referenced by test2 as there is no String "ABC" in the pool, so the same object is added to the pool and returned. Then I think there is no difference in both cases.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      When you do this:

      [code=java]
      String test2= new String("ABC");
      [/code]

      There will be *two* Strings:

      1) The literal String "ABC" which was in the pool already when the class was loaded;
      2) A new String with the content "ABC" (not interned).

      When you intern your test2 String, you should forget about that other String.
      You didn't do so in your first example, you forgot the interned String instead.

      In your current example test1 and test2 will both refer to the same (interned) String.

      kind regards,

      Jos

      Comment

      • sumittyagi
        Recognized Expert New Member
        • Mar 2007
        • 202

        #4
        Originally posted by JosAH
        When you do this:

        There will be *two* Strings:

        1) The literal String "ABC" which was in the pool already when the class was loaded;
        Ok!!!

        That means whenever we create a new string with new keyword, then there are actually two objects created!
        one for the parameter passed(which is a string object that gets added to the string pool) to the constructor and one for the new keyword, which is forcefully created.

        That's great!
        Thanks a lot Jos!

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by sumittyagi
          Ok!!!

          That means whenever we create a new string with new keyword, then there are actually two objects created!
          one for the parameter passed(which is a string object that gets added to the string pool) to the constructor and one for the new keyword, which is forcefully created.

          That's great!
          Thanks a lot Jos!
          You're welcome of course. So basically this is silly:

          [code=java]
          String foo= new String("bar");
          [/code]

          ... because the following would've done the same using one less object:

          [code=java]
          String foo= "bar";
          [/code]

          kind regards,

          Jos

          Comment

          Working...