I was under the impression that there are actually 3 string objects here,regardless of accessibility - "hello", "world" and "hello world", but the k&b study guide seems to be saying that only two exist, "hello" and "hello world".
Am I mistaken?
William Brogden wrote:Seems to me that after that last line executes there is only one user String reference - the x variable.
To get picky "hello" and "world" exist in the String pool but there is no user reference pointing to them.
Tricky question for sure.
Bill
The first line is straightforward: Create a new String object, give it the value
"Java" , and refer x to it. Next the JVM creates a second String object with the
value "Java Rules!" but nothing refers to it.
nick woodward wrote:
I was under the impression that there are actually 3 string objects here,regardless of accessibility - "hello", "world" and "hello world", but the k&b study guide seems to be saying that only two exist, "hello" and "hello world".
Am I mistaken?
A total of eight String objects were
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)
nick woodward wrote:the k&b study guide seems to be saying that only two exist
nick woodward wrote:plus, i still disagree with their answer slightly. surely " " and the whole expression passed to println() are Strings 9 and 10?
how many String objects and how many reference variables were created prior to the println statement?
Joe Bishara wrote:
nick woodward wrote:the k&b study guide seems to be saying that only two exist
In the first example, the book does not say “there were a total of two String objects created”
Three String objects were created as you rightly point out, however, the book says nothing about the third String object because it is irrelevant to the subject matter. The subject matter is the immutability of String objects i.e. because String objects are immutable, calling concat() on an existing String object creates a new String object instead of modifying the original String object.
nick woodward wrote:plus, i still disagree with their answer slightly. surely " " and the whole expression passed to println() are Strings 9 and 10?
In the second example, the book is correct when it says “there were a total of eight String objects created”
The book says:
how many String objects and how many reference variables were created prior to the println statement?
nick woodward wrote:i do think though that the first example is written pretty badly - it may not say 'a total of two strings', but it does say the second string is 'java rules'. it just seems to fail to explain that the string literal is also a string object - an idea that it then tests you on.
I'm pretty sure many readers would be confused, because the explanation doesn't mention anything about a second String object being created. So they probably think some explanation is missing or it's an errata item and should be "second" (instead of "third").The first line is straightforward: Create a new String object, give it the value "Java", and refer x to it. Next the JVM creates a third String object with the value "Java Rules!" but nothing refers to it. The third String object is instantly lost; you can't get to it.