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

Quick string question (K&B7)

 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Author and all-around good cowpoke
Rancher
Posts: 13077
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


yeah, agree witht he references, but in their example (sorry i was paraphrasing from memory) they seem to point out that the "second" object has no reference....


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.


isn't 'rules' the second, and 'java rules' the third, both with no reference?
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3819
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Is the question about number of string objects or about the number of references?
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are explaining strings paul

They seem to be saying that the JVM never creates a 'rules' object, but does create a 'java rules' object that isn't referenced.

That seems like an odd explanation of strings to me, given that it at least looks like a string object is passed as an argument to the concatenation method. But maybe I'm confused.
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a few pages later they seem to agree with me (counting for example both "springfall" and "fall" on line 3 as objects). strange (or i'm still missing something):




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)


plus, i still disagree with their answer slightly. surely " " and the whole expression passed to println() are Strings 9 and 10?
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?


my mistake on the last example, was very tired!

anyway, that's good to know, thanks.

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.

oh well, it's still a great book
 
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 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 don't agree with you!

The example is discussing the immutability of String objects, not about how many String objects there are created. So if the explanation should discuss/explain everything what's going on, the explanation might become quite comprehensive and then you (as a reader) could miss the important stuff. So from an educational point of view, it's better to focus on what's actually important than explaining every possible detail.
From this perspective it makes sense that the explanation uses "second" (and not "third"), because the explanation tells you that on the first line the first String object is created. And as the code snippet (and explanation) is focusing on immutability and lost strings, the next line creates the second String object "Java Rules!".
If the explanation would be changed to
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.
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").

And if you really want to get very nitpicky: "Java Rules!" is the fourth String object being created in that code snippet. "Java", " Rules!" and "x = " are the first three String objects (all compile time constants) which are created And "x = Java" will be the fifth String object.

As you already noticed: if the book is discussing the actual number of String objects being created (as with the code snippet about the four seasons), the study guide is spot-on and doesn't miss any String object. Because it's important not to miss any String object in this scenario (every String object counts ), but when you are discussing String immutability and/or lost String objects, the actual number of String objects is less important.

Hope it helps!
Kind regards,
Roel

PS. when you are discussing some part of a study guide or other resource, it's always very useful to mention a page number or link of the statement/question/code snippet you have doubts/questions about. That's much easier for others to have a look at the original source and thus increase your chances of getting a meaningful answer. I'll add them for you: the code snippet about "Java Rules!" can be found on page 262 and the one about the four seasons on page 264.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic