Memory Allocation

What is the difference between below 2 statements?
String s1=new String("abc");
String s2="abc";
How it will allocate in memory?

Questions by singh.pratibha58   answers by singh.pratibha58

Showing Answers 1 - 37 of 37 Answers

String s1=new String("abc");
String s2="abc";

Using new operator we can create many object of the same string class..
For example : 
String s1=new String("abc");
                      String s2=new String("abc");  
Here ,though the content are same ,two different object are created and the address are assigned to different variable,

But when we create object of String class using double quotes   String s2="abc"; we can atmost create only one boject in the entire program .If we try to create more object ,the same address of the alredy created object  will be assigned to the variable pointing to the  newly created object.  
  String s2="abc";    
   String s3="abc";     here content i.e (addres of object) s2 & s3  is same.

Cheers
Omprakash

amandeep3

  • Feb 23rd, 2010
 

String s = "abc"; is the simple case, it will create one string object and one reference variable. "abc" will go into the pool and s will refer to it...

String s = new String("abc"); it will create two objects and one reference variable.
java will create a new string object in the nonpool memory, s will refer to it. the literal "abc" will be placed in the pool.

  Was this answer useful?  Yes

While creating s1=abc this will create in stack part and and s1 is a variable which is pointing towards abc and while creating s2=new sting("abc") this will create in heap part and s2 is pointing toward refrence.

MMK143

  • Apr 4th, 2010
 

In Java, if a create any object to the class using "new" operator, it allocates a entire memory to all class methods and instance variables.

Since String is a predefined class, a variable object for string can be creatred using "new" operator.

For example:
String s1=new String("abc");

In this example the variable object 'S1' has the valueof string "abc";

asanse

  • May 27th, 2010
 

public String(String original)
Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string. Unless an explicit copy of original is needed, use of this constructor is unnecessary since Strings are immutable.

  Was this answer useful?  Yes

jeetu1607

  • May 30th, 2010
 

Whenever a object of String class is created using '=' a object is created in String pool of the JVM but in case the object is created using 'new' keyword, two objects get created, one goes to non-pooled memory i.e. normal memory and the other goes to String pool memory. Only one reference will be created in this case as well as in the prior case.


This concept arises from the 'Immutability' of String class. Immutability of a string means that whenever a object of type String is created, it gets memory in String pool and will stay there forever (Until the JVM restarts). Any further String objects created with same string as value will refer to the same String from string pool. This is better explained in a example below:

String str1 = "abc"';
System.out.println(str1.hashcode());   // Output 1

String str2 = "abc";
System.out.println(str2.hashcode());   // Output 2

This is worth noting that the 'Output 1' and 'Output 2' will be same justifying the immutability of Strings

bharath.334

  • Jun 10th, 2010
 

If String s = new String("something"); --> this saves an object into constant pool. So if you come across any new string same like something then it will create one more object without referring to it.

If String s = "hello" --> it saves an object in a non constant pool. If you come across any new String with hello, (since there is already an object existing)  it will refer to the existing one and makes use of the same.

1. String s="hello";
2. String s=new String("hello");

In first statement, assignment operator is used to assign the string literal to the string variables. In this case, JVM first of all checks whether the same object is already available in the string constant pool.  If it is available then it creates another reference to it.  If the same object is not available then it creates another object with the content "hello" and stores it into string constant pool.

In the second statement, new operator is used to create the string object.  In this case JVM always creates a new object without looking in the string constant pool.

  Was this answer useful?  Yes

In first situtation one object is created in String pool...and in the second situtation two object is created in memory one in Stringpool and second in memeory !

  Was this answer useful?  Yes

In case of s1 2 objects will be formed one in heap memory while other in string constant memory. In case of s2 1 object will be formed in string constant memory.

  Was this answer useful?  Yes

abhinav singh

  • Aug 29th, 2011
 

it call (string.value of )oprater
like this
string s1=new string("abc")
string s2=String.value of"abc"

  Was this answer useful?  Yes

sampra

  • Mar 13th, 2012
 

String s1=new String("abc");- creates 2 obj one in jvm and one in pool.

String s2="abc"; no obj will create it will point to pool above already created existing obj

  Was this answer useful?  Yes

gaurav

  • Jun 15th, 2012
 

in String s1 = "Hello World"; // "Hello world" is a constant and it will be allocated in the string pool. all the variables refering to "Hello World" constant will refer to same memory location in the String pool.
So String s2 = s1; // both will refer to same string constant.

But String s3 = new String("Hello World");// will create a string object in the heap.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions