Login with  Log in with facebook
Hiring Manager? SIGN UP HERE
0

how maney objects are created ?

String str = new String("hello");

and another one

also

String st="a"+"a"+"aa"+"aaaa";

and

String st1="a"+"b"+"c"+"d";

Maneesh Pal
08/21/2011 at 05:34
7 Answers
1
1

String str = new String("hello"); -> two objects: 1st when we declare "hello" and 2nd when String's constructor is called.

String st="a"+"a"+"aa"+"aaaa"; -> X objects: 1st is "a", 2nd when "a" + "a" (2nd "a" comes from String pool), 3th when "aa" + "aa" (declared "aa" comes from String pool) and 4th is when "aaaa" + "aaaa" (declared "aaaa" comes from String pool).

 

String st1="a"+"b"+"c"+"d"; -> 7 objects

 

José Reis
08/21/2011 at 10:20

2
1

thanx jose

Maneesh Pal
08/23/2011 at 07:11

3
0

For the first statement, only one object is created (when the statement is executed -- many objects are created when the class is loaded).

 

Michael Zalewski
08/24/2011 at 16:04

4
0

For the second statement, no new objects are created when the statement is executed. Most developers think that a statement like st = "a" + "b" + "c" + "d" must create an object to hold the result of each concatenation. This is not correct. Since the string is built up from constant literal values, the value of the String is known at compile time. The java compiler (if it's more recent than about 1.0) will evaluate this expression at compile time, and will create a constant that holds the value "abcd".

To prove this, use javap to disassemble the following .class file

public class Test {
  public static void main( String[] asArgs) {
    String s1 = new String( "hello");
    String s2 = "a" + "b" + "c" + "d";
  }
}

 

Michael Zalewski
08/24/2011 at 16:04

5
0

You should get results similar to the following

public class Test extends java.lang.Object
  SourceFile: "Test.java"
  minor version: 0
  major version: 50
  Constant pool:
const #1 = Method       #7.#16; //  java/lang/Object."<init>":()V
const #2 = class        #17;    //  java/lang/String
const #3 = String       #18;    //  hello
const #4 = Method       #2.#19; //  java/lang/String."<init>":(Ljava/lang/String
;)V
const #5 = String       #20;    //  abcd
const #6 = class        #21;    //  Test
const #7 = class        #22;    //  java/lang/Object
const #8 = Asciz        <init>;
const #9 = Asciz        ()V;
const #10 = Asciz       Code;
const #11 = Asciz       LineNumberTable;
const #12 = Asciz       main;
const #13 = Asciz       ([Ljava/lang/String;)V;
const #14 = Asciz       SourceFile;
const #15 = Asciz       Test.java;
const #16 = NameAndType #8:#9;//  "<init>":()V
const #17 = Asciz       java/lang/String;
const #18 = Asciz       hello;
const #19 = NameAndType #8:#23;//  "<init>":(Ljava/lang/String;)V
const #20 = Asciz       abcd;
const #21 = Asciz       Test;
const #22 = Asciz       java/lang/Object;
const #23 = Asciz       (Ljava/lang/String;)V;

{
public Test();
  Code:
   Stack=1, Locals=1, Args_size=1
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return
  LineNumberTable:
   line 1: 0


public static void main(java.lang.String[]);
  Code:
   Stack=3, Locals=3, Args_size=1
   0:   new     #2; //class java/lang/String
   3:   dup
   4:   ldc     #3; //String hello
   6:   invokespecial   #4; //Method java/lang/String."<init>":(Ljava/lang/Strin
g;)V
   9:   astore_1
   10:  ldc     #5; //String abcd
   12:  astore_2
   13:  return
  LineNumberTable:
   line 3: 0
   line 4: 10
   line 5: 13

}

Michael Zalewski
08/24/2011 at 16:05

6
0

First surprise: This simple program, with only two lines, created 23 constant objects (and one constant inside the main() method). But most of these objects are pure overhead. Java needs a String object to hold the class name, the method name, even the name of the String class.

Second surprise: The line String s2 = "a" + "b" + "c" + "d"; actually creates no new objects. As I explained above, a String object to represent "abcd" was created at compile time, and placed into the constant pool. The statement itself generates only the following p-Code

   10:  ldc     #5; //String abcd
   12:  astore_2


That is, the constant #5 representing the String "abcd" is loaded from the constant pool, then that reference is stored in the second local variable to the method.

Michael Zalewski
08/24/2011 at 16:05

7
0

Third surprise: I couldn't enter my entir answer in one post. Got errors when I tried, so I broke the answer into multipl parts.

Michael Zalewski
08/24/2011 at 16:06

If you want to post any answer to this forum then you need to log in.
Schedule a Demo

Schedule a Demo with us

Name *
Email *
Phone *
Company *
Details