• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

odd array signature

 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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[])
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,
Even the signature given below is valid

final public static void main(String[] args)

Cheers,
Sandeep
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic