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

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

    Explanation in simple layman's language.

    The main method expects us to provide some arguments when we direct our JVM to the class name. That means, suppose your file name is Try.java, now to execute this in command prompt you write "javac Try.java" to compile followed by "java Try" to execute. Now suppose instead of writing simply "java Try" you write "java Try 1". Here you have passed an argument "1". This will be taken by your main method even if you don't use it in your code.

    If you want to check whether your main method has actually taken the argument "1" or not. Simply, inside your main method type the following:

    for(int i = 0; i < args.length; i++) {
            System.out.println("Argument is: "+args[i]);
        }
    

提交回复
热议问题