What is “String args[]”? parameter in main method Java

前端 未结 17 1745
有刺的猬
有刺的猬 2020-11-21 23:58

I\'m just beginning to write programs in Java. What does the following Java code mean?

public static void main(String[] args)
  • What

17条回答
  •  情书的邮戳
    2020-11-22 00:27

    When you finish your code, you will turn it into a file with the extension .java, which can be run by double clicking it, but also throughout a console (terminal on a mac, cmd.exe on windows) which lets the user do many things. One thing is they can see console messages (System.out.print or System.out.println) which they can't see if they double click. Another thing they can do is specify parameters, so normally you would use the line

    java -jar MyCode.jar
    

    after navigating to the folder of the program with

    cd C:My/Code/Location
    

    on windows or

    cd My/Code/Location
    

    on Mac (notice that mac is less clunky) to run code, but to specify parameters you would use

    java -jar MyCode.jar parameter1 parameter2
    

    These parameters stored in the args array, which you can use in your program is you want to allow the user to control special parameters such as what file to use or how much memory the program can have. If you want to know how to use an array, you could probably find a topic on this site or just google it. Note that any number of parameters can be used.

提交回复
热议问题