String problem.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    String problem.

    Plz guys look at this code carefully.

    [code=java]
    String a = "abc";
    String b = "abc";
    String c = new String("abc");
    System.out.prin tln(a == b);
    System.out.prin tln(a == c);

    //and the output is true and false.

    [/code]
    why not the output is false and false?
    plz explain.

    kind regards.
    dmjpro.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by dmjpro
    Plz guys look at this code carefully.

    [code=java]
    String a = "abc";
    String b = "abc";
    String c = new String("abc");
    System.out.prin tln(a == b);
    System.out.prin tln(a == c);

    //and the output is true and false.

    [/code]
    why not the output is false and false?
    plz explain.

    kind regards.
    dmjpro.
    Think references and remember the String pool.
    In
    [CODE=java] String b = "abc";[/CODE] no new oject is created because the literal "abc" already exists in the String pool so the reference b is merely made to point to the same memory that a is pointing to. In

    [CODE=java]String c = new String("abc");[/CODE] The new keyword forces a new object to be created in memory disregarding what is in the String pool.

    Comment

    • sumittyagi
      Recognized Expert New Member
      • Mar 2007
      • 202

      #3
      Originally posted by dmjpro
      Plz guys look at this code carefully.

      [code=java]
      String a = "abc";
      String b = "abc";
      String c = new String("abc");
      System.out.prin tln(a == b);
      System.out.prin tln(a == c);

      //and the output is true and false.

      [/code]
      why not the output is false and false?
      plz explain.

      kind regards.
      dmjpro.
      This is due to string pool.
      whenever you define a string, that string is dropped in a pool of strings. When you define another string with same char sequence, then new string object is not created, the same object kept in the pool is returned.

      But if you create a string object by using new keyword, then java have no option other than creating new object as you directed it to do so.

      That's why first two references are same but third one is not.

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        I came to know a good thing .

        thanks for ur suggestions.

        kind regards.
        dmjpro.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Also have a look at the String.intern() method; this method adds a String to
          the String pool.

          kind regards,

          Jos

          Comment

          • dmjpro
            Top Contributor
            • Jan 2007
            • 2476

            #6
            That means all time the String Object is not pooled every time.
            right???

            so the first print will not be true every time.
            right???

            if not then why String.intern() ?????
            plz help.

            kind regards.
            dmjpro.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by dmjpro
              That means all time the String Object is not pooled every time.
              right???

              so the first print will not be true every time.
              right???

              if not then why String.intern() ?????
              plz help.

              kind regards.
              dmjpro.
              String literals such as "abc" are always pooled and the following will always be true:
              [code=java]
              String a= "abc";
              String b= "abc"
              System.out.prin tln(a == b); // true
              [/code]

              The following will always be false:
              [code=java]
              String c= new String("abc");
              System.out.prin tln(a == c || b == c); // false
              [/code]
              And the following will be true again:
              [code=java]
              c= c.intern();
              System.out.prin tln(a == c && b == c); // true
              [/code]

              kind regards,

              Jos

              Comment

              • dmjpro
                Top Contributor
                • Jan 2007
                • 2476

                #8
                Now the picture comes more clearer.
                Thanks for ur suggestions.

                kind regards.
                dmjpro.

                Comment

                Working...