| Author |
Object[] and Object
|
Anthony Karta
Ranch Hand
Joined: Aug 09, 2004
Posts: 342
|
|
why we canNot pass String[] to Object[] in method argument such as anyArrayType(new String[] {"a", "b"} ); void anyArrayType(Object[] o) { } //void anyArrayType(Object o) { } // this one work BUT... It is working in variable assignment such as Object[] o1 = new String[] {"a", "b"}; Object o2 = new String[] {"c", "d"}; thanks
|
SCJP 5
|
 |
Charith Fernando
Ranch Hand
Joined: Sep 12, 2005
Posts: 67
|
|
|
what is the error you are getting? please post the compilation error if you're getting any....
|
Charith I Fernando<br />SCJP5, SCWCD, SCBCD, BSc(Hons) IS<br />+94 773 263 222 (mobile)
|
 |
Deepak Bala
Bartender
Joined: Feb 24, 2006
Posts: 6603
|
|
Seems to work fine for me.
|
SCJP 6 articles - SCJP 5/6 mock exams - SCJP Mocks - SCJP 5 Mock exam (Word document ) - SCJP 5 Mock exam in Java.Inquisition format
|
 |
Anthony Karta
Ranch Hand
Joined: Aug 09, 2004
Posts: 342
|
|
Sorry guys. I tried with "primitive" type such as: anyArrayType(new short[] {5,8}); why it works for Wrapper classes?? thanks again
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
|
I think Anthony is asking about overloading here. Anthony do you mean having both anyArrayType method definitions at the same time?
|
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
Originally posted by Anthony Karta: Sorry guys. I tried with "primitive" type such as: anyArrayType(new short[] {5,8}); why it works for Wrapper classes?? thanks again
It works because arrays of wrapper class objects are arrays of Objects too.
|
 |
Anthony Karta
Ranch Hand
Joined: Aug 09, 2004
Posts: 342
|
|
Originally posted by Barry Gaunt: It works because arrays of wrapper class objects are arrays of Objects too.
well, I'm expecting short[] array will be autoboxed to Short[] by JVM, but the code even not compile. do more testing: short[] shortArray = new short[] {5,8} ; Object[] oo = (Short[]) shortArray; // Not compiled here how to convert primitive array to Wrapper array then? this whole array thing make me confuse. Seems autoboxing doesn't work the same way as non-array reference variable, such as below: short shortVar = 5; Short shortWrapper = shortVar; // worked !
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
|
What you are expecting is not possible. A short[] will not be autoboxed into a Short[]. Autoboxing only applies to the primitive and its corresonding wrapper class (eg. short -> Short).
|
 |
 |
|
|
subject: Object[] and Object
|
|
|