• 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

Varargs and null

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ;)
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And it prints: “April Fool!”
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
It runs on an internal combustion engine. This ad does not:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic