• Post Reply Bookmark Topic Watch Topic
  • New Topic

Number of String objects  RSS feed

 
Ed Charles
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm just going through Bert Bate's SCJP 6 Study Guide and am on the section of Strings. It seems quite straightforward but I want to understand an example in the book. The code is



The query was what is the output? How many String objects? How many reference variables?

According to the book it says there are 8 String objects. I want to check that is right and if so check why as i would have thought there were 10 String objects:

"spring ", "summer ", "spring summer ", "fall ", "spring fall ", "spring summer fall ", "winter ", "spring winter " are the eight listed in the book and I follow that. But what about the output? I would have thought that created at least one String object, if not two. I would have thought there would also be the String objects "spring winter spring summer " and " ".

Hopefuly someone can clarify :-)
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let’s add code tags, which you should always use. Not only does the post look better, but it also has line numbers. I think what you have is as follows:-

Line 1: One String: "spring "
Line 2: Two strings: "summer " and "spring summer "
Line 3: Two strings: "fall " and "spring fall ". The latter is not given an identifier, so it drops into a sort of cyber-limbo never to be seen again.
Line 4: One Strings: "spring summer spring ", which again vanishes without trace.
Line 5: Two Strings: "winter" and "spring winter "
Line 6: Two Strings: " ", "spring winter ‍ spring summer ". The latter is printed at the command line and thereafter disappears completely.
And was it two reference variables?

Any advance on that lot?

Don’t copy and past this post because some of the spaces are not what they seem to be.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look up System.out.println(String), and you find it calls this. Now you are going to get that from the + operators, which use a StringBuilder in the background, returning the string from its toString() method.
That creates the one String "spring summer ‍ spring winter " or whatever.

Again don’t use copy and paste.
 
Ed Charles
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I should have paid more attention to the text in the book as it said 8 objects prior to the println statement.

Out of interest when Strings become lost and they are in the String Constant Pool can they be 'found' again? For example:



On line 2 the String "test" is lost but I take it, as long as the garbage collector has not run, then it will still be in the String Constant Pool. On line 3 a new reference variable is created. Now does this also create a new String object "test" on the constant pool or is it smart enough to say we already have that so use the old one if still available?
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It finds the old object and reuses it. Search this site for “Strings, Literally” and you find this, which tells you all sorts of useful things about Strings and the String pool.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ed Charles wrote:Thanks, I should have paid more attention to the text in the book as it said 8 objects prior to the println statement.

Out of interest when Strings become lost and they are in the String Constant Pool can they be 'found' again? For example:



On line 2 the String "test" is lost but I take it, as long as the garbage collector has not run, then it will still be in the String Constant Pool. On line 3 a new reference variable is created. Now does this also create a new String object "test" on the constant pool or is it smart enough to say we already have that so use the old one if still available?


Hi Ed. I give you a scenario to test if you have actually understood difference between a String literal and a new String object.

I have the following 2 different statements:

Case 1:


Case 2:


My question is :

a) In case 1, how many objects and references are there?

b) In case 2, how many objects and references are there?

 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He knows the difference between a literal and a new String.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!