This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Behavior of Arrays.asList() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Behavior of Arrays.asList()" Watch "Behavior of Arrays.asList()" New topic
Author

Behavior of Arrays.asList()

Olivier Ledru
Greenhorn

Joined: Jun 03, 2008
Posts: 6
Hi,
Just wonder why Arrays.asList() has this behavior when passing an int[] !?
Why asList take it as a single Object, and not as a regular array ?!


Olivier,<br />SCJP5
Taariq San
Ranch Hand

Joined: Nov 20, 2007
Posts: 192
Because an array is just a single object, or have I misunderstood you?
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

the primitive array int[] arrayInt = new int[] {1,2,3,4}, i think here it is considered as single array object.
But in case of wrapper array each elements are cosidered as objects,

I am also not sure about it, i came to conclusion based on retrun type of Arrays.asList method in both the cases generated by eclipse IDE.



SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Olivier Ledru
Greenhorn

Joined: Jun 03, 2008
Posts: 6
Well, after reading http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arrays.html#asList(T...) ,
Why int[] is not read as 'int...' but Integer[] is read as 'Integer...' ?
Nothing in the API point any difference on this.
Did I miss something ?
Taariq San
Ranch Hand

Joined: Nov 20, 2007
Posts: 192
Originally posted by Olivier Ledru:
Well, after reading http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arrays.html#asList(T...) ,
Why int[] is not read as 'int...' but Integer[] is read as 'Integer...' ?
Nothing in the API point any difference on this.
Did I miss something ?



Those are 4 different ints each shoved into Integer objects, that's 4 different objects, whereas arrayInteger is a single object, an array of Integers. If it's not a primitive it's an Object, that goes for arrays too.
[ June 03, 2008: Message edited by: Taariq San ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Behavior of Arrays.asList()
 
Similar Threads
Convert list to array
why the list size is 1
How to use enhanced loop
why can not print List's content?
generics 1.4