Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arrays.asList()

 
Dhana Sen
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a doubt in Java Arrays.asList() method. I try to pass Store = null, It's returning [null] (It's considering as null a value and size as 1), then I tried with Store = "" (it's considering [] (empty as a value) and size will be 1) Is there any flaw in asList Method.


 
Sita Marivada
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrays class has some bug in it. Sun has to fix that.
Be sure not to pass null values to Arrays.asList() while coding itself.

 
Mike Simmons
Ranch Hand
Posts: 3080
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This doesn't seem like a bug at all to me - the method is doing what you asked it to do. What did you think the result would be?

I can think of three possible responses to Arrays.asList(null):

1. Return a list with a null in it, length 1.

2. Return an empty list, length zero.

3. Return a null (calling length() woudl throw a NullPointerException)

It's not clear to me which of these might be what you want; all are potentially reasonable responses. But the first one seems the closest match to what your code actually says.
 
Sita Marivada
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would agree with the third result.

Return a null (calling length() woudl throw a NullPointerException)

 
Henry Wong
author
Marshal
Pie
Posts: 21123
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Plus... it is implement as a var-arg of generics. So, option 2 isn't really an option that would make sense.

Between option 1 and option 3, option 1 is definitely the better choice.

Henry
 
Mike Simmons
Ranch Hand
Posts: 3080
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I left out:

4. Throw an exception (NullPointerException or IllegalArgumentException) - this is what I am usually inclined to do in my own code, if it's not a library used by others. I often try to write my code such that null is not considered a valid option.

Hentry Wong wrote:Plus... it is implement as a var-arg of generics. So, option 2 isn't really an option that would make sense.

I'm not sure why the var-arg or generics would be a problem here. Can you elaborate? Thanks.

Sita: so, what if you called

and store1 = "Apple", store2 = null, store3 = "Android". What should the result be?

1. return a List of ("Apple", null, "Android")

2a. return a List of ("Apple", "Android")

2b. return a List of (null)

3. return a null

4. throw a NullPointerException
 
Henry Wong
author
Marshal
Pie
Posts: 21123
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:
Hentry Wong wrote:Plus... it is implement as a var-arg of generics. So, option 2 isn't really an option that would make sense.

I'm not sure why the var-arg or generics would be a problem here. Can you elaborate? Thanks.


Bad wording on my part. Sorry.

Basically, I meant, with var-args and generics, that the method will handle an array of Objects. And with processing the var-arg, the compiler's two choices are ... (1) treat the null as an array of objects, or (2) create an array of objects using the parameters. And option 2 can't happen with either option.

Henry
 
Mike Simmons
Ranch Hand
Posts: 3080
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I see. I was thinking of option 2 as creating a List of whatever reference(s) are given, but silently removing the nulls. This is seen in 2a later. I don't think it's a good idea (unless maybe you rename the method to asListWithNullsRemoved()), but it would certainly be possible to implement.

Note that as far as the compiler is concerned, the compiler doesn't "know" that the variable store is null. It doesn't remember that as it compiles. It just knows that store is of type String, and has been definitely assigned before the call to Arrays.asList(). So the compiler will necessarily construct a call as if there's a single String parameter - inserted into an array since it's a vararg.
 
Saurav Sen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interestingly, above code will through NPE if I do it as follows --
Arrays.asList(null);
But if I do it as --
String store = null;
Arrays.asList(store);
then it doesn't throw NPE instead creates an list with one string in it.

Definitely it is a bug with var-argument generic implementation in Java & certainly not a problem with Arrays.asList(T... a) implementation.
Explanation below --
1. var-arg generic is implemented at the level of compiler just like other features of generics. i.e. JVM has no concept of variable argument method & its existence.
2. the way it is implemented at the level of compiler is as follows --
Code [
String str1 = null;
String str2 = null;
String str3 = null;
Arrays.asList(str1, str2, str3):
]

Now if you open the byte code (i.e. decompiling) you will find that it is implemented in the following way --
bytecode [
String str1 = null;
String str2 = null;
String str3 = null;
Arrays.asList(new String[]{str1, str2, str3});
]

So, if you examine it carefully you can see that even though str1, str2, str3 are all NULL but at the time of execution Arrays.asList(....) doesn't fire NPE because it gets a new String[]{str1, str2, str3} array the element of which are NULL.
So, that is exactly is the reason why ---

Arrays.asList(null); throws NPE

but,
String store = null;
Arrays.asList(store);
doesn't thorw NPE.
 
Rob Spoor
Sheriff
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saurav Sen wrote:Arrays.asList(null); throws NPE

but,
String store = null;
Arrays.asList(store);
doesn't thorw NPE.

That's caused by the varargs. Arrays.asList(null) sees the null as the entire T[], not just one T element.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The behavior you observe has nothing to do with generics, and everything to do with the implementation of varargs and that "million dollar mistake" null.



The moral of this story is, do your best to avoid passing/returning nulls .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic