1 2 Previous Next 19 Replies Latest reply: Nov 27, 2008 8:19 AM by masijade RSS

    Regarding String....

    843785
      Hi,

      I have a few doubts here.
      1)What happens when we say
      String a ="abc"; ( I think a new String object is created )

      2)In the next if i say
      String b="abc"; ( I think one more String object is created).

      3) Next if i say a==b? it is returning true.

      why is it so?
        • 1. Re: Regarding String....
          843785
          [http://forums.sun.com/thread.jspa?forumID=54&threadID=5293881]
          • 2. Re: Regarding String....
            800308
            phdk wrote:
            [http://forums.sun.com/thread.jspa?forumID=54&threadID=5293881]
            A good answer indeed. jeff.kudos++;
            • 3. Re: Regarding String....
              843785
              hey buddy thanks a lot.

              So what happens when i say

              String c = new String("abc");

              Does it check the constant pool?
              • 4. Re: Regarding String....
                800282
                vints wrote:
                hey buddy thanks a lot.

                So what happens when i say

                String c = new String("abc");

                Does it check the constant pool?
                Yes, and right after that, a new "abc" is added in the pool.
                String x = "abc", y = "def", z = "ghi";
                
                                                +---------+
                                                | POOL    |
                                                |         |
                                        +<-<-<-<-<- "abc" |
                                        |       |   "def" |
                String c = new String("abc");   |   "ghi" |
                            |                   |         |
                            +->->->->->->->->->->-> "abc" |
                                                +---------+
                So, as you can see, there is no need to use new String(...)
                • 5. Re: Regarding String....
                  798906
                  prometheuzz wrote:
                  Yes, and right after that, a new "abc" is added in the pool.
                  This is likely down to the vm implementation but what you will probably end up with is a reference to the reference in the string constant pool. The string contents would not be duplicated (performance thing)
                  • 6. Re: Regarding String....
                    masijade
                    No, the "new String" will create a new String with it's own character array and everything, and no part of that will be placed on the "pool". Only the original literal String will be a part of the "pool".
                    • 7. Re: Regarding String....
                      800282
                      Looks like I need to spend some time browsing the JLS... Or just refrain from answering these kind of questions!
                      ; )
                      • 8. Re: Regarding String....
                        798906
                        masijade. wrote:
                        No, the "new String" will create a new String with it's own character array and everything, and no part of that will be placed on the "pool". Only the original literal String will be a part of the "pool".
                        Are you saying you believe the contents would be duplicated? If so I am curious as to why and for what reason?
                        • 9. Re: Regarding String....
                          masijade
                          YoGee wrote:
                          masijade. wrote:
                          No, the "new String" will create a new String with it's own character array and everything, and no part of that will be placed on the "pool". Only the original literal String will be a part of the "pool".
                          Are you saying you believe the contents would be duplicated? If so I am curious as to why and for what reason?
                          Because the API docs say so, for one
                          String

                          public String(String original)

                          Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string. Unless an explicit copy of original is needed, use of this constructor is unnecessary since Strings are immutable.

                          Parameters:
                          original - A String
                          And the String literals that appear in the code are the only things to appear on the pool (unless you specifically put one there, and if I remember right, there are ways of doing that).
                          • 10. Re: Regarding String....
                            mlk
                            masijade. wrote:
                            No, the "new String" will create a new String with it's own character array and everything,
                            Nope. If the String is not a substring(1), then they both use the same array (why not, both are immutable). If the string is a substring then it will copy to a new char array.

                            In Suns 1.6 JVM anyway.


                            1) String.substring(...) piggy backs off the parent strings char array.
                            • 11. Re: Regarding String....
                              798906
                              masijade. wrote:
                              Because the API docs say so, for one
                              No they don't
                              • 12. Re: Regarding String....
                                masijade
                                mlk wrote:
                                masijade. wrote:
                                No, the "new String" will create a new String with it's own character array and everything,
                                Nope. If the String is not a substring(1), then they both use the same array (why not, both are immutable). If the string is a substring then it will copy to a new char array.

                                In Suns 1.6 JVM anyway.
                                Okay, looking at the source, if the original is longer than what the new String should be, a new char array is created, otherwise the same char array is used. The String itself, however, (and from the way I read your post you weren't refuting this) is still a new Object and not simply a reference to the String object in the pool.

                                >
                                1) String.substring(...) piggy backs off the parent strings char array.
                                And yes, substring always uses the same array as the original (I knew that one, but the wording for new String(String) suggests that it will create a new char array, when in reality, it only does in one specific situation).

                                Edit: I hadn't bothered to look at the source code for it, as it is a rather pedantic point anyway. It is still a new Object, even if the "primary" field is the same one that something else uses.
                                • 13. Re: Regarding String....
                                  masijade
                                  YoGee wrote:
                                  masijade. wrote:
                                  Because the API docs say so, for one
                                  No they don't
                                  Strictly worded what does
                                  the newly created string is a copy of the argument string
                                  mean to you?


                                  It is, at least, definately not "a reference to a reference" as you put it.

                                  Edit:

                                  And even
                                  newly created String object so that it represents the same sequence of characters as the argument
                                  seems to say it as "represents the same sequence of characters" is not the same thing as "references the same char array".
                                  • 14. Re: Regarding String....
                                    mlk
                                    masijade. wrote:
                                    The String itself, however, (and from the way I read your post you weren't refuting this)
                                    I don't think anyone is saying the String object would be the same. Just the backing char array.
                                    wording for new String(String) suggests that it will create a new char array
                                    That is not how I read the Java Doc posted above.
                                    1 2 Previous Next