Skip to content
3 May 2012 / parroit

Printing the type of an object to console

Given an instance of any object, you can call it’s getClass() method to get an instance of the Class object that describe the type of the object.

Using the Class object, you can easily print it’s type name:

Integer number=Integer.valueOf(15);
This print to console the fully qualified name of the class, which for the example is:


If you want a more concise output, you can use instead:

Integer number=Integer.valueOf(15);

getSimpleName() give you only the name of the class:


Printing the type of primitive variables is a bit more complex: see this SO question for details.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: