This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi all I got results from getResultList, the result contain Object array When i try to cast it to BigDecimal i get Class cast but if i loop on the Object array i am able to cast to BigDecimal Why is that?
Originally posted by Roger Chung-Wee: Your cast says that BigDecimal inherits from Object. This is not so as there is never any hierarchy amongst arrays. To put it another way, BigDecimal only inherits from Object.
I don't think this is true -- there should be a "mirror" hierarchy with array objects. The top of the hierarchy should be Object inheriting from Object.
As to the original posters question, it is likely that the object is not a BigDecimal array object. Keep in mind, that if an array Object is holding BigDecimal objects, it doesn't mean the array is an BigDecimal array.
Henry [ March 18, 2008: Message edited by: Henry Wong ]
Simply put: an Object instance is not assignable to a reference of type BigDecimal, just like an Object instance is not assignable to a reference of type BigDecimal. You can try to cast it as much as you like, but it just won't fit.
but the problem is i wanna use one variable name in code after that. Only myArray!!! 1. Is there a way to change the type of given variable? 2. Or how to find variable type and use it for dynamical declaration? for instance <type of any variable> c= new <type constructor>
Probably getClass() is the answer but, how to use it? [ March 29, 2008: Message edited by: judas123 ]
Joined: Jul 31, 2003
Originally posted by Roger Chung-Wee: So, can you explain why you get the ClassCastException?
When the array reference point to Object array, you cannot cast it to BigDecimal array, take a look at the source and the result in my prev post.