Thursday, March 31, 2005

Java: Object Array Casting

Here is one interesting problem which I confronted in Java yesterday.
Well I was thinking I knew Java Language reasonably, but yesterday I ran into this issue which has left me pondering what is my level of proficiency in Java?!
Here goes the problem

String[] s={"Hello","There"};
Object[] o=(String[]) s;
for(int i=0; i System.out.println(o[i]);

Looks simple enough! but was shocked to find out that the above simple code resulted in an Exception - ClassCastException!!
Aren't those simple casts allowed? After all casting is a method which comes Handy many a times esp. in RTTI. I was wondering "How Come?"
At the end googled and found that array casting from object to another is not possible in Java. Means to say that all Strings are Objects, so go ahead and cast String to an Object. But all String arrays are not Object arrays. You can individually cast all member but not as a whole!
Interesting learning for me this one, even though I think this just one the primitive things which I have conveniently ignored.

Comments:
1) Am not able to create a userId here - says userId doesnt exist when i try to sign in.

2)
String[] s={"Hello","There"};
Object o=(String[]) s;

In Java, any Array can be case to an object (not an object array). Please check u'r inbox for snarayanaswamy@inautix.com - if u havent deleted it.

P.N.Subramanian.
 
how to convert Object[] to int[] without looping them.
 
I am the kind of guy who enjoys to seek innovative things. Right now I'm making my own solar panels. I am managing it all alone without the assistance of my men. I'm utilizing the net as the only way to acheive this. I stumbled upon a really awesome site that explains how to create solar panels and wind generators. The web site explains all the steps needed for building pv panels.

I'm not really sure bout how correct the data given there is. If some guys over here who had experience with these things can have a peak and give your feedback in the thread it will be awesome and I'd really appreciate it, cause I extremely like solar panel construction.

Tnx for reading this. U people are the best.
 
Yes exactly, in some moments I can bruit about that I agree with you, but you may be inasmuch as other options.
to the article there is even now a definitely as you did in the decrease delivery of this demand www.google.com/ie?as_q=okoker iso maker 6.3 ?
I noticed the utter you have not used. Or you profit by the pitch-dark methods of inspiriting of the resource. I suffer with a week and do necheg
 
Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?