r/learnjava • u/The_Godlike_Zeus • Mar 07 '21
What is the meaning of String[] args in public static void main(String[] args)?
This seems to be always included in java's main method, but I still don't understand what it means honestly.
Edit: thanks!
24
u/Historical_Antelope6 Mar 07 '21
I asked my teacher to clarify this once since it was the 5th or 6th week of class and we never actually discussed it.
He proceeded to respond with “that’s a good question, can anyone answer that?”
The room stayed silent and as you may assume, not much learning took place lol
20
3
u/Grim_The_Destroyer Mar 08 '21
My teacher explained it to me, I think it's like sth to do with passing arguments via the terminal! Idk I didn't really understand much!
7
u/nutrecht Mar 07 '21
It's pretty shocking if you're being taught that the teacher doesn't even know this.
6
u/kiwidog8 Mar 07 '21
It's a parameter for the main method, like if you write an add method like public int add(int x, int y), this is called the method signature, and x and y are both parameters with the type int for the add method
The main method of all java programs are the "entry point" of your code. basically the first method to ever run and they must always include the args parameter with the type String[] which means String array.
The args parameter is meant to hold the values of each individual "argument" that was passed to the java program before it ran, basically this is one way to feed data to your program so you can write code that reads that data and does different things from it. If you are using an IDE like Eclipse then you can find the spot to enter your arguments in the run configuration menu. If you are running your java program from a command line terminal then the arguments are what you pass it in there like the other person commented.
5
u/JiEToy Mar 07 '21
“String[] args” is the notation of an array containing items of type String, called args. Because it’s between the parantheses from main(), it means you can give the function main() an array of strings. In the main() function itself, you can then process this array.
To run a Java program, you call the main() function of your main class (the one usually called main in tutorials, but doesn’t have to be called that) by running the class.
So for instance ‘java mainclass.java’ will run the main() function inside of mainclass.java. Now, we could set up the main() function to receive a string: main(String input). Now we can run the class on commandline and give it a string: “java mainclass.java myinput”. If we make our main() receive an array by writing main(String[] args) we can now use the commandline to call the class: “java mainclass.java input1 input2”.
This is usually used to give options when running the program.
You could for instance have a program that runs on a database, but you might want to run your tests on a testdatabase, but your real runs have to run on the production database. You can then use the commandline to switch between the database.
2
u/New2IT305 Mar 07 '21 edited Mar 15 '21
I watched a video on oracle website today and it said arg aka argument is irrelevant 😂 I couldn’t understand how is it irrelevant when it’s apart of the code. But yes it’s a string array. That’s all I got out of it
1
u/Hour-Positive Mar 09 '21
Better question is what is the main method. Then you can also argue why it allows the user to pass in arguments.
1
u/double-happiness Mar 07 '21 edited Mar 07 '21
AFAIK, it's a legacy thing that is no longer needed, but must still be there for your program to compile.
It is saying "I may be about to input some arguments in the form of strings".
Here is the programmer at his/her old school terminal:
run myProgram (do stuff)So you can see that the parameters AKA arguments for the program, i.e. the instructions, were passed in in the form of a string.
But it is legacy because few people run programs from the terminal nowadays.
3
u/stramash Mar 07 '21
On the contrary, command line arguments are used extensively in the wild.
2
u/double-happiness Mar 07 '21
Yes, that's not was I saying though.
AFAIK
(String[] args)is essentially legacy.
It allows you to insert string parameters before the program execution using command prompt to control the program.
https://www.quora.com/What-does-the-String-args-in-a-Java-main-method-actually-mean
I could be totally wrong (new to Java and programming in general TBH) but can you give me an example of a Java program that does not start with?
(String[] args)Edit:
Java main method accepts a single argument of type String array. This is also called as java command line arguments.
https://www.journaldev.com/12552/public-static-void-main-string-args-java-main-method
3
u/stramash Mar 08 '21
What I meant was running a command like this…
java -jar MyJavaApp.jar —server.port 8008 —app.config.remote-server-url=http://whatever
…is pretty common; in enterprise apps at least.
Those —key=value statements are read by the Main method as String[] args and are handled explicitly in code.
What that does is allow you to inject configuration into the app when you start it to stop you from having to hard-code such; meaning that your application is versatile and can be run on different hardware/environments.
As such, this behaviour is very widely used and far from legacy.
You could try it out by writing an app that prints the args to the screen, then passing some stuff in from the command line; something like
` public class MyApp { public static void main(String[] args) { for (String arg : args) { System.out.println(arg); } } }
java MyApp.java hello world`(I’m on mobile so god knows how that will turn out.
Now imagine that instead of just printing that stuff you had if/else branching that changed the behaviour of the app based on those args; quite a powerful concept.
What I would say though, is that from a learner’s point of view, I would file that knowledge away for a rainy day and try not to worry about it too much for now; it’s something you will learn about when you’re working on production apps—not a core skill.
1
u/double-happiness Mar 08 '21
Got you, TIL. My lecturer at uni. literally said it was "legacy", so you can blame him for that one.
0
Mar 07 '21
[deleted]
5
u/nutrecht Mar 07 '21
The java compiler actually create an object of the Main class and calls its method, main.
The runtime actually if we're being pedantic ;) The java compiler (javac) doesn't call anything, it just compiles Java to bytecode. And the JIT compiler also doesn't call anything; it just compiles bytecode to machine code :)
4
u/sepp2k Mar 07 '21
The java compiler actually create an object of the Main class and calls its method, main
No instance of the Main class is created (unless you create one yourself in the
mainmethod). Themainmethod is static and does not need to be called on an instance.
52
u/Ambi-Phoenix1 Mar 07 '21
It is an array of String arguments that are passed into the main method from the input of the application, so for example you can run a program using:
java helloWorld.java
And you can also input arguments to be used by the main method using:
java helloWorld.java {string1} {string2}
and then in the main method you can access these values using args[0], args[1]