• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Regarding Array.

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody explain me what is the reason behind the successful compilation of this line.




Thanks & Regards,
Pankaj Patel.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason is all arrays are implicitly object. So the first assignment is compatible.

Second line gives error becoz of incompatibility.
[ March 20, 2007: Message edited by: JB Ramesh ]
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

1)Object objArray = new float[20];
Here float[20] is an Object; Arrays are Objects according to JLS.
Any Object can be assigned to Object ref;

2)Float fArray = new float[20];

Array is a Object; but its not Float Object; its float[] Object.
implies float[] reference can't be assigned to Float reference.
[ March 20, 2007: Message edited by: Srinivasan thoyyeti ]
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi!
<code>
Object objArray = new float[20];//Perfect
Float fArray = new float[20]; //Error
</code>

Understanding on the behalf of instanceof test:

Every array passes the instanceof test with the Object class.

Float[] flt = new Float[20];

System.out.println(flt instanceof Object); //prints true
System.out.println(flt instanceof Float); //No
System.out.println(flt[0] instanceof Float); //prints true

the reference to the array is itself Object as always but the elements can be instanceof any class if you write like this :

Object[] object1 = new Object[10];
Here obviously object1 passes the instanceof test with the Object and each element of the array may keep reference of Any object because Object reference can keep reference of any Object.

Nobody except Object reference can refer to Float[20], because "Float[20]" fails instanceof test with the Float.

I hope understanding the concept in this way helps you!

cmbhatt
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi this is the relation ship.


object --- > objectArrray[] -----> floatArray[]

objectArray[] ---> Any array (inlcuding references)

object ---> float

object ---> any primitives


object is a super class of all including primitive arrays and reference arrays . So we can assign subclass to super class reference.

but float is not super class of floatArray[].

so it will give compile time error.
 
Pankaj Patel
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot to Everybody. And yes, i am very clear about the concept.

Enjoy(ing) The World Of Programming...
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rami,

object ---> float
object ---> any primitives



Java is not Pure Object Oriented Language.
Primitives(int,char,short...) are not Objects.

They were included for performance sake.
But every thing, (including pritives as Wrappers) can be represented as Object.

Thats why java is Complete Object Oriented Language.

 
reply
    Bookmark Topic Watch Topic
  • New Topic