• 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

Doubt in main method

 
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was wondering if we won't pass any value to the main method's String[]args argument, why wouldn't args.length throw NullPointerException and instead return 0 as the result.

With command line invocation:

java test



As, noramlly:

Will throw NullPointerException because String[]a is get to null by default.
But why isn't String[]args set to null?
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you pass into the main method? String[] ... this is an array of Strings. So when you call your programs with or without parms, these are the strings in position 0, 1, 2 etc.

If NPE is to happen then the actual string array is null which I think is not possible.
 
Sidharth Khattri
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

K. Tsang wrote:What do you pass into the main method? String[] ... this is an array of Strings. So when you call your programs with or without parms, these are the strings in position 0, 1, 2 etc.

If NPE is to happen then the actual string array is null which I think is not possible.



Is something still passed to main method by default even if we don't explicitly pass anything from this command line invocation?

java test

 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes an empty array hence "java test" will output 0 for args.length
 
Sidharth Khattri
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

K. Tsang wrote:Yes an empty array hence "java test" will output 0 for args.length



Thanks for confirming. I couldn't find it anywhere else.
 
Ranch Hand
Posts: 68
MyEclipse IDE PHP Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does it pass an empty array and not "null" as is the value when initialized? Null would be more logic to me
 
Sidharth Khattri
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

timo corn wrote:Why does it pass an empty array and not "null" as is the value when initialized? Null would be more logic to me



That's what I thought, null would be more logical. Apparently, args.length returns 0, hence empty String array is passed. Though I couldn't find the answer anywhere, as for why? Maybe it is designed that way. Anyone with an answer?
 
author
Posts: 23949
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sidharth Khattri wrote:

timo corn wrote:Why does it pass an empty array and not "null" as is the value when initialized? Null would be more logic to me



That's what I thought, null would be more logical. Apparently, args.length returns 0, hence empty String array is passed. Though I couldn't find the answer anywhere, as for why? Maybe it is designed that way. Anyone with an answer?



Short answer. That is how it is specified (although arguably, the way that it is written in the JLS can be interpreted differently). And that's how it has been done since Java 1.0 -- release back in late 1994.

Longer answer. Only the original designers of Java are likely to know why it was designed that way. And I don't think that there are any currently at the ranch. Sorry.


And BTW, I don't understand how null would be more logical. Doesn't that mean that you will have to code a special case for no command line arguments?

Henry
 
Sidharth Khattri
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Sidharth Khattri wrote:

timo corn wrote:Why does it pass an empty array and not "null" as is the value when initialized? Null would be more logic to me



That's what I thought, null would be more logical. Apparently, args.length returns 0, hence empty String array is passed. Though I couldn't find the answer anywhere, as for why? Maybe it is designed that way. Anyone with an answer?



Short answer. That is how it is specified (although arguably, the way that it is written in the JLS can be interpreted differently). And that's how it has been done since Java 1.0 -- release back in late 1994.

Longer answer. Only the original designers of Java are likely to know why it was designed that way. And I don't think that there are any currently at the ranch. Sorry.


And BTW, I don't understand how null would be more logical. Doesn't that mean that you will have to code a special case for no command line arguments?

Henry



Unless we make use of String[]args in a program, it won't throw any Exception on invocation with no values. If we really want to use it, we'd pass some values. So, I as per the default rule of class variables declaration or runtime initialization, it should be null if not initialized. Anyways, they made it that way and are not here to answer why.
 
Eliminate 95% of the weeds in your lawn by mowing 3 inches or higher. Then plant tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic