This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Array questions about Object[]  RSS feed

 
Chen SanHau
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get some questions about using Object[]!



Problems :
Object c = a; // L3    ==> Why needn't delcare array as L2 ?
Number e = a; // L5     ==> If L3 is OK, why it get error ?
Object[] o2 = a; // L11   ==> If L8 is OK, why it get error ?


If anyone knows, I'd really appreciate some help !
Thanks a lot.
[ May 10, 2005: Message edited by: Chen SanHau ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
L3 is correct because all arrays implictly extend Object. This means that Integer[] "is an" Object.

L5 causes an error because arrays DON'T extend Number or any other class. All arrays extend Object directly.

L8 is okay because Integer extends Object, so you can assign an Intger[] to an Object[]. However, L11 causes an error because a is an int[]. Since int is a primitive type, it DOES NOT extend Object. This means that an int[] cannot be assigned to an Object[].

HTH

Layne
 
Chen SanHau
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I got it!
 
Chen SanHau
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I find something and get confused about it:
L5 causes an error because arrays DON'T extend Number or any other class. All arrays extend Object directly.


If all arrays extend Object directly and DON'T extend any other class, why this work:

 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is legal because c can hold a reference to an instance of array type Integer[]. Why is this? As Integer IS-A Number, then Integer can be assigned to Number.
 
Chen SanHau
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So "all arrays extend Object directly and DON'T extend any other class." is right, yah ?
 
Rajee Dhana
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

following are few rules to remember,



1)array of any data type (primitive/object references ) can be assigned to a reference variable of type Object class, since array itself is an Object by inheritence.
2) however, If an array is assigned to another array, then the rules are,

the data type of the array on the right side of assignment operator must be compile-time type convertible with the data type of the left side array.
==> for reference data types it must be a subclass or same class, implements the Interface or a sub-interface if left side array is of some Java Interface type

==> for primitives, it must be of same data type or allows safe type conversion
==> primitive to object are never type compatible, same in vice versa case
 
Chen SanHau
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Rajee Dhana. Bravo! I got it!

Array of any data type (primitive/object references ) can be assigned to a reference variable of type Object class, since array itself is an Object by inheritence.
Although array itself is an Object by inheritence, why not Java designed to using "[]" to declare array type? It's clearly and easily understood.
Using of "Object c = a;  // L3" that make me confused.
[ May 11, 2005: Message edited by: Chen SanHau ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!