kiran kumar
kiran kumar

Reputation: 1359

Memory allocation in Java Strings

If

String x = "abc";
 String y = "abc";

What is the memory allocation for x and y ?

Upvotes: 2

Views: 4528

Answers (5)

Priyanshu Gupta
Priyanshu Gupta

Reputation: 1

Because strings are most commonly used. So Java uses memory optimization and to prevent excessive usage of memory it uses string pool memory in which if a string object with the same value is already present then the new object reference created with the same string value will point towards the same. But if the string is created using "new" then the object is created in heap memory and also in string pool if the same string value is not already present in it. To understand more differences and how the memory is allocated go through the code snippet.

    String s1="hello";   // here s1 is string object reference, "hello" is string 
                         // value of string object created using this statement
    String s2="hello";   // s2 reference the same string object as s1 do in string 
                         //  pool area
    String s3=new String("hello"); // string object created in heap memory
    String s4=new String("hello"); // new string object created in heap memory
    System.out.println(s1==s2);   //true as both string object references point 
                                  // towards same object in string pool area
    System.out.println(s3==s4);   //false as both string objects reference to two 
                              //different object having same string value in heap area
    String s5="hell"+"o";      
    System.out.println(s1==s5);  //true as s5 is a combination of two strings 
    String s6="hell";
    String s7=s6+"o";     // when we use already initialised string object reference, 
                           //  then new object created in heap memory
    System.out.println(s1==s7);  //false
    s6+="o";    //when we use already initialised string object reference, then new 
                //object created in heap memory
    System.out.println(s1==s6); //false
    System.out.println(s3==s6); //false as objects in heap memory are not comparable 
                                //even if with same string value using "==" so use 
                                //equals() function
    s6="hello";
    s4="hello";   // initially s4 was created using "new" so points string in heap 
                  // area now it points towards string pool area
    System.out.println(s1==s6);  //true s6 is reinitialized withou using new so 
                                 //reference the string in string pool area which is 
                                 //common for all objects with same string value
    System.out.println(s3==s6);  //false again one object(s3) reference string object 
                                 //in heap area and s6 reference string object in 
                                 //string pool
    System.out.println(s3=="hello");  // false as s3 reference string object in heap 
                                      //   area so not comparable
    System.out.println(s1=="hello");  // true
    String s8="";
    s8+="hello";
    System.out.println(s1==s8);    //false

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533432

There is only one string which be placed in the String literal pool. No matter how many times you run these two lines e.g in a loop, not more objects will be allocated.

EDIT: If you want to create more objects you can do this.

String x = new String("abc"); // don't do this
String y = new String("xyz"); // don't do this either.

This creates an object every time because you told it to. ;)

Upvotes: 1

ramshankar
ramshankar

Reputation:

String x = "abc"; it will create one string object and one reference variable. "abc" will go into the pool and x will refer to it.

Upvotes: 0

zoul
zoul

Reputation: 104065

Here is a nice reference regarding string literals in Java. I guess you are interested in this quotation:

If String objects having the same data are created using a constant expression, a string literal, a reference to an existing string, or by explicitly using the intern() method, their references will be the same.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1499770

The two variables will each take as much space as is required for a reference.

The two references will both have the same value - that is, they'll refer to the same object - due to interning of string literals. In other words, there will only be one String object. However many times you execute this piece of code (within the same classloader, at least) the values of x and y will always refer to the same single object.

The two variables are still independent, of course - you can change one without changing the other:

String x = "abc";
String y = "abc";
x = "def";
System.out.println(y); // Still prints abc

Upvotes: 9

Related Questions