aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding Array. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding Array." Watch "Regarding Array." New topic
Author

Regarding Array.

Pankaj Patel
Ranch Hand

Joined: Jul 13, 2006
Posts: 73
Can anybody explain me what is the reason behind the successful compilation of this line.




Thanks & Regards,
Pankaj Patel.
JB Ramesh
Greenhorn

Joined: Jan 09, 2007
Posts: 20
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 ]
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
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 ]

Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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


cmbhatt
Ram Reddy
Ranch Hand

Joined: Feb 20, 2007
Posts: 88
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

Joined: Jul 13, 2006
Posts: 73
Thanks a lot to Everybody. And yes, i am very clear about the concept.

Enjoy(ing) The World Of Programming...
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
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.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Regarding Array.