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

Arrays

 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you declare the following array:

Vehicle cars [ ];

or

Vehicle [ ] cars;

Is there any difference?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No difference. I think most people prefer

Vehicle[] cars;
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
I think most people prefer

Vehicle[] cars;
I prefer it that way as it's more clear that "[]" modifies the type of the variable "cars" and not the variable itself. It also seems less likely that someone will mistake it for a single Vehicle. Moreso, I writeso it stands out.

Having said that, this is probably near the bottom of my list of coding style conventions for which I'm a stickler.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use the <Class>[] <variable name> for all arrays except for the parameters to main -- that is always "public void main (String argv[])"

I have not discerened any logic to this; it's just the way the crossed-wires in my brain work.
 
Patrick Haley
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel, I do the same thing, and I know the reason. Because in my first Java class, I was told to burn into my brain because it must never ever change.

So I burned it into my brain. So it can never be (String[] args).

Sometimes, when I'm feeling loopy, I'll change args to argh or argv.
[ November 07, 2004: Message edited by: Patrick Haley ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic