What is the parameter "String args[]" in the main function in Java?

Asked 1 years ago, Updated 1 years ago, 140 views

When defining the main function public void main(String[] args) You do it like this. Here What is String [args] and what is this for?

java main parameter command-line-argument

2022-09-21 18:46

1 Answers

In Java, args is an array of String objects in the command line. When you run Java on the command line, you can use the parameters It's used to support the delivery. For example, in the command line,

If you run java MyProgram one two, the args will contain ["one"",two"]. If you want to check it out, turn the repeat statement and look at the output.

public class MyProgram {
    public static void main(String[] args) {
        for(int i = 0; i < args.length; i++) {
            System.out.println(args[i]);
        }
    }
}


2022-09-21 18:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.