• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object array & primitives array  RSS feed

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int[] a = {1,1};

a is direct subclass of Object class .

String[] s = {"1","1"};

s is subclass of Object[] class & this is subclass of Object .

Can any explain me Why this is like this ?

thanks a lot .
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"a" cannot be a subclass of Object[] because it is an array of int's and int is not a subclass of Object. A subclass of Object[] must be an array of object references. "a" is an array of primitives.

However, all arrays types are subclasses of Object. That means that "a" is a reference to an array - "a" itself is not an array.
 
Akash Singhal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Arrays of primitive data types such as int,long,float are subclasses of Object & not Object[].Whereas all other arrays are subclasses of Object[]


Regards,
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mike .
 
Mostafa Radwan
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all.
"In the Java programming language arrays are objects , are dynamically created, and may be assigned to variables of type Object. All methods of class Object may be invoked on an array" - James Gosling -

I think we all need to read more in the Java Language Specification( 2nd Edition) , Chapter 10:Arrays and you will understand that in Java (rather than C++) any array (primitve/Object Refernces) are considered an Object and all Object's methods can be invoked on it (for ex: args.length) .

Thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!