File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class cast on Object Array

 
Sharon whipple
Ranch Hand
Posts: 294
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Bill Shirley
Ranch Hand
Posts: 457
  • 0
  • Mark post as helpful
  • send pies
  • 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)
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Rob Spoor
Sheriff
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Peter Chase
Ranch Hand
Posts: 1970
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, can you explain why you get the ClassCastException?
 
Rob Spoor
Sheriff
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
judas123
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic