i just confuse about how arrays work.
int[] (or other primitive array) somehow is subclass of java.lang.Object
so does Object[].
so both are Object.
here, its a anomaly :
Object o = new Something();
if (o instanceof Object) System.out.println("okay");
o = new int[0];
if (o instanceof Object) System.out.println("okay");
int[] aPrimitiveArray = new int[0];
if (aPrimitiveArray instanceof Object) // will compile error
class Test {
public static void main(String[] args){
Object o = new Object();
if(o instanceof Object)
System.out.println("ok");
o = new int[10];
if(o instanceof Object)
System.out.println("ok");
int[] k = new int[10];
if(k instanceof Object)
System.out.println("ok");
}
}
prints 3 ok's for me.
> sorry i mean Object[]
>
> > int[] a = new int[0]
> if (a instanceof Object[]);
>
Yeh, the compiler is smart enough to know that a primitive array is not ever going to be an instance of a reference array, and tells you so. Because it isn't
> sorry i mean Object[]
> > int[] a = new int[0];
> if (a instanceof Object[]);
>
Yeh, the compiler is smart enough to know that a primitive array is not ever going to be an instance of a reference array, and tells you so. Because it isn't
we can do this all day!
> Yeh, the compiler is smart enough to know that a primitive array is not ever going to be an instance of a reference array, and tells you so. Because it isn't
i see... & the fact its not just array.
since it's definitely return false, so compiler reduce our time compiling it :P
> from where [I come ?
It's name of the class of an array of ints. The [ implies you have an array and the I tells you that the component type is int. The syntax is specified in section 4.3.2 of the VM spec.
http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#14152
> > from where [I come ?
>
> It's name of the class of an array of ints. The [
> implies you have an array and the I tells you that
> the component type is int. The syntax is specified in
> section 4.3.2 of the VM spec.
> http://java.sun.com/docs/books/jvms/second_edition/htm
> l/ClassFile.doc.html#14152
yes this is what i was looking for , thanks alot
> That's because there is no class construct for an
> array. The runtime generates it for you as needed.
> Arrays are a monumental pain in the @rse if you're
> dealing with them reflectively, for this reason :-(
> blo0dy java
What are you talking about? Arrays have well defined classes just like all other objects in Java. The class name just happens to not be a valid Java identifier so you can't use the usual syntax to instantiate, but this is no problem to reflection. For example you can writeClass cls = Class.forName("[[D"); // array of arrays of doubles
Well, the class doesn't have any constuctors, fields, or methods, so it's not very useful...
> > That's because there is no class construct for an
> > array. The runtime generates it for you as needed.
> > Arrays are a monumental pain in the @rse if you're
> > dealing with them reflectively, for this reason
> :-(
> > blo0dy java
>
> What are you talking about? Arrays have well defined
> classes just like all other objects in Java. The
> class name just happens to not be a valid Java
> identifier so you can't use the usual syntax to
> instantiate, but this is no problem to reflection.
> For example you can writeClass cls =
> Class.forName("[[D"); // array of arrays of
> doubles
Well, the class doesn't have any
> constuctors, fields, or methods, so it's not very
> useful...
What I mean is, arrays have to be dealt with specifically when you're reflecting upon them, you often can't just pass them through the same old reflective code as any other object
> But apparently you can't access them through the class instance.. when you list the
> fields the array comes out empty.
It it occured to me I didn't know whether by "the class" you meant the class associated with the array (which does have .length) or the Class cls (which doesn't make .length or clone() available).
public class ArrayTest {
public static void main(String[] args) throws Exception {
String[] strArr = {"a"};
System.out.println(strArr instanceof Object[]); // 1 true
Object[] obArr = strArr;// 2
obArr[0] = new Object();// 3 ArrayStoreException
}
}
I'm not at all sure I understand this.
As pointed out in reply 7 the instanceof expression in line 1 returns true. So String[] is seen as a subclass of Object[]. When I look in the JLS (15.20.2), I see that the fact that "strArr instanceOf Object[]" is true means that strArr can be cast to Object[] without a ClassCastException. So I'm not surprised that the assignment in line 2 is OK.
But then I look further: 10.8 is clear that the direct superclass of an array type is Object.(See the OP's reply 26)
And 8.1.4 talks about other classes: the direct superclass is the class from which it "extends". The direct subclass is the opposite of this, and the subclass relationship is defined as the transitive closure of the direct subclass relationship.
I'm assuming that this definition of the subclass relationship applies to arrays as well. In that case since String[] is a direct subclass of Object it cannot be a subclass of Object[]. Even though an instance of String[] may, nonetheless, be validly cast to Object[] without a ClassCastException.
So which is it? Is String[] a subclass or merely a subclass wannabe that can be cast to Object[].
(I put the last line of code in to show more "special" array behaviour - String[] won't behave like a proper subclass either.)
well your line 3 should never work, so that behaviour is correct.
and it's true that the 'superclass' (as returned by getSuperclass()) for all arrays is object, but it means little because they are also subclasses of their parents arrays, like so:
public class Test {
public static void main(String[] args){
Object[] obj = new C[10];
System.out.println(obj.getClass().getSuperclass().getName());
System.out.println(obj instanceof B[]);
}
}
class A { }
class B extends A { }
class C extends B { }
which, honestly, i like.