This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object[] and Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object[] and Object" Watch "Object[] and Object" New topic
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: 6657
    
    5

Seems to work fine for me.



SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object[] and Object
 
Similar Threads
Array
asking about interfaces
interface again
Array declaration confusion
Array of Objects