wood burning stoves 2.0*
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "odd array signature" Watch "odd array signature" New topic
Author

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:
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 ]

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

Joined: May 31, 2005
Posts: 519

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.


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJD, SCEA/OCMJEA

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

Joined: Nov 21, 2005
Posts: 91
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

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
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

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

final public static void main(String[] args)

Cheers,
Sandeep
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: odd array signature