本文共 5394 字,大约阅读时间需要 17 分钟。
main方法中args
public static void main(String[] args)
is the most important Java method. When you start learning java programming, this is the first method you encounter. Remember the first Java Hello World program you wrote that runs and prints “Hello World”?
public static void main(String[] args)
是最重要的Java方法。 当您开始学习Java编程时,这是您遇到的第一种方法。 还记得您编写的第一个运行并打印“ Hello World”的Java Hello World程序吗?
Java main method is the entry point of any java program. Its syntax is always public static void main(String[] args)
. You can only change the name of String array argument, for example you can change args
to myStringArgs
.
Java主要方法是任何Java程序的入口点。 它的语法始终是public static void main(String[] args)
。 您只能更改String数组参数的名称,例如,可以将args
更改为myStringArgs
。
Also String array argument can be written as String... args
or String args[]
.
同样,String数组参数也可以写成String... args
或String args[]
。
Let’s look at the java main method closely and try to understand each of its parts.
让我们仔细看看java main方法,并尝试理解其每个部分。
This is the access modifier of the main method. It has to be public
so that java runtime can execute this method. Remember that if you make any method non-public then it’s not allowed to be executed by any program, there are some access restrictions applied. So it means that the main method has to be public. Let’s see what happens if we define the main method as non-public.
这是main方法的访问修饰符。 它必须是public
以便Java运行时可以执行此方法。 请记住,如果将任何方法设为非公共方法,则不允许任何程序执行该方法,因此会应用一些访问限制。 因此,这意味着主要方法必须是公开的。 让我们看看如果将main方法定义为非公共方法会发生什么。
public class Test {static void main(String[] args){ System.out.println("Hello World"); }}
$ javac Test.java $ java TestError: Main method not found in class Test, please define the main method as: public static void main(String[] args)or a JavaFX application class must extend javafx.application.Application$
When java runtime starts, there is no object of the class present. That’s why the main method has to be static so that JVM can load the class into memory and call the main method. If the main method won’t be static, JVM would not be able to call it because there is no object of the class is present. Let’s see what happens when we remove static from java main method.
当Java运行时启动时,不存在该类的对象。 这就是为什么main方法必须是静态的,以便JVM可以将类加载到内存中并调用main方法的原因。 如果main方法不是静态的,则JVM将无法调用它,因为不存在该类的对象。 让我们看看当我们从java main方法中删除static时会发生什么。
public class Test {public void main(String[] args){ System.out.println("Hello World"); }}
$ javac Test.java $ java TestError: Main method is not static in class Test, please define the main method as: public static void main(String[] args)$
Java programming mandates that every method provide the return type. Java main method doesn’t return anything, that’s why it’s return type is void
. This has been done to keep things simple because once the main method is finished executing, java program terminates. So there is no point in returning anything, there is nothing that can be done for the returned object by JVM. If we try to return something from the main method, it will give compilation error as an unexpected return value. For example, if we have the main method like below.
Java编程要求每个方法都必须提供返回类型。 Java main方法不返回任何内容,这就是其返回类型为void
。 这样做是为了保持简单,因为一旦main方法执行完毕,java程序就会终止。 因此,返回任何东西都是没有意义的,JVM无法对返回的对象做任何事情。 如果我们尝试从main方法返回某些内容,它将给出编译错误作为意外的返回值。 例如,如果我们有如下所示的main方法。
public class Test {public static void main(String[] args){ return 0;}}
We get below error when above program is compiled.
当上面的程序被编译时,我们得到下面的错误。
$ javac Test.java Test.java:5: error: incompatible types: unexpected return value return 0; ^1 error$
This is the name of java main method. It’s fixed and when we start a java program, it looks for the main method. For example, if we have a class like below.
这是java main方法的名称。 它是固定的,当我们启动Java程序时,它将查找main方法。 例如,如果我们有一个像下面这样的类。
public class Test {public static void mymain(String[] args){ System.out.println("Hello World");}}
And we try to run this program, it will throw an error that the main method is not found.
并且我们尝试运行该程序,它将引发一个错误,即未找到main方法。
$ javac Test.java $ java TestError: Main method not found in class Test, please define the main method as: public static void main(String[] args)or a JavaFX application class must extend javafx.application.Application$
Java main method accepts a single argument of type String array. This is also called as java command line arguments. Let’s have a look at the example of using java command line arguments.
Java main方法接受String数组类型的单个参数。 这也称为java命令行参数。 让我们看一下使用Java命令行参数的示例。
public class Test {public static void main(String[] args){ for(String s : args){ System.out.println(s); } }}
Above is a simple program where we are printing the command line arguments. Let’s see how to pass command line arguments when executing above program.
上面是一个简单的程序,我们在其中打印命令行参数。 让我们看看在执行上述程序时如何传递命令行参数。
$ javac Test.java $ java Test 1 2 3123$ java Test "Hello World" "Pankaj Kumar"Hello WorldPankaj Kumar$ java Test$
Below images show how to pass command line arguments when you are executing a java program in Eclipse.
下图显示了在Eclipse中执行Java程序时如何传递命令行参数。
That’s all for public static void main(String[] args)
, java main method.
这就是java main方法的public static void main(String[] args)
的全部内容。
翻译自:
main方法中args
转载地址:http://rfqzd.baihongyu.com/