Interesting call to main method
I was around Wikipedia reading about a programming language called 'D', it is the first time I read about it.
It was curious to me that the syntax looks very similar to Java. But most interesting was when I saw thei开发者_高级运维r main method uses char[][].
I opened up Eclipse and I tried this:
public static void main(char [][] args){
}
I was surprised when I saw it compiled with no syntax errors, but I did not understand why.
Can someone explain to me why this call to the main method can compile in Java?As what you have written is valid Java syntax (static void method with name main and as argument a two dimensional char array) it will compile. The problem however is, that this main method will not work as entry point to a Java program, as this has to have the signature:public static void main(String[] args)
.
dcn, is correct it is a valid method named main. But can't be used to start a Java app.
To expand further, there is some flexibility in the signature:
You can use any variable name, not just args
:
public static void main(String[] whateverNameYouWant) {
}
The variable must be an array of Strings, but you can declare that in any valid Java syntax, like so:
public static void main(String args[]) {
}
As, Michael Krussel, points out, you can also use varargs:
public static void main(String... args) {
}
精彩评论