File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Main method argument problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Main method argument problem" Watch "Main method argument problem" New topic
Author

Main method argument problem

sharma ishu
Ranch Hand

Joined: Sep 10, 2012
Posts: 70
[Added code tags - see UseCodeTags for details]

Here the code is run without any arguements.

Please explain the reason for this difference in the output.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
You're assuming that s is null if there are no arguments - how did you come by that assumption?


Ping & DNS - updated with new look and Ping home screen widget
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

Note that there's no possible way line 5 can print "true". Because if s was null then line 4 would have thrown a NullPointerException.
sharma ishu
Ranch Hand

Joined: Sep 10, 2012
Posts: 70
Ulf Dittmer wrote:You're assuming that s is null if there are no arguments - how did you come by that assumption?

Then what does it has if it is not null?
sharma ishu
Ranch Hand

Joined: Sep 10, 2012
Posts: 70
Matthew Brown wrote:Note that there's no possible way line 5 can print "true". Because if s was null then line 4 would have thrown a NullPointerException.

This is what it printed. s.length is 0. Why is s not null as no argument has been passed to it.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
What makes "null" a better value than a zero-length array if there are no parameters? If "s" has as many elements as there are parameters, then it should be a zero-length array if there are no parameters.
sharma ishu
Ranch Hand

Joined: Sep 10, 2012
Posts: 70
Ulf Dittmer wrote:What makes "null" a better value than a zero-length array if there are no parameters? If "s" has as many elements as there are parameters, then it should be a zero-length array if there are no parameters.


Please don't tell what should be. I request you to clearly state what is happening here.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
I'm not sure what's unclear about that ... ?

There are zero parameters, hence a zero-length array is passed in. You think that null would be a better value than a zero-length array, and I'm trying to find out why you think that.
Mike Okri
Ranch Hand

Joined: Jun 22, 2011
Posts: 83
sharma ishu wrote:Why is s not null as no argument has been passed to it.

The JVM initializes s to a zero length array (not null) if there are no command line arguments.
sharma ishu
Ranch Hand

Joined: Sep 10, 2012
Posts: 70
Ulf Dittmer wrote:I'm not sure what's unclear about that ... ?

There are zero parameters, hence a zero-length array is passed in. You think that null would be a better value than a zero-length array, and I'm trying to find out why you think that.

Because normally arrays are initialized to null by default if they are declared as a class variable or not initialized at all if declared as local variable. Nothing is initialized to a zero length array by default except this.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

It's a parameter, not a variable. It's set by whatever is calling main(). So trying to comparing it to initializing a variable is comparing apples to teddy bears.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

And because the value is being specifically set by whatever is calling main (the JVM), it makes sense for them to have chosen the most convenient value.

It's common to have to check the length of the array to see if enough arguments exist for your particular program. But if the argument can be null you first have to check if it's null or checking the length will throw a NullPointerException, which is a pain.

This is actually a pretty general point. Whenever you've got a return method or a parameter for a method that uses some form of collection (whether a collection class or an array), it's almost always better to use an empty one to indicate a lack of items, rather than using null.
 
jQuery in Action, 2nd edition
 
subject: Main method argument problem
 
Similar Threads
difference between null string and ""
Need help on below code
When a final is not a constant
Check String for null without error?
how the execution happens?