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

前端 未结 17 1684
有刺的猬
有刺的猬 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:03

    The String[] args parameter is an array of Strings passed as parameters when you are running your application through command line in the OS.

    So, imagine you have compiled and packaged a myApp.jar Java application. You can run your app by double clicking it in the OS, of course, but you could also run it using command line way, like (in Linux, for example):

    user@computer:~$ java -jar myApp.jar
    

    When you call your application passing some parameters, like:

    user@computer:~$ java -jar myApp.jar update notify
    

    The java -jar command will pass your Strings update and notify to your public static void main() method.

    You can then do something like:

    System.out.println(args[0]); //Which will print 'update'
    System.out.println(args[1]); //Which will print 'notify'
    

提交回复
热议问题