This discussion is archived
1 2 Previous Next 16 Replies Latest reply: May 3, 2008 3:55 AM by 807591 RSS

Question regarding String objects and memory

807591 Newbie
Currently Being Moderated
I have following two statements:

String str1 = new String("java");
String str2 = "java";

Now how many objects are created by each line and where?
Secondly, where is strings constant pool created? IN the heap itself??
  • 1. Re: Question regarding String objects and memory
    807591 Newbie
    Currently Being Moderated
    You first. Tell us what you think the answer is and if you are wrong we can point out where.
  • 2. Re: Question regarding String objects and memory
    807591 Newbie
    Currently Being Moderated
    I think there are two objects created by first line, one in string constant pool and one on the heap that is pointed by str1. NOw when the second line is executed, str2 (since its a literal) , will check if there is any object (i.e. with the value "java") in the string constant pool and since it finds one (that was created by first statement) it wont create any new object, but just point its reference to that. right? so there are total two objects created by these two statements.
  • 3. Re: Question regarding String objects and memory
    807591 Newbie
    Currently Being Moderated
    anyone?
  • 4. Re: Question regarding String objects and memory
    801523 Newbie
    Currently Being Moderated
    Yes, you are right. total two objects will be created for above statements and two objects will be in heap memory
  • 5. Re: Question regarding String objects and memory
    796440 Guru
    Currently Being Moderated
    javanewbie80 wrote:
    I have following two statements:

    String str1 = new String("java");
    String str2 = "java";

    Now how many objects are created by each line and where?
    Second line: An entry is created in the .class file at compile time. When the class is loaded, that entry ("java") is put into the constant pool. Executing that line at runtime does not create any objects. The "java" String object was created--in the constant pool--when the class was loaded. This would also be the case if you switched the order of the lines. This is true for all string literals in your source code.

    First line: Since there's new, an object is created when you execute that line. The literal "java" does not cause the creation of a String object when that line is executed, because String literals never do.
    Secondly, where is strings constant pool created? IN the heap itself??
    In the method area, which is logically part of the heap, but may be treated differently for GC purposes:
    [http://java.sun.com/docs/books/jvms/second_edition/html/Overview.doc.html#22972]
    [http://java.sun.com/docs/books/jvms/second_edition/html/Overview.doc.html#6656]
  • 6. Re: Question regarding String objects and memory
    796440 Guru
    Currently Being Moderated
    pvmk wrote:
    Yes, you are right. total two objects will be created for above statements and two objects will be in heap memory
    Depends what you mean.

    One is created when the class is loaded. Execution of those two statements only creates one object.
  • 7. Re: Question regarding String objects and memory
    807591 Newbie
    Currently Being Moderated
    Yes that's right that two objects were created after these stmt, But that is absolutely wrong that both the objects going to place in the same heap... Java keep aside a special memory for the string constant that is called "String Constant Pool". when the compiler encounters a String literals, its checks the pool for the identical string, if match is found ,the reference to the new string literal is directed to the existing string and new string literal object is created. (the existing string simply has a additional reference). That is why the String are immutable objects.

    check this:

    String s = "abc";
    In this case "abc" will go in the pool and s refer to it.


    String s = new String("abc");
    In this case becouse I have used new keyword, java will create a new string object in the normal memory (nonpool memory). and s refer to it. In addition the literal "abc" will be placed in the pool area.
  • 8. Re: Question regarding String objects and memory
    796440 Guru
    Currently Being Moderated
    Poonam_Agarwal wrote:
    Yes that's right that two objects were created after these stmt, But that is absolutely wrong that both the objects going to place in the same heap...
    Did you read the part of the spec that I quoted? It states that they are placed in the method area, which is part of the heap
    Java keep aside a special memory for the string constant that is called "String Constant Pool".
    Yes, and that's part of the heap.
    when the compiler encounters a String literals, its checks the pool for the identical string, if match is found ,the reference to the new string literal is directed to the existing string and new string literal object is created.
    No.

    First, there's no such thing as a "string literal object." There's just one kind of String object--an instance of the String class.

    Second, when the compiler encounters a string literal that it has already encountered for the current class, no new object is created. A reference to the existing one is simply inserted.
    (the existing string simply has a additional reference).
    Yes, which is different from what you said just above.
    That is why the String are immutable objects.
    No, that is not why.

    check this:

    String s = "abc";
    In this case "abc" will go in the pool and s refer to it.
    Yes. It get placed into the pool when the class is loaded, and s is made to refer to it when that line is executed.
    String s = new String("abc");
    In this case becouse I have used new keyword, java will create a new string object in the normal memory (nonpool memory). and s refer to it.
    Yes.
    In addition the literal "abc" will be placed in the pool area.
    Not when that line is executed, but when the class is loaded.
  • 9. Re: Question regarding String objects and memory
    807591 Newbie
    Currently Being Moderated
    jverd wrote:
    It states that they are placed in the method area, which is part of the heap
    There is nothing called as methos area. Please read the stuff again. I mentioned all the details which u talking about. actually both mentioned the same stuff.

    Second, when the compiler encounters a string literal that it has already encountered for the current class, no new object is created. A reference to the existing one is simply inserted.
    (the existing string simply has a additional reference).
    Yes, which is different from what you said just above
    I said the same think that existing string simply got the additional reference. The sentance which i menatoned was "
    *when the compiler encounters a String literals, its checks the pool for the identical string, if match is found ,the reference to the new string literal is directed to the existing string and no new string literal object is created*"

    >
    That is why the String are immutable objects.
    No, that is not why.
    And if this is not the case pleaes make me learn then what causes String immutable.

    Not when that line is executed, but when the class is loaded.
    It states that they are placed in the method area, which is part of the heap
    Please explain me this poin...!
  • 10. Re: Question regarding String objects and memory
    807591 Newbie
    Currently Being Moderated
    javanewbie80 wrote:
    I have following two statements:

    String str1 = new String("java");
    String str2 = "java";

    Now how many objects are created by each line and where?
    There is no way of knowing without checking the source for class String since the String class is composition and creates private objects.
  • 11. Re: Question regarding String objects and memory
    807591 Newbie
    Currently Being Moderated
    jverd wrote:
    It states that they are placed in the method area, which is part of the heap
    There is nothing called as methos area. Please read the stuff again. I mentioned all the details which u talking about. actually both mentioned the same stuff.

    Second, when the compiler encounters a string literal that it has already encountered for the current class, no new object is created. A reference to the existing one is simply inserted.
    (the existing string simply has a additional reference).
    Yes, which is different from what you said just above
    I said the same think that existing string simply got the additional reference. The sentance which i menatoned was "
    *when the compiler encounters a String literals, its checks the pool for the identical string, if match is found ,the reference to the new string literal is directed to the existing string and no new string literal object is created*"

    >
    That is why the String are immutable objects.
    No, that is not why.
    And if this is not the case pleaes make me learn then what causes String immutable.

    Not when that line is executed, but when the class is loaded.
    It states that they are placed in the method area, which is part of the heap
    Please explain me this poin...!
  • 12. Re: Question regarding String objects and memory
    796440 Guru
    Currently Being Moderated
    Poonam_Agarwal wrote:
    jverd wrote:
    It states that they are placed in the method area, which is part of the heap
    There is nothing called as methos area.
    Yes, there is. Read the section of the JVM spec I referred to.
    Please read the stuff again. I mentioned all the details which u talking about. actually both mentioned the same stuff.
    I read what you wrote and corrected where you were wrong.

    Not when that line is executed, but when the class is loaded.
    It states that they are placed in the method area, which is part of the heap
    Please explain me this poin...!
    You mean the part about the String object being created when the class is loaded, rather than when the line is executed? That's in the JVM spec somewhere.

    Or do you mean the part about the method area? That's in the JVM spec at one of the links I provided in an earlier post.

    Edited by: jverd on May 3, 2008 11:56 AM
  • 13. Re: Question regarding String objects and memory
    796440 Guru
    Currently Being Moderated
    Poonam_Agarwal wrote:
    jverd wrote:
    Second, when the compiler encounters a string literal that it has already encountered for the current class, no new object is created. A reference to the existing one is simply inserted.

    (the existing string simply has a additional reference).
    Yes, which is different from what you said just above
    I said the same think that existing string simply got the additional reference. The sentance which i menatoned was "
    *when the compiler encounters a String literals, its checks the pool for the identical string, if match is found ,the reference to the new string literal is directed to the existing string and no new string literal object is created*"
    No, what you said--which I just copied from your earlier post--was this: the reference to the new string literal is directed to the existing string and new string literal object is created.

    You did not say "no new string literal object." You left out the word "no", which to me says that you meant that one is created. If that was just a typo on your part and you meant to say "no", fine that's correct. But I can't read your mind. I can only go by what you actually said.
    That is why the String are immutable objects.
    No, that is not why.
    And if this is not the case pleaes make me learn then what causes String immutable.
    What makes String immutable is that there's no setter method or other way to modify its state. String could be mutable and still exist in the pool (though that wouldn't make much sense, and would be difficult to manage). It could also be immutable even if there were no constant pool. The constant pool doesn't make string immutable, though String's immutability makes the constant pool easier to manage.
  • 14. Re: Question regarding String objects and memory
    807591 Newbie
    Currently Being Moderated
    Yes.. i actually missed out the word "no", and i don't even realize it. I got the first point about the behaviour of String object but still in terms for the statements u have given me about the "Why string are immutable" . that's true that String dosen't provide any methods to modify its state. But there are so many classes in Java those too dosen't offer the same thing, intead we can change their objects state.

    I don't know much about the String memory management , but i am very sure about "The String objects are immutable becouse of the way JVM handle its memory area". The same thing on which we have been discussing so far. :)
1 2 Previous Next