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[]
?
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.
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!
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.
[D
means an array of doubles. Check this link for an explanation on class names. Why would you like something like double[]
instead?
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[]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With