Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving array class name

Tags:

java

arrays

I have I problem.

In a method I receive a general Object as parameter and I have to retrieve the name of the class

public void myMethod(Object o)
  String className = o.getClass().getName();
  ...
}

It works, except when i give to the methods arrays. For example if a pass to the method an array of double (double[]), getClass().getName() returns me [D

How can I retrieve something like double[]?

like image 867
Maverik Avatar asked Aug 10 '11 12:08

Maverik


People also ask

What is the class name of array in Java?

What is the class name of Java array? In Java, an array is an object. For array object, a proxy class is created whose name can be obtained by getClass().getName() method on the object.

How do I find the class name of an object?

Access the name property on the object's constructor to get the class name of the object, e.g. obj.constructor.name . The constructor property returns a reference to the constructor function that created the instance object. Copied!

How do I retrieve an element from an array?

get() is an inbuilt method in Java and is used to return the element at a given index from the specified Array. Parameters : This method accepts two mandatory parameters: array: The object array whose index is to be returned.


2 Answers

[D means an array of doubles. Check this link for an explanation on class names. Why would you like something like double[] instead?

like image 117
Fortega Avatar answered Sep 30 '22 02:09

Fortega


Simple name of the class is what you are looking for:

System.out.print(new String[0].getClass().getSimpleName());

and the result well be:

String[]
like image 41
Mostafa Avatar answered Sep 30 '22 02:09

Mostafa