12

We can know object reference is-a test by using instanceof operator. But is there any operator to check primitive types. For example:

byte b = 10;

Now if I only consider the value 10. Is there any way I could find out that it was declared as a byte?

erakitin
  • 11,437
  • 5
  • 44
  • 49
Mateen
  • 1,631
  • 1
  • 23
  • 27
  • i know this sounds crazy. why would i want to know the datatype when i can work without knowing the datatype of the variable but i m asking this just out of my curiosity i never come across a situation to knew the declared primitive type before using it – Mateen Dec 10 '14 at 12:25
  • 1
    Is this for a local variable or an object field?? – Adam Dec 10 '14 at 12:40
  • it can local variable or object field but it must be primitive not wrappers – Mateen Dec 16 '14 at 16:07

3 Answers3

8

Local variables

Assuming you mean by local variables the primitive will always be automatically wrapped by its wrapper type whenever passed as an object, java.lang.Byte in this case. It's impossible to refer to local variables using reflection so you cannot differentiate between Byte and byte or Integer and int etc.

Object bytePrimitive = (byte) 10;

System.out.println("is a Byte ?   " + (bytePrimitive instanceof Byte));
System.out.println("Check class = " + (bytePrimitive.getClass()));

// false because class in this case becomes Byte, not byte.
System.out.println("Primitive = " + (bytePrimitive .getClass().isPrimitive()));

Fields

However, if you're talking about fields in classes, then things are different as you can get a handle on actual declared type. You can then use java.lang.Class.isPrimitive() as expected and the type will be byte.class.

public class PrimitiveMadness {
    static byte bytePrimitiveField;
    static Byte byteWrapperField;

    public static void main(String[] args) throws Exception {
        System.out.println("Field type  =     " + PrimitiveMadness.class.getDeclaredField("bytePrimitiveField").getType());
        System.out.println("Is a byte   =     " + (PrimitiveMadness.class.getDeclaredField("bytePrimitiveField").getType() == byte.class));
        System.out.println("Is a primitive? = " + PrimitiveMadness.class.getDeclaredField("bytePrimitiveField").getType().isPrimitive());
        System.out.println("Wrapper field   = " + PrimitiveMadness.class.getDeclaredField("byteWrapperField").getType());
    }

}
Adam
  • 35,919
  • 9
  • 100
  • 137
5

If you really want to play with literals...

    if(Byte.class.isInstance(10)) {
        System.out.println("I am an instance of Byte");         
    }
    if(Integer.class.isInstance(10)) {
        System.out.println("Ups, I can also act as an instance of Integer");            
    }
    if(false == Float.class.isInstance(10)) {
        System.out.println("At least I am not a float or double!");         
    }
    if(false == Byte.class.isInstance(1000)) {
        System.out.println("I am too big to be a byte");            
    }
Gren
  • 1,850
  • 1
  • 11
  • 16
  • where is the "isInstance" method ? – Mateen Dec 16 '14 at 16:04
  • The method is member of the class `Class`. – Gren Dec 16 '14 at 16:10
  • isInstance taking "Object" class as argument how can we provide it with primitive – Mateen Dec 16 '14 at 16:16
  • Java automatically converts a primitive(e.g. int) to a wrapped primitive object(e.g. Integer) and vice versa, if this is needed for assignement or a method parameter. That feature is called autoboxing/unboxing. – Gren Dec 16 '14 at 16:25
  • okay i know about boxing, but its about converting primitives to wrappers and viceversa it doesn't convert primitive to Object, so we cant directly assign an primitive to an Object but we can assign wrapper to Object – Mateen Dec 16 '14 at 16:43
  • Very helpful as doing something like "double_number instanceOf int" was giving me casting errors. – SingularityFuture Feb 01 '17 at 17:36
1
byte b = 10;
Object B= b;
 if (B.getClass() == Byte.class) {
  System.out.println("Its a Byte");
 }

Note: Byte is final, so instanceof is equivalent to class equality.

Now if you try:

Object ref = 10;
System.out.println(ref.getClass()); //class java.lang.Integer

Object ref = 10.0;
System.out.println(ref.getClass()); //class java.lang.Double

Object ref = 10L;
System.out.println(ref.getClass()); //class java.lang.Long

etc...

javaHunter
  • 1,097
  • 6
  • 9