• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

new String("abc") - one object or two?

 
Steffe Wilson
Ranch Hand
Posts: 165
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K&B says that this code:

will result in two objects being created, one for the string object created by 'new' in non-pool and one for the literal string "abc" in the string constant pool.

The Mala Gupta book presents a similar example:

and says this only creates one object: "creates a new String object with the value "Summer". This object is not placed in the String constant pool."

Now the disparity may be due to some subtle difference in the authors' accounting but my immediate question is this:
If this form of String creation is included in an exam question, should I assume that one object has been created or two?
 
Liutauras Vilda
Bartender
Pie
Posts: 2756
111
BSD VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Such question in one or another form definitely should come up in exam.
I'm not sure in which context you read that, but in this case it is created 1 object only and it is not placed in a String constant pool, but rather created in memory location called Heap.
 
Liutauras Vilda
Bartender
Pie
Posts: 2756
111
BSD VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the example were like this, then 2 objects created.

In example below only 1 object (because str1 reuses immutable string object from constant pool).
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steffe Wilson wrote:K&B says that this code:

will result in two objects being created, one for the string object created by 'new' in non-pool and one for the literal string "abc" in the string constant pool.

That's correct (assuming of course the String Constant Pool was empty or at least not containing the "abc" literal).

Steffe Wilson wrote:The Mala Gupta book presents a similar example:

and says this only creates one object: "creates a new String object with the value "Summer". This object is not placed in the String constant pool."

This thread and this one discuss that code snippet already in great detail.

Steffe Wilson wrote:Now the disparity may be due to some subtle difference in the authors' accounting but my immediate question is this:
If this form of String creation is included in an exam question, should I assume that one object has been created or two?

Although I can't remember having such a question on the actual exam (asking me for the number of String objects created), without any doubt: two! Here is an excellent article about strings, the String Constant Pool and how literals are processed. From this article you know, when a class is loaded the JVM will look for String literals and checks the String Literal Pool to see if there is already an equivalent String. If not, the String is created and its reference added to the pool. As far as I know, no study guide mentions this and the reason is simply: you don't need to know how String literals are processed.

And finally, although strings and garbage collection can be lots of fun I like to mention this quite important note (from K&B7, page 201)
K&B7 wrote:Note: Due to the vagaries of the String constant pool, the exam focuses its garbage collection questions on non-String objects, and so our garbage collection discussions apply to only non-String objects too.


Hope it helps!
Kind regards,
Roel
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:I'm not sure in which context you read that, but in this case it is created 1 object only and it is not placed in a String constant pool, but rather created in memory location called Heap.

That's completely wrong!

In this statement"abc" is a literal and will be placed in the String Literal Pool (when the class is loaded by the JVM). Also another String will be created with exactly the same character sequence as "abc" (which is not added to the String Literal Pool).
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:If the example were like this, then 2 objects created.

Not correct! The first line is not required to create 2 String objects (as explained in my previous posts here and here).

Liutauras Vilda wrote:In example below only 1 object (because str1 reuses immutable string object from constant pool).

Correct! Assuming of course the String Literal Pool was empty or at least not containing the "abc" literal (otherwise no objects will be created).
 
Liutauras Vilda
Bartender
Pie
Posts: 2756
111
BSD VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:Here is an excellent article about strings

The SCJP Tip Line wrote:Really, it's a collection of references to String objects. Strings, even though they are immutable, are still objects like any other in Java. Objects are created on the heap and Strings are no exception. So, Strings that are part of the "String Literal Pool" still live on the heap, but they have references to them from the String Literal Pool.

Roel De Nijs wrote:"abc" is a literal and will be placed in the String Literal Pool (when the class is loaded by the JVM). Also another String will be created with exactly the same character sequence as "abc" (which is not added to the String Literal Pool).
Can we say, that not string literal placed in the string pool, but the reference, which refers to the immutable string object on a heap?


1st line. So, when the class is loaded, reference to string object "text" being placed in the string constant pool, and at the runtime another string object "text" being created right on the heap.
2nd line. s1 going to reuse the same string object which is referenced from string constant pool, during the s creation.

Is that correct?


Steffe Wilson, please ignore my previous posts - these were not correct.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Can we say, that not string literal placed in the string pool, but the reference, which refers to the immutable string object on a heap?

Of course! But that's very long to type each time So the quote ""abc" is a literal and will be placed in the String Literal Pool" is short for: "abc" is a literal and the String Literal Pool is checked for an occurence of "abc". If found, the reference to this object, is returned. Otherwise, the String object "abc" is created on the heap, the reference is stored in the String Literal Pool and the reference is returned.

Liutauras Vilda wrote:1st line. So, when the class is loaded, reference to string object "text" being placed in the string constant pool, and at the runtime another string object "text" being created right on the heap.
2nd line. s1 going to reuse the same string object which is referenced from string constant pool, during the s creation.

Is that correct?

I don't fully understand your explanation, so I don't know if it's 100% correct. All String literals will be handled when the class is loaded by the JVM. So when those statement are actually executed, the String literals (both on line1 and line2) have already been handled appropriately and one new String object is created (with exact same character sequence as String literal "text") on the heap (and will not be added to the String Literal Pool). This post has an excellent explanation about what's going on behind the scenes (disclaimer: it's a post of me, so I might be a little biased )

Hope it helps!
Kind regards,
Roel
 
Liutauras Vilda
Bartender
Pie
Posts: 2756
111
BSD VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. Topic about the Strings is not easy at all, and can raise confusion immediately when start digging deeper.

code example and quote from Roel's earlier post wrote:Then when the class is loaded, the JVM will check for these messages and perform its String Literal magic. So it encounters 1st occurence of "summer" (on line 1), checks if this literal is already in the String Literal pool; because it's not, it's created on the heap (on address 1979) and a reference to that object in the constant table. Then the 2nd occurence of "summer" (on line 2) is processed: this string is already in the String Literal Pool, so its reference is returned.

I'm still a bit unsure about underlined sentence. Roel, please clarify if I understand correct:
Won't necessarily create an object on the heap, if the "summer" litteral" is already in the constant pool from earlier?
 
Steffe Wilson
Ranch Hand
Posts: 165
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Roel for clarifying and for the interesting links.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Won't necessarily create an object on the heap, if the "summer" litteral" is already in the constant pool from earlier?

Exactly!

Assume you have this interfaceand this class

Consider the following two scenarios. For every scenario we assume the String Literal Pool is completely empty.

Scenario 1: interface Season is loaded by the JVM before class SeasonStuff
When interface Season is loaded by the JVM, the JVM notices the String literal "summer" (on line1) and handles appropriately: the String object "summer" is created on the heap and its reference is added to the String Literal Pool. When class SeasonStuff is loaded by the JVM, the JVM notices the String literal "summer" (on line2) and handles appropriately: "summer" is already in the String Literal Pool and thus its reference is returned (no object is created). When the go() method is invoked, a new String object is created (with same character sequence as "summer") on the heap, its reference is returned and assigned to reference variable summer. This object is not a String literal and therefore not added to the String Literal Pool.

Scenario 2: the JVM only needs to load class SeasonStuff
When class SeasonStuff is loaded by the JVM, the JVM notices the String literal "summer" (on line2) and handles appropriately: the String object "summer" is created on the heap and its reference is added to the String Literal Pool. When the go() method is invoked, a new String object is created (with same character sequence as "summer") on the heap, its reference is returned and assigned to reference variable summer. This object is not a String literal and therefore not added to the String Literal Pool.

Hope it helps!
Kind regards,
Roel
 
Liutauras Vilda
Bartender
Pie
Posts: 2756
111
BSD VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. That was very explicit.
In addition of that, i'll revise my books, but I don't think there is so explicit information. That is why I think my thoughts were fallen apart.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic