Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a way to check instanceof primitives variables java

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?

like image 953
Mateen Avatar asked Dec 10 '14 12:12

Mateen


People also ask

How do you check if an object is a primitive type in Java?

The java. lang. Class. isPrimitive() method can determine if the specified object represents a primitive type.

What is Instanceof check in Java?

The instanceof operator in Java is used to check whether an object is an instance of a particular class or not. Its syntax is. objectName instanceOf className; Here, if objectName is an instance of className , the operator returns true . Otherwise, it returns false .

Can you list 8 primitive types in Java?

Primitive data types - includes byte , short , int , long , float , double , boolean and char.


2 Answers

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");            
    }
like image 55
Gren Avatar answered Oct 11 '22 22:10

Gren


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());
    }

}
like image 33
Adam Avatar answered Oct 11 '22 20:10

Adam