Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array placeholders

 
Jinny Morris
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose I have an array containing elements that are related to its indexes, but I don't need (won't ever have to) access all of its elements. For example, I may never need to access the zeroth element. Are there any "rules of thumb" for when to use any one of these in preference to the others:









Thanks!
 
Marilyn de Queiroz
Sheriff
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wonder if the second choice will even compile.
The advantage of the third over the first is that you avoid creating a String object. Any time you can avoid creating extra unnecessary objects, you're better off.
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Jinny. This question sounded a little familiar so I did a quick search on "placeholder".


And now I know why it sounded familiar (and why I like this question so much)...

...I asked it myself some time ago, he he.
 
Jinny Morris
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the second choice will compile and run; I've used it. The reason I asked the question is that the instructor's solution for one of the "java in general" assignments uses 2 arrays: the first choice is used in one of the arrays and the third choice is used in another of the arrays. So I wondered ...
 
Marilyn de Queiroz
Sheriff
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens if you use the 3rd choice in the array that the instructor uses the first choice in?
 
Marilyn de Queiroz
Sheriff
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jinny Morris:
Yes, the second choice will compile and run; I've used it.


Ah, yes. After thinking about it some more, I can see that it would.
 
Jinny Morris
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's C-ish or VB-ish or something like that - nicely convenient, in any case (although perhaps not as "readable" as null?)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic