8 Replies Latest reply: Dec 21, 2012 5:28 PM by EJP RSS

    confusion in  Stirng object

    980221
      my Question is , How Many object Has been created i of String in given below , i am confused so please reply the Explanation

      String S= "abc";

      String S1= new String ("abc");

      String S2= "abc";

      String S3= S1;
        • 1. Re: confusion in  Stirng object
          Kayaman
          This is a school homework question or similar, right?
          How many Strings do you think are created?
          • 2. Re: confusion in  Stirng object
            961708
            You need to understand simple concept, when a new object will be created in context to String..

            1- everytime a contructor is used by using new, it is true for any object of Java.
            2- even a string if created via String pool, and when it doesn't exists already in pool.

            So, there will be two objects in your example...

            1- String S= "abc"; first occurence of your codebase line, assuming "abc" already doesn't exists in pool.
            2- String S1= new String ("abc"); //This will always create new object doesn't matter if exists or not in the pool.

            And remaning two lines wouldn't impact the object creation, that is just a reference assignment for string pool and created object.

            hope, this helps you.
            • 3. Re: confusion in  Stirng object
              EJP
              You need to understand simple concept, when a new object will be created in context to String..

              1- everytime a contructor is used by using new, it is true for any object of Java.
              Correct.
              2- even a string if created via String pool, and when it doesn't exists already in pool.
              Meaningless. To the extent I can make sense of this, it is incorrect.
              So, there will be two objects in your example...
              There will indeed, but not for the reasons you have given.
              1- String S= "abc"; first occurence of your codebase line, assuming "abc" already doesn't exists in pool.
              But "abc" does exist in the string pool, by definition. All string literals are pooled. This line does not create a new object. It creates a reference to an existing object in the string pool.
              2- String S1= new String ("abc"); //This will always create new object doesn't matter if exists or not in the pool.
              Correct except that in this case once again "abc" does exist in the pool.
              And remaning two lines wouldn't impact the object creation, that is just a reference assignment for string pool and created object.
              Of the remaining two lines, the first is the same as the original first line, so it is a new reference to an existing object, and the second is also a new reference to an existing object.
              hope, this helps you.
              It won't because it is 50% incorrect.
              • 4. Re: confusion in  Stirng object
                aksarben
                Why does it matter how many string objects there are?
                • 5. Re: confusion in  Stirng object
                  gimbal2
                  aksarben wrote:
                  Why does it matter how many string objects there are?
                  When are school programming assignments ever about answering sensible questions?
                  • 6. Re: confusion in  Stirng object
                    EJP
                    1971. My first two assignments were a bubble sort and a heap sort, the latter to prove that the former is simple but useless. Never used a bubble sort since. Very useful bit of knowledge.

                    This perennial question about the strings doesn't even have the virtue of proving anything in particular, and as I said above it is questionable whether the people asking the question would know the correct answer when they see it.

                    It must be in a book somewhere: it's always exactly the same whenever asked.
                    • 7. Re: confusion in  Stirng object
                      gimbal2
                      EJP wrote:
                      1971. My first two assignments were a bubble sort and a heap sort, the latter to prove that the former is simple but useless. Never used a bubble sort since. Very useful bit of knowledge.
                      Not fair. Back then schools actually trained useful people. Nowadays even math is going out the door because it is "too hard".
                      • 8. Re: confusion in  Stirng object
                        EJP
                        First three courses were Fortran, numerical analysis, and Logical Organization (i.e. of the hardware).