简体   繁体   English

main()和main(String args [])之间有什么区别

[英]Whats the difference between main() and main(String args[])

I am a biggner in java. 我是Java的biggner。 I am going well with java. 我与Java进行得很好。 The problem is when we declare main function in java as main(String args). 问题是当我们在java中将main函数声明为main(String args)时。 I am learning with bluej. 我正在与bluej学习。 It worked fine if I just write main(). 如果我只写main(),它就可以正常工作。 So what's the difference between both. 那么两者之间有什么区别。

public static void main(String[] args is the entry point (which can be final or not, doesn't matter) that the java tool and standard IDEs and such look for in the main class of a Java application. If you don't include the parameter declaration (the String[] args ), the signature doesn't match the expectation of the java tool and so may not work. public static void main(String[] argsjava工具和标准IDE等在Java应用程序的主类中寻找的入口点(可以是final ,也可以不是,这无关紧要)。 t包含参数声明( String[] args ),签名与java工具的期望不符,因此可能不起作用。

main() will compile , because it's just a method, but won't work with the java tool and other tools following its conventions. main()将进行编译 ,因为它只是一个方法,但是不能与遵循其约定的java工具和其他工具一起使用。

If BlueJ allows you to leave off the parameter declaration, that's behavior specific to the BlueJ tool. 如果BlueJ允许您省去参数声明,则该行为特定于BlueJ工具。

So for instance, this compiles just fine: 例如,这样编译就可以了:

public class Example {
    public static void main() {
        System.out.println("Hi");
    }
}

It compiles to an Example class with a method called main . 它使用名为main的方法编译为Example类。 But if you try to run that via the java tool: 但是,如果您尝试通过java工具运行该代码,请执行以下操作

$ java Example
Error: Main method not found in class Example, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

To make it compatible with the java tool, you need the parameter. 为了使其与java工具兼容,您需要参数。

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

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