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

K&B Chapter 5 - Self Test Question # 2

 
Brian Brumpton
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The questions states:
Given:

And, if the code compiles, the command line:

java Hilltop eyra vafi draumur kara

What is the result?

I want to make sure I'm analyzing the code correctly. The part that I'm questioning is line 4. Am I correct in my assumption that the assignment to null is irrelevant because it gets assigned a new reference in line 7? Is it merely included just for the purposes of confusion?
 
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
Brian Brumpton wrote:The part that I'm questioning is line 4. Am I correct in my assumption that the assignment to null is irrelevant because it gets assigned a new reference in line 7? Is it merely included just for the purposes of confusion?

As you probably know when you create an array, all the array elements are initialized with the default value of the array type. So for String, that's null. So after line3 has finished, horses[4] is already null. So the intent of line4 is probably more to add some confusion than having an impact on the array

Are you really sure line7 will assign a new reference to horses[4]?
 
Brian Brumpton
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you really sure line7 will assign a new reference to horses[4]?


Why you gotta mess with me Roel? No, it won't assign a reference. If I were a real Java developer, I would have known that array indexes are zero based and only index positions 0 through 3 would be assigned a reference.
 
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
Brian Brumpton wrote:Why you gotta mess with me Roel?

Because I like it, I like it a lot I already got a superhero name, maybe this could be my superpower

Brian Brumpton wrote:No, it won't assign a reference. If I were a real Java developer, I would have known that array indexes are zero based and only index positions 0 through 3 would be assigned a reference.

Spot-on!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic