Win a copy of Programming with Types this week in the Angular and TypeScript forum
or The Design of Web APIs in the Web Services forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Why does main method need to have an argument of (String[] args) ?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I started learning java, and i'm not using an IDE because the book didn't recomment it for now.
While writing my first code using textEditor and cmd, I have to declare (String[] args) in main method.
Why? what is the need of this? and what does it do?
 
lowercase baba
Posts: 12782
51
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's a way to get arguments into the program from the command line.  if your class with the main() method is called "foo", you could do this on the command line:

>java foo argument1 argument2 argument3

with as many arguments as you want.  The all get stored in that "args" array, which you can then pull apart and used the values in your program.  
 
Marshal
Posts: 66975
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

The main() method is called by the JVM when it starts up; it is the first code you actually run. Your program always starts with the main method, and the Java Language Specification (=JLS) gives you two recommended forms for the method header. You are allowed slight changes; for example public static void main(final String[] args) will work, but any major changes will cause your program to fail to run. Our FAQ makes some useful suggestions about the use of the main method.
 
Amol Araragi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

fred rosenberger wrote:it's a way to get arguments into the program from the command line.  if your class with the main() method is called "foo", you could do this on the command line:

>java foo argument1 argument2 argument3

with as many arguments as you want.  The all get stored in that "args" array, which you can then pull apart and used the values in your program.  



Thank you, so it basically allows us to use arguments in cmd?
 
Amol Araragi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

The main() method is called by the JVM when it starts up; it is the first code you actually run. Your program always starts with the main method, and the Java Language Specification (=JLS) gives you two recommended forms for the method header. You are allowed slight changes; for example public static void main(final String[] args) will work, but any major changes will cause your program to fail to run. Our FAQ makes some useful suggestions about the use of the main method.



Thank you
 
fred rosenberger
lowercase baba
Posts: 12782
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Amol Araragi wrote:, so it basically allows us to use arguments in cmd?


exactly.  remember, "main" is a function, just like every other function.  it just happens to be the one the JVM calls to start your program.  So, as a function, it takes a String Array as its argument.  the array can be empty, or can have as many arguments as you want/need.  What you do with them, or how you use them, is entirely up to you.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred -

Thank you so, so much for your response.  I have been wondering this for a while.  I started CodeAcademy and it stated that it would explain this later in the course.

Now, I don't have to wait to get there to know why the signature is as such.

Again, thank you!
 
Campbell Ritchie
Marshal
Posts: 66975
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Amol Araragi wrote:. . . allows us to use arguments in cmd?

Yes, as did a similar construct in the earlier language C. As Fred ahs told you already, you can use the command‑line arguments or ignore them and it is up to you. I suggest you should be careful about command‑line arguments because your users might not know what they mean. The following is one possible solution:-
 
Ranch Foreman
Posts: 89
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although it's already explained and I don't know if TO will read this, here's some additional basic info for start learning Java:

This question, along with many others, are what I like to group up as "Because!" questions. Whenever you come up with a "Why ...?" question try this: "Why not/different?" and often the answer is: Because someone decided so for some reason - take it as given.
Same goes for main:
- why it has to be static: cause it's called without an instance of your class (an Object)
- why has it to be public: so it can be called from another spot not part of your Class
- why has it return type void: cause someone decided that a non-0 return code has to be done different than in C
- why has it an array of String as it's parameter: cause someone had the convinient idea to pass the arguments as this type as it makes it easier to handle them
May there were several devs involved in design main - and may there were some important things to decide for or against some of what's ended up the final result - and sure someone might know all these - but for the all-day dev it's enough to take main as given as it is because someone thought it to be a good idea to be it the way it now is. You will encounter a lot of these situations in your Java career - and unless someone has a good understanding to explain to you why something is the way you encounter you often have to live with: Because!

Another example: I'm into crypto stuff and once recommended to the dev of a lib to change something cause it was a mess to use it the way it was. I got the info, that it can't be changed as it would break type dependencies - but he understood that it was inconvenient to use in its current state. So he ended up by adding a very convenient helper method do the some job but hide some of the ugly conversions made it easier. Root cause: cause it never was planed to use it the way I did - although it was a common usecase noone reported yet.

@Fred: In Java its called methods and parameters, not functions and arguments. Sure it's the same, but not normal "Java dev slang".
 
Saloon Keeper
Posts: 21437
143
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why?

Because people learned very early on that it was convenient to be able to pass variable information into a program as part of the command line instead of simply starting the program and then feeding options to it as a separate operation. So command shells were created to locate the program to be executed and - as a convenience - to break out command line options as separate strings. Or at least the Unix shells did. In IBM mainframe batch command processing, the whole option thing came as one big string and the application had to break out the pieces itself. As did some of the early PC OS's.

Because the old-time programming languages were generally not very smart, they required explicit information. So the standard main program definition to this day in C is like so:


The argc was the argument count  and was equal to the number of elements in the argument vector (argv), which was an array of strings. An explicit argc was required because to this day C lacks the ability to determine how many elements are in a dynamic array. Oh, and argv[0] was the command itself, often as a fully-qualified path name. This allowed the same program to be stored under several aliases and act differently based on what name you called it by. It was also a good way to make sure you were executing the right copy of a program!

Java was originally designed on Unix (SunOS), and they knew a Good Thing when they saw it, so they continued in the same tradition. Except that the arguments were converted to Java String objects and passed in a Java array object. Which means that argc isn't needed explicitly, since it's the length attribute of the argv array. Unlike C, Java does know how big its arrays are.
 
No more fooling around. Read this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!