2

If

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

What is the memory allocation for x and y ?

4
  • 1
    What do you mean by "What is the memory allocation"? Also, is this homework? You've been asking a series of iPhone-related questions so it's rather surprising to see a Java question out of the blue. Commented Feb 11, 2011 at 17:17
  • what exactly do you mean by "memory allocation"? Commented Feb 11, 2011 at 17:17
  • I think the OP is asking whether the string "abc" will be interned. Commented Feb 11, 2011 at 17:18
  • is x and y are refer to same memory or different memory locations Commented Feb 11, 2011 at 17:19

5 Answers 5

9

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
Sign up to request clarification or add additional context in comments.

3 Comments

You should add that changing either of the variables creates a new Object, as is the case for all String "modifications".
@Argote, saying "assigning creates a new Object" is not strictly correct. If you assign an existing string to one of these references, it will be changed, but no new object will be created, obviously.
@Argote: Sergey is right. It's not the assignment which creates the new object.
1

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.

Comments

1

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. ;)

3 Comments

if i do change String x value "abc" to "xyz" does it creates new allocation?
When the class loads it creates two String literals, but doesn't allocate any more objects.
@kiran, yes, if you do that, you will have three objects: one for the string literal "xyz", and two strings x and y with identical content, but occupying different memory areas. But it makes no sense to do so.
0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.