Is (double[]).getClass().getName() defined?

I have a simple class that holds an ArrayList of a single type of Objects.

Most of the time it will be a double[] but other times it might be a data

holding class.

I want to be able to query the holding class for what type of objects are

in the ArrayList. I was going to return a Class object.

public Class getObjectType(){

return some Class;

}

I get "[D" as the Class name for double[]. Can i rely on that name or

should i just wrap the double[] array into a wrapper class?

I wrote this test class:

publicclass ArrayClassTest{

publicstaticvoid main(String[] args){

new ArrayClassTest();

}

public ArrayClassTest(){

System.out.println("This Class: " + this.getClass().getName());

double[] dblArray ={0.0, 1.0, 2.0};

int[] intArray ={1, 2, 3};

Object[] objArray ={null};

ArrayClassTest[] actArray ={this};

System.out.println("Array Class: " + dblArray.getClass().getName());

System.out.println("Array Class: " + intArray.getClass().getName());

System.out.println("Array Class: " + objArray.getClass().getName());

System.out.println("Array Class: " + actArray.getClass().getName());

}

}

Which yields these results:

This Class: ArrayClassTest

Array Class: [D

Array Class: [I

Array Class: [Ljava.lang.Object;

Array Class: [LArrayClassTest;

[2293 byte] By [TuringPesta] at [2007-10-3 9:22:23]
# 1
you can rely on it - it's a wierd name as that;s what the jvm gives for array type. it's still a genuine Class object - you'll have difficulty doing anything with it tho other than comparison with other class objects.
David_Waddella at 2007-7-15 4:36:02 > top of Java-index,Java Essentials,Java Programming...
# 2
Thanks a lot!
TuringPesta at 2007-7-15 4:36:02 > top of Java-index,Java Essentials,Java Programming...
# 3

I think so, the names of the array classes have been stable at least since Java 1.2, as far as I know.

The '[' says that it's an array. D, I etc. specifies a primitive (double, int etc.) '[L' means it's an object array, followed by the fully qualified name of the element type. So [Ljava.lang.String; would be a String[] etc.

johannefa at 2007-7-15 4:36:02 > top of Java-index,Java Essentials,Java Programming...
# 4
It's not clear to me what exactly you're trying to do.If you're just tyring to see if you have a particular class, skip the name part and just test if whatever.getClass == double.class. Although, if you're doing this kind of test, it's likely you have a design flaw.
jverda at 2007-7-15 4:36:02 > top of Java-index,Java Essentials,Java Programming...