• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

strings and the string pool

 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just going over Strings again, and had a couple of thoughts/queries.....

In Mala's book, on page 178, this code and explanation appears:

The code at '1' creates a new String object with the value "summer". This object is not placed in the String pool.

I'd agree that 'that' object (the one created with 'new') is not, however:

The code at '2' creates a new String object with the value "summer" and places it in the string pool.

According to K&B (page 265), line 1 creates 2 objects, placing one in the string pool. I guess from the literal argument to the string constructor.

Which is correct? Or have I misunderstood?


Also, on a related topic, if a String becomes unreachable (and was in the String pool) is it eligible for collection, or does it remain in the pool for future use? K&B seems to refer to this as wasted memory, so i thought perhaps strings differed from objects in this way, but can't seem to find clarification.

Thanks in advance!!

Nick
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JVM at the class loading time searches for String literals
If it finds any,then it check whether it has been referenced earlier or not,if not then it creates an object in heap and put its reference in String literal pool(constant table)

In your case ,JVM finds "summer" (string literal) at line 2 and put its reference in string literal pool,now as many time this literal arrives in your code,it gets replaced by its reference in string literal pool

Now in line 1 even it is the same string literal ,but new forces a new object creation(which is not referenced in String literal pool)

Conclusion: summer and pool both has reference to "summer"(string literal)
summer2 has reference to another object with "summer"

Now for GC:
Even if you make summer and summer2 null even though string literal pool has reference to one string
So only one object is eligible for GC

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick woodward wrote:Which is correct? Or have I misunderstood?

This topic discusses exactly the same code snippet. Have a read and let us know if your doubts are cleared.

nick woodward wrote:Also, on a related topic, if a String becomes unreachable (and was in the String pool) is it eligible for collection, or does it remain in the pool for future use? K&B seems to refer to this as wasted memory, so i thought perhaps strings differed from objects in this way, but can't seem to find

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.
So no need to worry about Strings being eligible for garbage collection or not (for the exam).

Hope it helps!
Kind regards,
Roel

 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also want to test my understanding Roel
Is there anything wrong in there?
 
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
Sachin Tripathi wrote:Is there anything wrong in there?

Where is there?
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
nick woodward wrote:Which is correct? Or have I misunderstood?

This topic discusses exactly the same code snippet. Have a read and let us know if your doubts are cleared.

nick woodward wrote:Also, on a related topic, if a String becomes unreachable (and was in the String pool) is it eligible for collection, or does it remain in the pool for future use? K&B seems to refer to this as wasted memory, so i thought perhaps strings differed from objects in this way, but can't seem to find

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.
So no need to worry about Strings being eligible for garbage collection or not (for the exam).

Hope it helps!
Kind regards,
Roel



ah yes, i do remember that note in K&B. i will back away from that topic slowly.... thanks!

as for the number of string objects, I've read the thread you linked, and will look over the article you recommended too. you're right - exactly the same thought process!

thanks again,


Nick

 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was talking about my post,just above your's in this topic
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel, the article in the thread that you linked to by Corey McGlone was interesting, as (obviously!) was the thread itself. Thanks

Answered my question on garbage collection quite nicely, that yes, the literals are not eligible. Makes sense given K&B's comment on the topic, and the use of StringBuilder.

RE the sample code in the first post:

So line 1 creates 2 objects, one when the class loads, the other at run time? The variable on line 2 just references the string pool (replaced when the class is loaded) - and no object is created. I know you said to cut Mala's guide some slack, but as much as I love her chapter on Strings, her explanation does appear to be incorrect. I suppose for the purpose of the exam I can't see how it would make any difference, but I'm not sure why she approached it that way...

...This is of course assuming I've understood this all correctly!

Regards,

Nick

*edit: K&B states that the downside to the string object is that memory is wasted via the creation of an object 'lost' in the string pool when a string is altered. But is that not in many cases the same with StringBuilder too?

StringBuilder sb = new StringBuilder("abc"); still creates the string pool object/reference 'abc', which would be 'lost' in the same manner. The text also mentions that only a single stringbuilder object was needed.... which doesn't seem right when their string example explicitly states that two objects were created, the 'new' object, and the literal.

I do understand the differences between strings and sb when talking about the use of 'new', but this seems like a slightly inconsistant description of the string pool topic..... Sorry for the rambling, I just want to make sure I've got this down 100%!
 
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
nick woodward wrote:So line 1 creates 2 objects, one when the class loads, the other at run time?

Yes, you are correct! The string literals (like "summer") are marked at compile time by the compiler. So that code snippet could look like this (although it probably won't, just for demonstration purpose)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. So when this class is loaded, the code might look like this (again probably not, but for demonstration purpose)So the actual value of LITERAL_POOL[0] is the address of "summer" which was created on the heap (in our example: 1979). Now when the code is executed, another new string will be created (at runtime) on the heap (on address 2015) having the same value as literal "summer".

nick woodward wrote:I know you said to cut Mala's guide some slack, but as much as I love her chapter on Strings, her explanation does appear to be incorrect. I suppose for the purpose of the exam I can't see how it would make any difference, but I'm not sure why she approached it that way...

From all the study guides for OCA and OCP exams, I don't believe any of them mentions that literals are actually processed when the class is loaded. And that's normal, because you don't need to know this for the OCA/OCP exams! You only need to know the existence of the String Literal Pool, how it works and how that affects your code (number of String objects created, comparing String literals,...).
The end result of that code snippet in Mala's study guide is 100% correct. And for the exam that's the most important thing! In most study guides the first line is just a String literal and then a second String is created which is a copy of that String literal. So if in that code snippet line1 and line2 are switched, the explanation is spot-on. And maybe that was the case in a first or second draft, but was changed without revisiting the explanation. So it's indeed incorrect, but (related to the OCA exam) it's a minor issue. Maybe it's done to make it easier to understand: if the explanation would state that first line creates two String objects (one in the Pool and one not in the Pool), that will probably confuse people. And now it's easy: if you use new operator when creating a String, the String will not be in the String Literal Pool. But you can always report this as an erratum, and don't forget to link to these threads

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
nick woodward wrote:*edit: K&B states that the downside to the string object is that memory is wasted via the creation of an object 'lost' in the string pool when a string is altered. But is that not in many cases the same with StringBuilder too?

StringBuilder sb = new StringBuilder("abc"); still creates the string pool object/reference 'abc', which would be 'lost' in the same manner. The text also mentions that only a single stringbuilder object was needed.... which doesn't seem right when their string example explicitly states that two objects were created, the 'new' object, and the literal.

It's always nice to have some context if you ask a question. You can't expect from other ranchers to perfectly know to which code snippet(s) you are referring. Luckily the forum leader knows the code snippets pretty well So let's add them here. First the String exampleAnd then the StringBuilder exampleThe only purpose of these code snippets is to indicate the difference between String and StringBuilder (or StringBuffer) when you perform string manipulations. As you know String is immutable, so any manipulation will result in a new string being created. But with StringBuilder that's not the case: you create 1 object and you can perform all String manipulations you want on that single object. If you are happy with the string you created, you invoke the toString() method and a new String is created.
That's the only purpose of the above code, but the String Literal Pool makes it a little bit more complex/harder. It would probably be much more obvious if the String example was changed toIf you ignore the println() statement and the String literals (as they are in both examples the same), it should be crystal clear that in the String example two objects (of type String) will be created and in the StringBuilder example only one object (of type StringBuilder) will be created.

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
Sachin Tripathi wrote:In your case ,JVM finds "summer" (string literal) at line 2 and put its reference in string literal pool,now as many time this literal arrives in your code,it gets replaced by its reference in string literal pool

Although I don't know how it's implemented in the JVM, I assume String literals will be discovered top-to-bottom. So on line1 the JVM finds the String literal "summer" and because it's not in the String Literal Pool, "summer" is added to the String Literal Pool. On line2 the JVM spots the String literal "summer", and because it's already in the String Literal Pool, just replaces the occurence with the reference from String Literal Pool. When the code runs, a new String will be created with the same value as literal "summer". In this post you'll find a more detailed explanation about what's going on.

Sachin Tripathi wrote:Conclusion: summer and pool both has reference to "summer"(string literal)
summer2 has reference to another object with "summer"

Incorrect! summer refers to a String object (not a literal) with the same value as literal "summer". summer2 and the String Literal Pool refer to String literal "summer".

Sachin Tripathi wrote:Now for GC:
Even if you make summer and summer2 null even though string literal pool has reference to one string
So only one object is eligible for GC

Correct!

Hope it helps!
Kind regards,
Roel
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel, you are too much! (In a good way).

Honestly, the standard of your answers is great. Much appreciated!

I agree with what you're saying about Mala's guide, it just seemed a bit strange to explain it that way. You're right, it's pretty unimportant, and if anything made me learn more about strings by questioning it!

RE: The K&B example (apologies for not adding context, it was an after thought!) - I understand what they were trying to convey, and that fewer objects are created when manipulating stringbuilder objects (I agree completely with your example ignoring literals), but I think this:

Notice that in each of the previous two [stringbuilder] examples, there was a single call to new, so in each example we weren't creating any extra objects. Each example needed only a single StringBuilder object to execute.


is poorly worded given the context.

The single call to 'new' isn't the reason for the lack of an extra object, and wouldn't be the reason for an extra object in a string example (which is what it feels like the implication is). For one thing, there is only a single call to new in that example itself.

Again, probably not that important, but I thought it would be helpful to know from the perspective of a novice - it certainly seemed confusing.

Regards,

Nick


 
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
nick woodward wrote:but I think this:

Notice that in each of the previous two [stringbuilder] examples, there was a single call to new, so in each example we weren't creating any extra objects. Each example needed only a single StringBuilder object to execute.


is poorly worded given the context.

The single call to 'new' isn't the reason for the lack of an extra object, and wouldn't be the reason for an extra object in a string example (which is what it feels like the implication is). For one thing, there is only a single call to new in that example itself.

It's probably nothing more but interpretation. In both StringBuilder examples, there was a single call to new, so each example there was just 1 object created, no other extra objects created. Would the same example have been with String, then you would have had many calls to new, but the main problem is: you don't see them, because these calls happen behind the scenes, on each invocation of a method which manipulates the character sequence. But as you know a String is immutable, so if you concat another String, you'll know a new object (String) will be created. But if you append another String to a StringBuilder, it won't result in creating another object.
I think that's the most important thing to get/understand from these examples.
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
nick woodward wrote:but I think this:

Notice that in each of the previous two [stringbuilder] examples, there was a single call to new, so in each example we weren't creating any extra objects. Each example needed only a single StringBuilder object to execute.


is poorly worded given the context.

The single call to 'new' isn't the reason for the lack of an extra object, and wouldn't be the reason for an extra object in a string example (which is what it feels like the implication is). For one thing, there is only a single call to new in that example itself.

It's probably nothing more but interpretation. In both StringBuilder examples, there was a single call to new, so each example there was just 1 object created, no other extra objects created. Would the same example have been with String, then you would have had many calls to new, but the main problem is: you don't see them, because these calls happen behind the scenes, on each invocation of a method which manipulates the character sequence. But as you know a String is immutable, so if you concat another String, you'll know a new object (String) will be created. But if you append another String to a StringBuilder, it won't result in creating another object.
I think that's the most important thing to get/understand from these examples.


Oh definitely, and I do understand that. And maybe you're right, it probably is just interpretation. I just thought that as the ambiguity came up in my mind when reading, it might come up in others. It's also nice to clarify that I'm not misunderstanding the authors point, even though I knew what they meant by the 'extra' string during manipulation.

So thanks!

Nick
 
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
nick woodward wrote:I just thought that as the ambiguity came up in my mind when reading, it might come up in others.

It's not (yet) reported in the K&B7 errata thread. And as far as I know/remember, no other topics in this forum have questioned this section of the study guide
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I understood, my explaination was regarding the reverse sequence (the most frequent way of occurance,of such type question)as actually asked by op
 
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
Sachin Tripathi wrote:my explaination was regarding the reverse sequence (the most frequent way of occurance,of such type question)as actually asked by op

The OP posted a code snippet and you replied, so probably everybody will assume your explanation is about that code snippet and therefore some parts are incorrect (as I mentioned). If you give an explanation about something else, you should at least mention it and even better provide the code snippet you are talking about. So it's crystal clear for everyone.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No I truly accept my fault,I was saying that the reason of my explanation is just because I was accustomed to reverse sequence of code snippets
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:No I truly accept my fault,I was saying that the reason of my explanation is just because I was accustomed to reverse sequence of code snippets

Now I get it! You just made the switch in your head while typing your text, because you are used to see these code snippets in reverse order. Apologies for not understanding it much faster!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic