• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Class cast on Object Array

 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?


Thank you
Sharon
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would bet that it's not an array of BigDecimal objects.
Have you checked?

It seems odd that each object in the list is an array to me. Are you coding EJB? If you iterate through the Object array, what's in there? (or look at it in your debugger)

(p.s. you can turn off smilies for the post, to allow the code to show correctly)
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:


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.


True, a cast from Object[] to String[] is safe - as long as the array was created as String[].

And that's most likely the point. The array can only be cast to BigDecimal[] if it was created as BigDecimal[] (or subclass).

Consider:

Similarly:
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


Yes, you are right, I have been mistaken all these years. :roll:

I have proved it with this bit of code which runs without error because the Object[] contains BigDecimal objects.

 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my case its Object arr, that contains BigDecimal instances,

from the debugger :


BigDecimal result[]= (BigDecimal[]) it.next()
Throws class cast
Roger isn't it exactly the same scenario as you described?

Thank you
Sharon
[ March 27, 2008: Message edited by: Sharon whipple ]
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Slight departure from topic, but remember that you cannot cast objects. You cast object references.

This distinction should help you to understand what casts are, and are not, legal. Because casts are not done to objects, they cannot affect the objects.

So you cannot change a Fruit object to an Apple object by casting.

But, if you have a Fruit object reference that currently points to an Apple object, then you can cast the object reference to an Apple object reference.
 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand the cast concept
The only difference is the way you create the array
Object[] obj = new BigDecimal[] // will pass the cast
Object[] obj = new Object[] // will fail the cast


OUTPUT :


bigDecimal :10
bigDecimal :20

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object;
at com.comp.mng.general.SharonObjectCastTestDeleteMe.main(SharonObjectCastTestDeleteMe.java:31)
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, can you explain why you get the ClassCastException?
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have similar problem.

Object[] myArray;

if(...){myArray={3,4,5};}
else(....){myArray={'a','b','c'};}

of course i can create new variables and cast to them;

Integer[] arrayInt; /// arrayInt=(Integer[])myArray;
Character[] arrayChar; ////............

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 ]
 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

Shaorn
 
reply
    Bookmark Topic Watch Topic
  • New Topic