Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference Between Assignment and Creating Instance of String? [duplicate]

In an interview question, Interviewer asked me

What is the common and difference between the following statements:

String s = "Test";

String s = new String("Test");

Is there any difference in memory allocation?

like image 556
Shashi Avatar asked Feb 01 '10 06:02

Shashi


People also ask

What is the difference between creating string as new () and literal?

4. String Literal vs String Object. When we create a String object using the new() operator, it always creates a new object in heap memory. On the other hand, if we create an object using String literal syntax e.g. “Baeldung”, it may return an existing object from the String pool, if it already exists.

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.

What is difference between String str abc and string str new string ABC?

What is difference between String str = "ABC" and String str = new String("ABC") ? String str = "abc"; is a String literal,where as String str = new String("abc") is a String Object.

What is the difference between text and new String text )?

String s = "text"; the other one creates a string in the constant pool ( "text" ) and another string in normal heap space ( s ). Both strings will have the same value, that of "text". String s = new String("text");


3 Answers

String s = "Test"; 

Will first look for the String "Test" in the string constant pool. If found s will be made to refer to the found object. If not found, a new String object is created, added to the pool and s is made to refer to the newly created object.

String s = new String("Test");

Will first create a new string object and make s refer to it. Additionally an entry for string "Test" is made in the string constant pool, if its not already there.

So assuming string "Test" is not in the pool, the first declaration will create one object while the second will create two objects.

like image 60
codaddict Avatar answered Sep 24 '22 12:09

codaddict


The differnce in term of memory is that the expressions of the form : String s = "test" uses the "interned" string so as to share unique instances.

The invocation of form: String s = "test"
is efficient compared to String s = new String("test")

The first call makes use of the existing constant expression (if there is one), the second call creates a new instance without making use of any existing instance.

Below code chunk demonstrates this:

String test = new String("test");
String internTest = "test";
String nonInternTest = new String("test");

System.out.println(test == internTest); // prints false
System.out.println(test != nonInternTest); // prints true
System.out.println(test.equals(nonInternTest)); // prints true

Also note that the JLS specifies the behavior to be thus:
Each string literal is a reference to an instance of class String (§4.3.3). String objects have a constant value. String literals-or, more generally, strings that are the values of constant expressions are "interned" so as to share unique instances, using the method String.intern.

like image 45
sateesh Avatar answered Sep 24 '22 12:09

sateesh


String s = "Test"; // creates one String object and one reference variable In this simple case, "Test" will go in the pool and s will refer to it.

String s = new String("Test"); // creates two objects and one reference variable In this case, because we used the new keyword, Java will create a new String object in normal (non-pool) memory, and s will refer to it. In addition, the literal "Test" will be placed in the pool.

but what they have in common is that they all create a new String object, with a value of "Test", and assign it to a reference variable s.

like image 20
sap Avatar answered Sep 24 '22 12:09

sap