1 2 Previous Next 19 Replies Latest reply: Feb 3, 2011 1:40 AM by tschodt RSS

    No. of String Objects ??

    836282
      class ExampleProgram {
      public static void main(String[] args){
      String text = new String("I'm a simple Program ");
      System.out.println(text);
      String text2 = text.concat(
      "that uses classes and objects");
      System.out.println(text2);
      }
      }





      How many Total String Objects are in this code ???? explain how if ans is 4.....
        • 1. Re: No. of String Objects ??
          802316
          Looks like four to me. I don't understand your question. Do you want to confirm 4 is correct or you can see a different number and don't know why it is 4?
          • 2. Re: No. of String Objects ??
            830868
            four:

            1: literal "I'm a simple Program " (created by literal)
            2: text variable holding "I'm a simple Program" (created by new String() construct)
            3: literal "that uses classes and objects" (created by literal)
            4: text2 variable holding: "I'm a simple Program that uses classes and objects" (created by concatting 2 and 3)
            • 3. Re: No. of String Objects ??
              tschodt
              user9065269 wrote:
              [code]
              class ExampleProgram {
              public static void main(String[] args){
              String text = new String("I'm a simple Program ");
              System.out.println(text);
              String text2 = text.concat(
              "that uses classes and objects");
              System.out.println(text2);
              }
              }
              [/code]

              How many Total String Objects are in this code? --???--
              Please explain how if ans answer is 4...
              When the class is loaded it adds to the constant pool
              char[] $charArray1 { 'I','\'','m',' ','a',' ','s','i','m','p','l','e',' ','P','r','o','g','r','a','m',' '}
              String $str1 { value=$charArray1, length=21 }
              char[] $charArray2 { 't','h','a','t',' ','u','s','e','s',' ','c','l','a','s','s','e','s',' ','a','n','d',' ','o','b','j','e','c','t','s' }
              String $str2 { value=$charArray2, length=29 }
              When main() executes new objects are added to the heap
              String text { value=$charArray1, length=21 }
              char[] $charArray3 {'I','\'','m',' ','a',' ','s','i','m','p','l','e',' ','P','r','o','g','r','a','m',' ','t','h','a','t',' ','u','s','e','s',' ','c','l','a','s','s','e','s',' ','a','n','d',' ','o','b','j','e','c','t','s' }
              String text2 { value=$charArray3, length=50 }
              • 4. Re: No. of String Objects ??
                835575
                There are four String objects because..you have declared two String objects named text and text2. The string *"that uses classes and object"*, although not initialized as an object, is a String nevertheless. Finally String args[] is a String object.
                • 5. Re: No. of String Objects ??
                  830868
                  String arg[] or String[] arg is an Array object (an array of Strings).
                  • 6. Re: No. of String Objects ??
                    835575
                    The data type itself String, is an object
                    • 7. Re: No. of String Objects ??
                      796440
                      Levi Morris wrote:
                      There are four String objects because..you have declared two String objects named text and text2.
                      No.

                      One does not declare objects. One declares variables. And variables declared has nothing to do with objects created. Declaring a variable does not create an object. You can declare 100 variables, and have 0, 1, or 100 objects, or any number between.
                      Finally String args[] is a String object.
                      No. It is a String array object, which is most definitely NOT a String object.
                      • 8. Re: No. of String Objects ??
                        796440
                        Levi Morris wrote:
                        The data type itself String, is an object
                        No, it's not. The Class object representing the String class (for which we can obtain a reference with the class literal String.class) is an object, but it is not a String object.
                        • 9. Re: No. of String Objects ??
                          796440
                          fscherpe wrote:
                          String arg[] or String[] arg is an Array object (an array of Strings).
                          Which is irrelevant, since he's asking about String objects,
                          • 10. Re: No. of String Objects ??
                            800268
                            jverd wrote:
                            fscherpe wrote:
                            String arg[] or String[] arg is an Array object (an array of Strings).
                            Which is irrelevant, since he's asking about String objects,
                            It's relevant because Levi Morris (whom he was replying to) claimed incorrectly that a String[] is a String object (as you noted yourself :) ).
                            • 11. Re: No. of String Objects ??
                              796440
                              Walter Laan wrote:
                              jverd wrote:
                              fscherpe wrote:
                              String arg[] or String[] arg is an Array object (an array of Strings).
                              Which is irrelevant, since he's asking about String objects,
                              It's relevant because Levi Morris (whom he was replying to) claimed incorrectly that a String[] is a String object (as you noted yourself :) ).
                              Ah, I see. I didn't realize that he was replying to LM. Thanks for the correction.
                              • 12. Re: No. of String Objects ??
                                835575
                                Thanks for the clarification jverd and fscherpe
                                • 13. Re: No. of String Objects ??
                                  836282
                                  but i think there is only one object in following

                                  1: literal "I'm a simple Program " (created by literal)
                                  2: text variable holding "I'm a simple Program" (created by new String() construct)

                                  so according to me it must me 3 objects....Plz explain........
                                  • 14. Re: No. of String Objects ??
                                    836282
                                    My question is that if there are 4 objects than how ????
                                    1 2 Previous Next