8

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[]?

3 Answers 3

14

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

Sign up to request clarification or add additional context in comments.

3 Comments

+1 for the link. Never saw that before, learned something new!
@Fortega I need double[] because it is more clear than [D, I thought that [D could be a non unique identifier. Anyway getCanonicalName returns double[]
A reason for needing double[] is if you're creating JavaDoc links.
6

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

Comments

1

If you give using the wrapper class, you get '[Ljava.lang.Double'

Double[] d = new Double[10]

d.getClass().getName() gives you [Ljava.lang.Double

1 Comment

@lucaghera: Anyways,as you said, getCannonicalName returns you double[]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.