Diff b/w String s="hello" and String s=new String("hello");

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rahana
    New Member
    • Oct 2007
    • 12

    #1

    Diff b/w String s="hello" and String s=new String("hello");

    hello friends,

    i have a doubt,what is the difference between

    String s="Hello" and
    String s=new String("Hello")

    please help me.


    Thanking u
    Rahana Parveen
  • heat84
    New Member
    • Nov 2007
    • 118

    #2
    Its the same.
    Only one String object is shared by all string having same character sequence.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by heat84
      Its the same.
      Only one String object is shared by all string having same character sequence.
      This is definitely not true. The String s= "hello" initialization statement creates
      one single interned String while String s = new String("hello") creates two String
      objects: one interned the other one on the heap.

      kind regards,

      Jos

      Comment

      • heat84
        New Member
        • Nov 2007
        • 118

        #4
        Originally posted by JosAH
        This is definitely not true. The String s= "hello" initialization statement creates
        one single interned String while String s = new String("hello") creates two String
        objects: one interned the other one on the heap.

        kind regards,

        Jos
        Whats contained in an objects is the same but the references are the different i.e if you invoke s1.equals(s2) it returns true but s1==s2 returns false . Thats what I mearnt when I said they are the same.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by heat84
          Whats contained in an objects is the same but the references are the different i.e if you invoke s1.equals(s2) it returns true but s1==s2 returns false . Thats what I mearnt when I said they are the same.
          I understand what you mean but 'same', 'equal', 'equivalent' and 'identical' have
          very precise meanings. Be a bit sloppy with their meaning and it will rear its ugly
          head one sad day.

          kind regards,

          Jos

          Comment

          • heat84
            New Member
            • Nov 2007
            • 118

            #6
            Originally posted by JosAH
            This is definitely not true. The String s= "hello" initialization statement creates
            one single interned String while String s = new String("hello") creates two String
            objects: one interned the other one on the heap.

            kind regards,

            Jos
            At compile time , String s="hello" and String s = new String("hello") are the same in the sense that the strings will be in the string pool.In this case only one String object is shared by all string having same character sequence. However , at runtime, an object is created in the program space .

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by heat84
              At compile time , String s="hello" and String s = new String("hello") are the same in the sense that the strings will be in the string pool.In this case only one String object is shared by all string having same character sequence. However , at runtime, an object is created in the program space .
              There is no string pool at compile time; javac just puts the string literal "hello"
              in the .class file when it generates code for it. String s= "hello" generates
              different byte code from what String s= new String("hello") does; check it with javap.

              The two code fragments are definitely different. If all you want to see is "hello",
              yes, then they're identical.

              kind regards,

              Jos

              Comment

              • itsraghz
                New Member
                • Mar 2007
                • 124

                #8
                Yes, JosAh is absolutely correct.

                They both are very much different! His explanations are appropriate.

                Comment

                • heat84
                  New Member
                  • Nov 2007
                  • 118

                  #9
                  Originally posted by JosAH
                  There is no string pool at compile time; javac just puts the string literal "hello"
                  in the .class file when it generates code for it. String s= "hello" generates
                  different byte code from what String s= new String("hello") does; check it with javap.

                  The two code fragments are definitely different. If all you want to see is "hello",
                  yes, then they're identical.

                  kind regards,

                  Jos
                  Thanks , I got some enlightening .

                  Comment

                  Working...