aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question of Arrays.asList() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question of Arrays.asList()" Watch "Question of Arrays.asList()" New topic
Author

Question of Arrays.asList()

vijay saraf
Ranch Hand

Joined: Jan 08, 2005
Posts: 141
Hello all,

Here i have a question regarding asList() Method of Arrays class

Here is the source of Question



if we Look at the Method asList() it is as follow



I just wanted to confirm the following line
if we pass primitive array to the method asList the 'T' would be treated as Object if it is so then why following line gives error...



is there any one who can clarify the things...



Thanks
Vijay Saraf.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Okay, lets start with a basic example here of why List<? extends Integer> doesn't work. Let's take a look at an example



So now we know that if you pass an array by yourself to a var-arg method of a generic type, then the generic type represents that array type. So in our case, T represents an int[] and not Integer. So if you modify your statement to this



Then it will work as now T (in asList method of ArrayClass) will represent Integer and not int[]...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
And, to put a little twist on what Ankit said, try doing this:



All code in my posts, unless a source is explicitly mentioned, is my own.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Well Ruben, that worked because generics don't work on primitives. When you pass an int array into a normal int vararg method, then it works fine



Here the output would be 1. But if you put generics into play, then a generic vararg cannot accept an int[] as is. So it uses the int[] as an Object and creates a new array of objects with 1 size i.e. it creates an object array like this



So this is now an object array of 1 size. That's why when we pass int[] to a T..., it creates an Object array but not in case of Integer[]...
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Ankit Garg wrote:Well Ruben, that worked because generics don't work on primitives. When you pass an int array into a normal int vararg method, then it works fine



Here the output would be 1. But if you put generics into play, then a generic vararg cannot accept an int[] as is. So it uses the int[] as an Object and creates a new array of objects with 1 size i.e. it creates an object array like this



So this is now an object array of 1 size. That's why when we pass int[] to a T..., it creates an Object array but not in case of Integer[]...

Ankit, actually it won't create an Object array. Look at this:



The actual reason I think has nothing to do with generics. It simply has to do with how vararg calls work. If you have a method which takes an int... parameter and you pass it an int[] argument, the argument array will be exactly matched with the parameter varargs array. If, on the other hand, you have a method which takes an Integer... parameter and you pass it an int[] argument, it simply won't work, because you can't directly match an int[] array to an Integer[] array.

Now, the problem with collections is that they can't directly store primitives like you said, so a generic type T in a generic collection can never be resolved to something like int. But it could be resolved to int[], which is what is happening in the example above.

Did I misinterpreted something of what you said? I think I understand what you are saying, and agree with it (except what applies to the creation of the Object[] array.)
Igor Baiborodine
Greenhorn

Joined: Apr 13, 2007
Posts: 10

Hi guys,

I think it is more simple if you put it that way:
1. Is int[] instance of Object class? Yes.
2. Is int[] instance of Integer class? No.
That is way the second line does not compile.


SCJP 6 | SCWCD 5 | ACE ColdFusion 8 | ACE Flex 4 | OCPJP 7 | OCEWCD 6
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
But Integer[] is not an instance of Integer, and that works. You can match an array argument to a vararg array method parameter.
Igor Baiborodine
Greenhorn

Joined: Apr 13, 2007
Posts: 10

Hi Ruben,

but don't forget that int[] CANNOT be boxed into Integer[] and vice versa.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
I never said you could do that, Igor. I was referring to this:

Integer[] b = {new Integer(1), new Integer(2), new Integer(3)};
List<Integer> l3 = Arrays.asList(b); // OK (even though Integer[] is not an instance of Integer.)
vijay saraf
Ranch Hand

Joined: Jan 08, 2005
Posts: 141
Thanks Ankit & Ruben,
Ankit, I partially agree with you...
actually i have seen the class file code...
when we write

Compiler changes this to


note that their is int[][] not Object[]{int[]}

Any way...but generics with primitives is confusing...
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

vijay saraf wrote:
when we write

Compiler changes this to


note that their is int[][] not Object[]{int[]}.


Yes I was wrong on my point. It doesn't convert it into an Object[]. Maybe I myself got confused .

Any way...but generics with primitives is confusing..


How is it confusing. Generics simply don't apply to primitives. As Ruben also pointed out, primitive arrays are sub-types of Objects, that is why you can apply generics on them. If you directly use a primitive like int as parameter to generic methods, then they will automatically be boxed...


Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
vijay saraf wrote:

Compiler changes this to


note that their is int[][] not Object[]{int[]}

Vijay, are you sure about that? That looks really weird, and I don't understand why the compiler would do that.
vijay saraf
Ranch Hand

Joined: Jan 08, 2005
Posts: 141
Yes !!! Ruben,
I am sure about that conversion...
Actually that idea came from Ankit...
Looking at compiled .class file resolve most of the confusions
specially regarding inner,enum,var-args...etc.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
vijay saraf wrote:Yes !!! Ruben,
I am sure about that conversion...
Actually that idea came from Ankit...
Looking at compiled .class file resolve most of the confusions
specially regarding inner,enum,var-args...etc.

Thanks Vijay. One thing to keep in mind is that decompiling a .class file is not a process that always gives the same result. I suppose it depends on the implementation details of both the compiler which produced the class file and the decompiler that you are using to decompile the bytecodes. But Ankit has used decompiling in the past to explain some things, so it can be useful.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question of Arrays.asList()