File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Class cast on Object Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class cast on Object Array" Watch "Class cast on Object Array" New topic
Author

Class cast on Object Array

Sharon whipple
Ranch Hand

Joined: Jul 31, 2003
Posts: 294
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
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
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)


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
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.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18764
    
  40

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 ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
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

Joined: Jul 31, 2003
Posts: 294
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 ]
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Sharon whipple
Ranch Hand

Joined: Jul 31, 2003
Posts: 294
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

Joined: Sep 29, 2002
Posts: 1683
So, can you explain why you get the ClassCastException?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

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.
judas123
Greenhorn

Joined: Mar 29, 2008
Posts: 1
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

Joined: Jul 31, 2003
Posts: 294
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
 
 
subject: Class cast on Object Array