• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Main method argument problem

 
sharma ishu
Ranch Hand
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Rancher
Pie
Posts: 42966
73
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're assuming that s is null if there are no arguments - how did you come by that assumption?
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic