This is a total beginners question. I am new to java and have been browsing StackOverflow and CodeReview. I am finding these two different formats being used:
public static void main(String args[])
or
example 2 :
public static void main(String[] args)
This is what I have in my course notes:
These words are called modifiers. The main() method is also preceded by the word void to indicate that it does not return any value. Also the main() method always has a list of command line arguments that can be passed to the program
main(String[] args)
which we are going to ignore for now.
So, as you can see, we have been told to ignore this for now, but I'd like to know:
Is there an actual difference between these, if so, what?
From the Java Language Specification :
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.
For example:
byte[] rowvector, colvector, matrix[];
This declaration is equivalent to:
byte rowvector[], colvector[], matrix[][];
Actaully there are no difference between two main-method
defination and both are correct.
But by convention java prefers array declaration as String[] args
rather than String args[]
.
So it is more conventional -
public static void main(String[] args){...}
main method accepts arguement in String array following ways are accepted
public static void main(String args[])
public static void main(String []args)
public static void main(String... args)
All these are valid declarations of the main function in Java.
public static void main(String[] args) {
// code
}
static public void main(String[] args) {
// code
}
static public void main(String args[]) {
// code
}
public static void main(String[] MarkElliotZuckerberg) {
// code
}
public static void main(String... NewYork) {
// code
}
These are examples of invalid main method declarations -
static void main(String[] args) {
// public is missing
}
public void main(String args[]) {
// static is missing
}
public static int main(String... Java) {
// return type not void
return 0;
}
public void Main(String args[]) {
// "main" not "Main"
}
public void main(string args[]) {
// "String" not "string"
}
public void main(String.. SayHi) {
// Ellipses is 3 dots !
}
I'm sorry if the source code is not soo readable... I always had trouble posting source code... :P ... Hope this helps...! If it did, let me know by commenting..!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.