• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

main(String[]args)

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys, i want to know exactly what (String[]args) do. I know that we can use (String[]args) to pass command line arguments. But if i just write a simple program like,



and dont pass any command line arguments, does this program use (String[]args) at all?

What purpose does (String[]args) serve other than accepting command line arguments?

Is (String[]args) important for the execution of a program(if you dont use command line arguments)?

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you don't pass any arguments on the command line, then args will be an array of length 0. Note that you must always have the String[] args when you specify the main method, because Java expects it that way - it looks for a public static method with the name main that takes a String[] as the argument. It's ofcourse not required that you do anything with the arguments. If you don't use them, then simply nothing happens with them.

It doesn't have any other purpose than passing in command line arguments.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it will not be used, but you still have to have it there. The jvm is EXPLICITLY looking for a method with that signiature. So if you wrote

public static void main(){...}

and didn't have the "String [] args", the jvm would not be able to find the 'entry point' into your program, and would complain.

Note that the variable name can be anything, but 'args' is used by convention. I believe the args array will be created as an array with a length of 0, so you can test "if args.length() < 1" and not worry about a NPE.

 
Oceana Wickramasinghe
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone
 
I am not young enough to know everything. - Oscar Wilde This tiny ad thinks it knows more than Oscar:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic