jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Returning Arrays ! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Returning Arrays !" Watch "Returning Arrays !" New topic
Author

Returning Arrays !

vamsi acharya
Ranch Hand

Joined: Jun 28, 2009
Posts: 55

Hello guys , is returning an array to an Object type Method legal ?? If so how ?? ..Consider the following code

My certification book says its legal , i can't understand how !!


"Known is a drop , unknown is an ocean " !
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3892
    
  18

An array is a subtype of Object.

Every type in Java, except for the primitives (boolean, int, float, char, etc.) are subtypes of Object.


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Plamen Kamenov
Greenhorn

Joined: Aug 31, 2010
Posts: 1
Hi, it is legal, because the all arrays are objects like everything else in Java.
Only when you have to assign the returning result, you need to cast to int[], for example.
vamsi acharya
Ranch Hand

Joined: Jun 28, 2009
Posts: 55

Stephan van Hulst wrote:An array is a subtype of Object.

Every type in Java, except for the primitives (boolean, int, float, char, etc.) are subtypes of Object.


I know 'String' is a class hence is a subclass of 'Object' , I also know 'Arrays ' is class so its a subclass of Object but when we declare an array with '[]' how is it possible that it could be a subclass of the Object ???
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3892
    
  18

Because that's what the language designers decided. All arrays are subtypes of Object.

Try the following line: System.out.println(nums instanceof Object);
vamsi acharya
Ranch Hand

Joined: Jun 28, 2009
Posts: 55

Stephan van Hulst wrote:Because that's what the language designers decided. All arrays are subtypes of Object.



Where do the arrays reside in the API ???
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4537
    
    8

vamsi acharya wrote:Where do the arrays reside in the API ???

You need to check the Java Language Specification rather the the API. From that link:
The direct superclass of an array type is Object. Every array type implements the interfaces Cloneable and java.io.Serializable.
Stoian Azarov
Ranch Hand

Joined: Jun 01, 2011
Posts: 113
to make things more fun, here are some more legal statements that might look ridiculous:

on the contrary this is already illegal:

Regards


vamsi acharya
Ranch Hand

Joined: Jun 28, 2009
Posts: 55

Thank you very much people !!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Returning Arrays !
 
It's not a secret anymore!