Object[] class name?

what is class name of arrays of object or arrays of primitive?i try to print with getClass().getName() & result is '[I' or '[L'.
[150 byte] By [j_shadinataa] at [2007-11-27 6:12:44]
# 1
yep and that's the right way to find the name.
prob.not.sola at 2007-7-12 17:20:14 > top of Java-index,Java Essentials,New To Java...
# 2
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
georgemca at 2007-7-12 17:20:14 > top of Java-index,Java Essentials,New To Java...
# 3

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

j_shadinataa at 2007-7-12 17:20:14 > top of Java-index,Java Essentials,New To Java...
# 4
You only get a compile-time error there because you've got an if statement that goes nowhere. Stick a semi-colon after that last line or something, it'll compile. All arrays derive from java.lang.Object
georgemca at 2007-7-12 17:20:14 > top of Java-index,Java Essentials,New To Java...
# 5

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.

prob.not.sola at 2007-7-12 17:20:14 > top of Java-index,Java Essentials,New To Java...
# 6
sorry i mean Object[]int[] a = new int[0]if (a instanceof Object[]);
j_shadinataa at 2007-7-12 17:20:14 > top of Java-index,Java Essentials,New To Java...
# 7
yes well it doesn't inherit from Object ARRAY, it inherits from Object.primitive arrays don't inherit from object arrays.interestingly, object arrays do.i.e. (String[] type instanceof Object[]) is true
prob.not.sola at 2007-7-12 17:20:14 > top of Java-index,Java Essentials,New To Java...
# 8

> 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

georgemca at 2007-7-12 17:20:14 > top of Java-index,Java Essentials,New To Java...
# 9
sorry i mean Object[]int[] a = new int[0];if (a instanceof Object[]);
j_shadinataa at 2007-7-12 17:20:14 > top of Java-index,Java Essentials,New To Java...
# 10

> 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!

georgemca at 2007-7-12 17:20:14 > top of Java-index,Java Essentials,New To Java...
# 11
> we can do this all day!sorry, he means Object[]
prob.not.sola at 2007-7-12 17:20:14 > top of Java-index,Java Essentials,New To Java...
# 12
> > we can do this all day!> > sorry, he means 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
georgemca at 2007-7-12 17:20:14 > top of Java-index,Java Essentials,New To Java...
# 13
so the getClass().getName() return [I if there is no constructor for Array , it will goes for Object constructor , from where [I come ?
eaajea at 2007-7-12 17:20:14 > top of Java-index,Java Essentials,New To Java...
# 14
> so the getClass().getName() return [I > > if there is no constructor for Array , it will goes> for Object constructor , > > from where [I come ?The runtime just makes it up. You broke our recursive nonsense, curse you
georgemca at 2007-7-12 17:20:14 > top of Java-index,Java Essentials,New To Java...
# 15

> 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

j_shadinataa at 2007-7-21 21:45:42 > top of Java-index,Java Essentials,New To Java...
# 16
geo , thanks for your nice words huh !
eaajea at 2007-7-21 21:45:42 > top of Java-index,Java Essentials,New To Java...
# 17

> 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

jsalonena at 2007-7-21 21:45:42 > top of Java-index,Java Essentials,New To Java...
# 18

> > 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

eaajea at 2007-7-21 21:45:42 > top of Java-index,Java Essentials,New To Java...
# 19

> 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...

jsalonena at 2007-7-21 21:45:42 > top of Java-index,Java Essentials,New To Java...
# 20
> Well, the class doesn't have any constuctors, fields, or methods, so it's not very> useful...It does have .length and overrides Object's clone() though.
pbrockway2a at 2007-7-21 21:45:42 > top of Java-index,Java Essentials,New To Java...
# 21

> > 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

georgemca at 2007-7-21 21:45:42 > top of Java-index,Java Essentials,New To Java...
# 22
But apparently you can't access them through the class instance.. when you list the fields the array comes out empty.
jsalonena at 2007-7-21 21:45:42 > top of Java-index,Java Essentials,New To Java...
# 23

> 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).

pbrockway2a at 2007-7-21 21:45:42 > top of Java-index,Java Essentials,New To Java...
# 24
then is String[] is subclass of Object[] ?why Object[] o = new String[0]; works?i check both (Object[] and String[]) of them is directly subclass from java.lang.Object. (getClass().getSuperclass().getName()).Message was edited by: j_shadinata
j_shadinataa at 2007-7-21 21:45:42 > top of Java-index,Java Essentials,New To Java...
# 25
> then is > String[] is subclass of Object[] ?yes, as i showed you earlier.
prob.not.sola at 2007-7-21 21:45:42 > top of Java-index,Java Essentials,New To Java...
# 26
but when you check String[] superclass name, its java.lang.Object.
j_shadinataa at 2007-7-21 21:45:42 > top of Java-index,Java Essentials,New To Java...
# 27
its java.lang.Object. not [Ljava.lang.Object;
j_shadinataa at 2007-7-21 21:45:42 > top of Java-index,Java Essentials,New To Java...
# 28
> its java.lang.Object. not [Ljava.lang.Object;True. It looks like array classes are special in many ways.
jsalonena at 2007-7-21 21:45:42 > top of Java-index,Java Essentials,New To Java...
# 29
i like your current post count, joni.
prob.not.sola at 2007-7-21 21:45:42 > top of Java-index,Java Essentials,New To Java...
# 30

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.)

pbrockway2a at 2007-7-21 21:45:49 > top of Java-index,Java Essentials,New To Java...
# 31

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.

prob.not.sola at 2007-7-21 21:45:49 > top of Java-index,Java Essentials,New To Java...