jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes odd array signature Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "odd array signature" Watch "odd array signature" New topic

odd array signature

Joshua Smith
Ranch Hand

Joined: Aug 22, 2005
Posts: 193
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:

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?

[ January 15, 2006: Message edited by: Joshua Smith ]

Rational Pi Blog - Java, SCJP, Dev Bits- http://rationalpi.wordpress.com
Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 528

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[][])


myMethod(String[] []args)

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

The following is NOT legal though:

myMethod([]String args[])

Regards, Marzo.


Live life to an interface, not an implementation!
Tilo Hemp
Ranch Hand

Joined: Nov 21, 2005
Posts: 91

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

Joined: Aug 24, 2005
Posts: 518
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

Joined: Dec 31, 2004
Posts: 961
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

Joined: Nov 21, 2005
Posts: 91
:roll: hm, I guess I overlooked this... thanks for clarifying!
[ January 15, 2006: Message edited by: Tilo Hemp ]
Joshua Smith
Ranch Hand

Joined: Aug 22, 2005
Posts: 193

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.


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.


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

Thanks for everyone's input.

Sandeep Prabhakar
Ranch Hand

Joined: Dec 22, 2005
Posts: 59
Even the signature given below is valid

final public static void main(String[] args)

Have you checked out Aspose?
subject: odd array signature
It's not a secret anymore!