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
posted
0
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
posted
0
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: 35443
9
posted
0
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
posted
0
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: 35443
9
posted
0
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.
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
posted
0
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.
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.
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.