Java public static main ()

I am learning Java, this is the only thing I do not understand.

in the main procedure:

public static void main(String[] args) { 

I think I pretty much understand this, in a language that I know, I think it will be like this:

 public static function main(args:String):void { 

The first thing I don’t understand is that 2 brackets [] for in String []? And the second thing that interests me is if this is the first function to be called (and called by something outside the program), will the parameter ever really be passed?

Thanks.

+4
source share
5 answers

The main arguments are parameters that you pass to Java from the command line, passed as an array. For example:

 java MyProgram foo bar zoo 

takes three arguments: foo, bar and zoo

foo is args [0], bar is args [1], and zoo is args [2].

+13
source

Brackets mean array . For instance. String[] is an array of strings. main() function is the first function called in your program. It is called by the JVM .

The values ​​in String[] args are the parameters passed on the command line.

If you call a Java program (main class: FooBar in the foo.bar package) as follows:

 java foo.bar.FooBar foo bar buz 

then args will like it if you built it like this:

 String[] args = new String[3]; args[0] = "foo"; args[1] = "bar"; args[2] = "buz"; 

It might be worth a read: Take a closer look at the Hello World app

+5
source

Brackets mean it's an array of strings. And there may be parameters, for example. from the command line when starting the application.

+1
source

This means that you get an array of strings. They can be transmitted via the command line.

0
source

[] denotes an array, for example String x = "some value"; String [] x = {"value 1", "value 2", "value 3"};

therefore, in the second case, x [0] gives "value 1". This is basically an array of strings. The second part - who will call the function? Well, this method signature is an input signature and whenever you try to call a class using a java program, it will look for this function to start execution; if he does not find him; he will just throw an error.

Who will pass the vals values ​​to the String [] array? java someprogram value1 value2 value3

will automatically fill the array with the corresponding three values. Thus, basically the values ​​are populated when they are launched from the command line, and the values ​​are passed as parameters for the call.

Hope this clears

0
source

All Articles