Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Comparable and Arrays

 
Ranch Hand
Posts: 117
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends !

In the below code, line 18 throws an error, saying Class cast exception !
i don't get the error if line 5 is Integer[] type.

I don't understand the reason why ? Can somebody please help !
 
Lilou Laure
Ranch Hand
Posts: 117
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PS: assume all imports are there ! I have not included it while pasting here .
 
author
Posts: 23877
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Arrays asList() method takes a varargs of generics. Generics are for objects only. And since a primitive int isn't an object, the compiler decided that the object is the array.

In other words, the asList() method returned a single element list with the array. Additionally, since you are using raw types with your arraylist, the compiler couldn't catch that mistake for you either.... and .... of course, arrays don't implement Comparable, hence, the class cast exception.

Henry
 
Lilou Laure
Ranch Hand
Posts: 117
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Additionally, since you are using raw types with your arraylist, the compiler couldn't catch that mistake for you either.... and .... of course, arrays don't implement Comparable, hence, the class cast exception.




I didn't get this part . I'm using raw types, fine, but the asList(T... a) takes T... a as argument, and when a primitive type array is being passed, why doesn't the compiler detect a type mismatch between the generic class type T and the primitive int ?
 
Henry Wong
author
Posts: 23877
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lilou Laure wrote:
I didn't get this part . I'm using raw types, fine, but the asList(T... a) takes T... a as argument, and when a primitive type array is being passed, why doesn't the compiler detect a type mismatch between the generic class type T and the primitive int ?



It is *not* a mismatch. The generic class type T is match with the int array object.  You are passing a single object parameter to the vararg -- the int array.

Henry
 
Lilou Laure
Ranch Hand
Posts: 117
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I'm not using raw types, what would be the change ? can you give me an example please ?
 
Lilou Laure
Ranch Hand
Posts: 117
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and why doesn't it get autoboxed as in this : Object ob=9;
 
Henry Wong
author
Posts: 23877
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lilou Laure wrote:If I'm not using raw types, what would be the change ? can you give me an example please ?



If you had used generics, and say, specified, ArrayList<Integer>, expecting autoboxing to happen, then the compiler would have errored out -- saying that it didn't.

In other words, while generics wouldn't get it to work the way you expected, it would have allowed you to detect that it didn't.

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

Lilou Laure wrote:and why doesn't it get autoboxed as in this : Object ob=9;



The compiler follows the Java Language Specification, and this is simply not one of those cases where autoboxing happens.  Unfortunately, for more details on why, you will probably have to ask Oracle.

Henry
 
Lilou Laure
Ranch Hand
Posts: 117
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok.

When the asList() returns a List reference what is the object/ instance exactly whose reference  is being returned.
and how does the asList() actually work , in terms of changing the backed the array ?

 
Lilou Laure
Ranch Hand
Posts: 117
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there an implementation or method body definition available for the asList method ? so that I could understand and visualize its internal working and the backed array stuff
 
Henry Wong
author
Posts: 23877
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

There should be a zipped file, on the root directory of the JDK, which contains the Java source code.

Henry
 
Lilou Laure
Ranch Hand
Posts: 117
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lilou Laure wrote:Ok.

When the asList() returns a List reference what is the object/ instance exactly whose reference  is being returned.
and how does the asList() actually work , in terms of changing the backed the array ?



this ?
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The implementation of the asList() method is quite simpleAnd there's a huge difference between the following method invocations and the actual listsOutput:
list1: [1, 2, 3]
list2: [1, 2, 3]
array3: [I@2a139a55 -> [1, 2, 3]
list3: [[I@2a139a55]


So the output of list1 and list2 is probably what you would have expected. But in the output of list3 you'll notice that the list does not contain the elements 1, 2, and 3; but contains only one element: array3 (which contains these 3 elements).
 
Lilou Laure
Ranch Hand
Posts: 117
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roel De Nijs wrote:The implementation of the asList() method is quite simple



Roel, in your implementation of asList(), you have given that it just returns an arraylist initialized with a copy of the array passed in. But how are changes made in the arraylist reflected in the original array and vice versa ?
How does that part work actually ? For example if we call methods like, set() , size(), what exactly happens,?
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lilou Laure wrote:Roel, in your implementation of asList(), you have given that it just returns an arraylist initialized with a copy of the array passed in.


That's not my implementation of asList(), but the implementation of OpenJDK 8u40-b25, an open-source implementation of the Java SE 8 Platform.

Lilou Laure wrote:But how are changes made in the arraylist reflected in the original array and vice versa ?
How does that part work actually ? For example if we call methods like, set() , size(), what exactly happens,?


The code snippet I shared was very minimalistic, in fact a little bit too minimalistic to get a complete understanding. But if you would have clicked the link (from my previous post), you would have figured out the answer(s) to your questions yourself

Here is a more complete code snippetSo a static inner class ArrayList is used which uses the array to perform all actions on. Because both this ArrayList object and the actual array refer to the same array object, changes made to either the list or the original array are performed on the same object.
 
Lilou Laure
Ranch Hand
Posts: 117
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh sorry noel , I just didn't notice the link underlined ! my bad !

anyways, its fine now, thanks for your time and response !  
 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic