简体   繁体   English

为什么 java 中的主要方法必须是(字符串。[]args)?

[英]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.

相关问题 为什么 Java main() 方法接受 String args 数组? - Why does Java main() method accept an array of String args? java中的main方法为什么接受无效的String args - main method in java why Accept invalid String args 什么是“字符串参数 []”? 主要方法中的参数 Java - What is "String args[]"? parameter in main method Java 公共void main(String [] args)是无效的Java main方法签名吗? - Is public void main(String[] args) Invalid java main method signature ? 为什么我的 java 程序在没有 eclipse 中的 main(String[] args) 方法的情况下运行? - Why does my java program run without the main(String[] args) method in eclipse? 除了'string [] args'之外,java的main方法中还有其他参数吗? - Is there any other parameter in the main method of java other than 'string[] args'? Java初学者关于主方法中String [] args的问题 - Java Beginner question about String[] args in the main method String [] args与main方法中的String…args有何不同? - How String[] args different from String… args in the main method? java Hello World 错误:在类名中找不到主方法,请将主方法定义为:public static void main(String[] args) - java Hello World Error: Main method not found in classname, please define the main method as: public static void main(String[] args) JavaFX没有调用main(String [] args)方法 - JavaFX not calling main(String[] args) method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM