简体   繁体   English

正确的main方法语法

[英]Correct syntax for main method

This is a total beginners question. 这是一个初学者问题。 I am new to java and have been browsing StackOverflow and CodeReview. 我是java新手,一直在浏览StackOverflow和CodeReview。 I am finding these two different formats being used: 我发现使用了这两种不同的格式:

example 1 : 例1

public static void main(String args[])

or 要么
example 2 : 例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. main()方法前面还有单词void,表示它不返回任何值。 Also the main() method always has a list of command line arguments that can be passed to the program main()方法总是有一个可以传递给程序的命令行参数列表

main(String[] args) 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 : Java语言规范

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. 实际上,两种main-method定义之间没有区别,两者都是正确的。

But by convention java prefers array declaration as String[] args rather than String args[] . 但按照惯例,java首选数组声明为String[] args而不是String args[]

So it is more conventional - 所以它更传统 -

public static void main(String[] args){...}

main method accepts arguement in String array following ways are accepted main方法接受以下方式接受的String数组中的争论

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. 所有这些都是Java中main函数的有效声明。

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
}
  • The keywords public and static are interchangeable but mandatory. 关键字public和static是可互换的但必须的。
  • The parameter of the main method can take the var-args syntax. main方法的参数可以采用var-args语法。
  • The name can be anything..! 这个名字可以是任何东西..!

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...! 如果源代码不太可读,我很抱歉......我总是在发布源代码时遇到麻烦...:P ...希望这有助于......! If it did, let me know by commenting..! 如果有,请通过评论告诉我..!

Source - Java Tutorials on Theory of Programming 源程序 - Java程序设计理论教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM