Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String object creation in java [duplicate]

Tags:

java

string

Possible Duplicate:
Questions about Java’s String pool

I have a doubt in java Strings object creation.

String s1 = "Hello"+"world";
String s2 = s1+"Java";

in this program how many String objects will be created and how ?please explain it. Thanks.

like image 611
AndroidCrazy Avatar asked Jan 07 '13 04:01

AndroidCrazy


People also ask

How do you duplicate a string in Java?

Here is the shortest version (Java 1.5+ required): repeated = new String(new char[n]). replace("\0", s); Where n is the number of times you want to repeat the string and s is the string to repeat.

How do you duplicate an object in Java?

The clone() method of the class java. lang. Object accepts an object as a parameter, creates and returns a copy of it.

Can set have duplicate objects in Java?

A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.

What is object duplicate?

Duplicating an object creates a copy of it and adds it to the bottom of the object list in the Objects palette. When duplicating, you can choose to copy just the object or both the object and its associated shader network. 1.


3 Answers

The answer is 3

Two String objects will be created once per JVM start:

  • "Helloworld"
  • "Java"

Both will be interned, because they are constants (known at compile time).

They will be reused every time this code runs. A StringBuilder will be created to concatenate the two String above. References to them will be assigned to s1 and s2.

Here's the bytecode for the code:

   0:   ldc #37; //String Helloworld
   2:   astore_1
   3:   new #39; //class java/lang/StringBuilder
   6:   dup
   7:   aload_1
   8:   invokestatic    #41; //Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;
   11:  invokespecial   #47; //Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
   14:  ldc #50; //String Java
   16:  invokevirtual   #52; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   19:  invokevirtual   #56; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
   22:  astore_2
   23:  return
like image 88
Bohemian Avatar answered Nov 13 '22 12:11

Bohemian


You can't really say, how many Strings are created, since there's several differences due to the different implementations of the JVM.

As String is an immutable class, the naive answer is 5. But with some optimization (e.g. using a StringBuffer/ StringBuilder there would only be 2 Strings.

As concats would be summarized via append()-calls.

Edit: As the're some different answers here an explanation why I said 5:

  1. "Hello"
  2. "world"
  3. (s1) "Helloworld"
  4. "Java"
  5. (s2) "HelloworldJava"
like image 35
Zhedar Avatar answered Nov 13 '22 13:11

Zhedar


if you look at the compiled code, you can easily guess:

String s1 = "Helloworld";
String s2 = (new StringBuilder(String.valueOf(s1))).append("Java").toString();

We can't accurately know by just looking at source code as many optimizations are done by the compiler before execution.

Here we see that 1 String object is created for s1, and another String object for s2. Here 2 string literals are there in the string pool: "Helloworld" and "Java"

like image 2
vishal_aim Avatar answered Nov 13 '22 13:11

vishal_aim