Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Returning Arrays !

 
vamsi acharya
Ranch Hand
Posts: 55
Android Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!
 
Stephan van Hulst
Bartender
Pie
Posts: 6083
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An array is a subtype of Object.

Every type in Java, except for the primitives (boolean, int, float, char, etc.) are subtypes of Object.
 
Plamen Kamenov
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
Android Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 6083
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
Android Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
Android Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much people !!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic