How to determine the primitive type of a primitive variable?

JavaPrimitive TypesTypeof

Java Problem Overview


Is there a "typeof" like function in Java that returns the type of a primitive data type (PDT) variable or an expression of operands PDTs?

instanceof seems to work for class types only.

Java Solutions


Solution 1 - Java

Try the following:

int i = 20;
float f = 20.2f;
System.out.println(((Object)i).getClass().getName());
System.out.println(((Object)f).getClass().getName());

It will print:

java.lang.Integer
java.lang.Float

As for instanceof, you could use its dynamic counterpart Class#isInstance:

Integer.class.isInstance(20);  // true
Integer.class.isInstance(20f); // false
Integer.class.isInstance("s"); // false

Solution 2 - Java

There's an easy way that doesn't necessitate the implicit boxing, so you won't get confused between primitives and their wrappers. You can't use isInstance for primitive types -- e.g. calling Integer.TYPE.isInstance(5) (Integer.TYPE is equivalent to int.class) will return false as 5 is autoboxed into an Integer before hand.

The easiest way to get what you want (note - it's technically done at compile-time for primitives, but it still requires evaluation of the argument) is via overloading. See my ideone paste.

...

public static Class<Integer> typeof(final int expr) {
  return Integer.TYPE;
}

public static Class<Long> typeof(final long expr) {
  return Long.TYPE;
}

...

This can be used as follows, for example:

System.out.println(typeof(500 * 3 - 2)); /* int */
System.out.println(typeof(50 % 3L)); /* long */

This relies on the compiler's ability to determine the type of the expression and pick the right overload.

Solution 3 - Java

You can use the following class.

class TypeResolver
{
	public static String Long = "long";
	public static String Int = "int";
	public static String Float = "float";
	public static String Double = "double";
	public static String Char = "char";
	public static String Boolean = "boolean";
	public static String Short = "short";
	public static String Byte = "byte";

	public static void main(String[] args)
	{
		//all true
		TypeResolver resolver = new TypeResolver();
		System.out.println(resolver.getType(1) == TypeResolver.Int); 
		System.out.println(resolver.getType(1f) == TypeResolver.Float); 
		System.out.println(resolver.getType(1.0) == TypeResolver.Double);
		System.out.println(resolver.getType('a') == TypeResolver.Char); 
		System.out.println(resolver.getType((short) 1) == TypeResolver.Short); 
		System.out.println(resolver.getType((long) 1000) == TypeResolver.Long);
		System.out.println(resolver.getType(false) == TypeResolver.Boolean); 
		System.out.println(resolver.getType((byte) 2) == TypeResolver.Byte);
	}

	public String getType(int x)
	{
		return TypeResolver.Int;
	}

	public String getType(byte x)
	{
		return TypeResolver.Byte;
	}

	public String getType(float x)
	{
		return TypeResolver.Float;
	}

	public String getType(double x)
	{
		return TypeResolver.Double;
	}

	public String getType(boolean x)
	{
		return TypeResolver.Boolean;
	}

	public String getType(short x)
	{
		return TypeResolver.Short;
	}

	public String getType(long x)
	{
		return TypeResolver.Long;
	}

	public String getType(char x)
	{
		return TypeResolver.Char;
	}
}

Solution 4 - Java

There are two ways that you can use to determine the type of the Primitive type.

package com.company;

public class Testing {
public static void main(String[] args) {
    int x;
    x=0;
    // the first method 
    System.out.println(((Object)x).getClass().getName());
    if (((Object)x).getClass().getName()=="java.lang.Integer")
        System.out.println("i am int");
   // the second method it will either return true or false
    System.out.println(Integer.class.isInstance(x));
}

}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionashleyView Question on Stackoverflow
Solution 1 - JavaJoão SilvaView Answer on Stackoverflow
Solution 2 - JavaobatakuView Answer on Stackoverflow
Solution 3 - JavaDoumaView Answer on Stackoverflow
Solution 4 - JavaHazrat aliView Answer on Stackoverflow