Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B SCJP Study Guide for Java 5 ERRATA p 419

 
Harry Henriques
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


What is the output? For extra credit, how many String objects and how many
reference variables were created prior to the println statement?

Answer: The result of this code fragment is "spring winter spring summer". There
are two reference variables, s1 and s2. There were a total of eight String objects
created as follows: "spring", "summer " (lost), "spring summer", "fall" (lost), "spring
fall" (lost), "spring summer spring" (lost), "winter" (lost), "spring winter" (at this point
"spring" is lost). Only two of the eight String objects are not lost in this process.


I think the Answer: is incorrect, There are a total of nine String objects created as
follows: "spring " (lost), "summer " (lost), "spring summer ", "fall " (lost), "spring fall " (lost),
"spring summer spring " (lost), "winter " (lost), "spring winter ", and " " (lost).
Only two of the nine String objects are not lost in this process.

What do you think?
 
Harry Henriques
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see my mistake. There are eight String objects created prior to the System.out.println() statement.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic