Getting a Primitive Data Types Wrapper Class in Java

on

Like many working developers, I find myself scouring threads on StackOverflow to find solutions for problems at many levels. While searching through posts a few days ago I found an interesting question asking how one could get the wrapper class for a primitive data type in Java. At first this question seemed a bit… stupid. If you defined the variable how could you not know it’s type? Anyways, there are a lot of libraries that provide utilities to find out a primitive data type’s wrapper class.

The Apache Commons Lang package has a nice utility method in ClassUtils called primitiveToWrapper that can make light work of the job and Google’s Guava has a primitives class to get the wrapper class as well. However, I figured it’s pretty darn easy to implement yourself by rolling your own simple utility method and saving the library dependency for something more important.

Here’s a screenshot of a simple utility class I made first declaring a variable of each primitive data type: byte, short, int, long, double, float, char and boolean. Next, I created a simple method that takes an Object as a parameter and returns the canonical class name for the object reference. Essentially this method will Autobox the primitive into it’s wrapper class… that way we can easily extract the underlying type of a primitive value after being ‘boxed’ to it’s object representation.

Primitive Wrapper

After declaring the variables for the primitive types I added each one to an ArrayList of objects to run the utility method over each one and print out the results … As you can see it’s pretty easy to determine. Of course… This use case is a bit ridiculous as we declared the instance variable type already, but this could be useful in cases where you need to know the ‘exact’ representation. For instance, take the number 127. In and of itself, 127 is naturally an int… however,  127 is a candidate for three primitive types… byte, short and int.

In Java, the max value for a byte primitive is 127. As the max value for a short primitive is up to 32767, 127 is a candidate there too… and naturally the max value for a primitive int is 2147483647. So in theory there could be situations where you need to know the specific primitive value of a number. However, I doubt there would ever be a situation where you are unsure of a long, double, float, char or boolean… there’s little ambiguity there…

The code speaks for itself here really…

public class PrimitiveWrapper {

	byte _byte = 127;
	short _short = 32767;
	int _int = 2147483647;
	long _long = 9223372036854775807L;
	double _double = 1.7976931348623157E308;
	float _float = 3.4028235E38F;
	char _char = 'c';
	boolean _boolean = true;

	public static String getPrimitiveWrapper( Object o ) {
		return o.getClass()
		        .getCanonicalName();
	}

	public static void main( String[] args ) {
		PrimitiveWrapper clazz = new PrimitiveWrapper();
		List<Object> objects = new ArrayList<>();

		objects.add( clazz._byte );
		objects.add( clazz._short );
		objects.add( clazz._int );
		objects.add( clazz._long );
		objects.add( clazz._double );
		objects.add( clazz._float );
		objects.add( clazz._char );
		objects.add( clazz._boolean );


		for(Object o : objects) {
		    System.out.println( "The primitive value [" +o+ "] wrapper is " + getPrimitiveWrapper( o ) );
		}

	}
}

And after running the code above …

The primitive value [127] wrapper is java.lang.Byte
The primitive value [32767] wrapper is java.lang.Short
The primitive value [2147483647] wrapper is java.lang.Integer
The primitive value [9223372036854775807] wrapper is java.lang.Long
The primitive value [1.7976931348623157E308] wrapper is java.lang.Double
The primitive value [3.4028235E38] wrapper is java.lang.Float
The primitive value [c] wrapper is java.lang.Character
The primitive value [true] wrapper is java.lang.Boolean
The following two tabs change content below.
Edward J. Beckett is a passionate software engineer, web developer, server administrator and polyglot programmer with nearly a decade experience building desktop and web applications ranging from simple personal web sites to enterprise level applications on many technology stacks including Java, Java EE, Spring, Spring MVC, Spring Data, Hibernate, SQL, JPA, JMS, HTML, CSS, JavaScript, ColdFusion, PHP, Node.js and more...