Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Clean way of checking if object type is primitive or String

807589Jan 6 2009 — edited Jan 6 2009
Hi,

I'm just wondering if any method exists in Java where I can pass a value of type object and it will check to see if the type is primitive or String. Basically I want to avoid other objects such as collections, reference objects etc. Currently I have the following code
if ((value instanceof String) || (value instanceof Byte)
|| (value instanceof Short) || (value instanceof Integer)
|| (value instanceof Long) || (value instanceof Float)
|| (value instanceof Double) || (value instanceof Boolean)
|| (value instanceof Character) || (value instanceof null){}
which will only use the value if it is a String or primitive. However, I also have to check if the value is null because a String could be null etc. This is not ideal.

Does anyone know of a method/utility class which could be used to check if the type of the object is of String or primitive. E.g. something like the following
if(ClassUtils.isPrimitive(value)){}
Thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 3 2009
Added on Jan 6 2009
15 comments
1,804 views