• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array questions about Object[]

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I got it!
 
Chen SanHau
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So "all arrays extend Object directly and DON'T extend any other class." is right, yah ?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic