• Post Reply Bookmark Topic Watch Topic
  • New Topic

Varargs and null  RSS feed

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quick question because I'm lazy:

If you have a method
void doSomething(T... args) { ...
and invoke it with
doSomething(null);
how does the compiler interpret it? As a null array, or a one-element array with a value of null?

The first would seem more consistent, because then
void doSomething(T... args) { ...
and
void doSomething(int... args) { ...
could be handled the same way; but it would seem to preclude
doSomething(null, null);
for the first case, unless it deals with two or more nulls differently.

I can't find anything explicit in the JLS (or maybe I'm just looking in the wrong place), so I was wondering if anyone had the answer at their fingertips.

As I say: just lazy. I will test if I have to.

Winston
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh you lazy!


This prints:
Array is null

If you want pass a single null argument you should use:

It's in JLS SE7: 15.12.4.2. Evaluate Arguments:
If m is being invoked with k ≠ n actual argument expressions, or, if m is being invoked with k = n actual argument expressions and the type of the k'th argument expression is not assignment compatible with T[], then the argument list (e1, ..., en-1, en, ..., ek) is evaluated as if it were written as (e1, ..., en-1, new |T[]| { en, ..., ek }), where |T[]| denotes the erasure of T[].


An array is created only if you can't assign a vararg parameter to the vararg type. So in my case... null is valid value for String[] so no array is created.
If I executed foo("a"), the "a" String reference wouldn't be legal value for String[] so an array would be created.

And I based my explanation on this ;)
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting to try out: What does it look like when you pass no arguments at all: doSomething() - do you get a null array or an array with zero elements?
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And it prints: “April Fool!”
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:It's in JLS SE7: 15.12.4.2. Evaluate Arguments:

Aha! I thought I was probably looking in the wrong place (it would be nice if they had some forward links in sections 8 and 13). Cheers for that.

So it would appear to be solely dictated by the number of arguments (which does make sense); so if I supply two nulls, it will take them as values. Makes sense to me (now).

Thanks again...and yeah, I know I'm lazy. Got the link though, so maybe smart crazy...

And a Happy April Fools Day to all.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!