[英]Why main method in java has to be (String. []args)?
I am confused on why main method on java has to be:我很困惑为什么 java 上的主要方法必须是:
public static void main(String[] args)
My code is我的代码是
public class MergeSorted
{
public static void main (int[] nums1, int m, int[] nums2, int n)
{
----
}
}
Will executing >>java MergeSorted [2,4,5,0,0,0],3,[1,3],2;将执行 >>java MergeSorted [2,4,5,0,0,0],3,[1,3],2; help?
帮助?
In Java the main method gets noticed by these exact method signature.在 Java 中,主要方法被这些确切的方法签名注意到。 The
String[] args
are the arguments the program started with. String[] args
是程序开始时的 arguments。 So when you compile your program, goto the command line execute your program you can put parameters behind the "start command"因此,当您编译程序时,转到命令行执行您的程序,您可以将参数放在“启动命令”后面
For example例如
java -jar x.jar --myflag
^ args...
Then your String array is going to contain "--myflag", you can process those arguments to get the information that you want from it..然后您的字符串数组将包含“--myflag”,您可以处理这些 arguments 以从中获取您想要的信息。
You have to have it as you will run the program as a command line application.您必须拥有它,因为您将把程序作为命令行应用程序运行。 It it is javax application would be requiring a main method.
它是 javax 应用程序将需要一个 main 方法。
MergeSort.java合并排序.java
public class MergeSort {
public static void main(String[] args) {
// get input via input stream
mergeSort(...); // call method
}
public static void mergeSort(int[] nums1, int m, int[] nums2, int n) {
// logic here
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.