Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is now right about the String?

 
Kamil Hlubek
Ranch Hand
Posts: 49
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I have read, that if you write the following:

String str = new String( "True?" );

Java creates at first the str reference and then a String object, which is assigned to str.
And now comes the thing, where it gets interesting.
In the book of K&B I read that the string literal "True?" will be set in the String constant pool, but in the book from Mala Gupta you can read in an example of her it is not like that.

My question about is, what is now right about the topic? ( I thing that K&B has right. )

Yours sincerely,

Kamil
 
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
Have you already read this excellent article about string literals and the String Literal Pool?
 
Kamil Hlubek
Ranch Hand
Posts: 49
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a really excellent explanation. Thank you for the information, but I find it sad, that out of 3 books I read for the OCAJP only 1 talked about it ( That from K&B ).
 
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
Kamil Hlubek wrote:but I find it sad, that out of 3 books I read for the OCAJP only 1 talked about it ( That from K&B ).

That's probably because it looks pretty easy and straightforward, but actually a lot is happening behind the scenes.

Let's assume this line of code"True?" is clearly a String literal, so it will be added to the String Literal Pool. But instead of assigning the reference from the String Literal Pool to reference variable str, a new String is created with the same characters as the String literal "True?". So after this line of code you'll have actually two String objects: the String literal "True?" which is referenced from the String Literal Pool and another String "True?" which is not a literal.

And to illustrate the above, I made a little programSo thanks to the String Literal Pool, you still have two String objects: the String literal "Java rocks!" which is referenced by s1 and s2 (and from the String Literal Pool as well) and another String "Java rocks!" which is not a literal (and is referenced by s3).

Hope it helps!
Kind regards,
Roel
 
Kamil Hlubek
Ranch Hand
Posts: 49
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for that explanation, but if it is like that ( as I already thought ) than an example of counting the references in the book from Mala Gupta is false.
Is it rigth, that you have to look at that, when you have to count the objects on the OCAJP ( 8 ) ?
 
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
Kamil Hlubek wrote:Is it rigth, that you have to look at that, when you have to count the objects on the OCAJP ( 8 ) ?

To be honest, I don't know if you could get questions on the exam to simply count the number of created objects. I do know on the exam you need to know how many objects are eligible for garbage collection.

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.
 
Kamil Hlubek
Ranch Hand
Posts: 49
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Kamil Hlubek wrote:Is it rigth, that you have to look at that, when you have to count the objects on the OCAJP ( 8 ) ?

To be honest, I don't know if you could get questions on the exam to simply count the number of created objects. I do know on the exam you need to know how many objects are eligible for garbage collection.

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.


This explains, why the authors of the preperation books did not mention it ( or mentioned it only as a nice to know thing ). But it is no explanation to the mistake of Mala Gupta at page 178/179 ( in the explanation of the example ).
 
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
Kamil Hlubek wrote:But it is no explanation to the mistake of Mala Gupta at page 178/179 ( in the explanation of the example ).

I assume you are referring to this code snippetAs the book mentions, this code snippet creates a total of 5 String objects: three String literals "Summer", "autumn", and "summer" (all referenced from the String Literal Pool); and two String objects "Summer" (referenced by summer) and again "Summer" (referenced by autumn).

Hope it helps!
Kind regards,
Roel
 
Kamil Hlubek
Ranch Hand
Posts: 49
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the book mentions, this code snippet creates a total of 5 String objects: three String literals "Summer", "autumn", and "summer" (all referenced from the String Literal Pool); and two String objects "Summer" (referenced by summer) and again "Summer" (referenced by autumn).


Yes that is right and the result is also right, but the explanation says, that at line 3 one object is created and nothing more, but it is lacking the reference to a second String "Summer" object is set right here in the String constant pool. It mentions it first at line 4 ( where it actually only use the reference from the reference to the "Summer" object in the String 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
Kamil Hlubek wrote:Yes that is right and the result is also right, but the explanation says, that at line 3 one object is created and nothing more, but it is lacking the reference to a second String "Summer" object is set right here in the String constant pool. It mentions it first at line 4 ( where it actually only use the reference from the reference to the "Summer" object in the String constant pool ).

I think you should give the study guide a little bit of slack here.

As you know from the excellent article (you have read earlier), 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 doesn't mention this and the reason is simply: you don't need to know how String literals are processed.
If you switch 1 and 2 in the above code snippet, like thisthe explanation is spot-on! Because now "Summer" will be added to the String Literal Pool (2) and on 1 a new String is created which has the same value as the String object "Summer" (referenced from the String Literal Pool.

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic