• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

odd array signature

 
Joshua Smith
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm currently writing a lot of little code snippets as part of my study for SCJP 5. Many are correct. Many are incorrect, but I want to see how each of them behaves. I'm often surprized to find snippets that work, when I'm sure that they wouldn't. Here is one of them.



Oddly enough, this compiles fine and runs fine and gives the following output:
a
b
c
d

It behaves just as if it was String[][] args.

Does anyone have a good explanation of what's going on here? Why does it even compile?

Thanks,
Josh
[ January 15, 2006: Message edited by: Joshua Smith ]
 
Marcelo Ortega
Ranch Hand
Posts: 528
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joshua,

The method signature myMethod(String[] args[]) is telling you that it takes a 2d array of Strings as a parameter. Don't be fooled on how it looks, because:

myMethod(String[] args[])

is the same as:

myMethod(String[][] args)

which is the same as:

myMethod(String args[][])

and:

myMethod(String[] []args)

Conclusion:
The brackets can be on either side of the parameter name.

The following is NOT legal though:

myMethod([]String args[])

Regards, Marzo.
 
Tilo Hemp
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
interestingly,

myMethod(String [][] args)
myMethod(String [][]args)
myMethod(String[][]args)

also compile.

unfortunately, I did not find these in the language specification, so how should I answer to the hypothetical question:

"is this a vaild main method:
static public void main(String[]args)"

any ideas?
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These are all valid signatures for a main method:

public static void main(String[] args)
public static void main(String args[])

static public void main(String[] args)
static public void main(String args[])
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Tilo, comrade.

Java Language Specification Chapter 10: Arrays


The [] may appear as part of the type at the beginning of the declaration, or as part of the declarator for a particular variable, or both...


As to the proximity of the brackets to the variable, well, you have to remember that the brackets in this case are an operator. So you can do that for the same reason you can do...



...in all these cases the compiler identifies the operators and recognizes that they are not part of the variable identifier.
 
Tilo Hemp
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
:roll: hm, I guess I overlooked this... thanks for clarifying!
[ January 15, 2006: Message edited by: Tilo Hemp ]
 
Joshua Smith
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marzo-

Thanks. I knew that I could move the square brackets around. Although it's weird, it's legal. I guess I just assumed that you had to pick one location (with the type) or the other (with the variable name) and wasn't aware that you could split them. Thanks for the explanation. It makes sense.


Tilo-

Yes, this is legal, even without the spaces between the type, the square brackets and variable name. It compiles and runs fine as a main method.



All-

I also found few others that are fine for main as well:



Thanks for everyone's input.

Josh
 
Sandeep Prabhakar
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,
Even the signature given below is valid

final public static void main(String[] args)

Cheers,
Sandeep
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic