is there a way to check instanceof primitives variables java

前端 未结 3 870
小鲜肉
小鲜肉 2020-12-16 03:40

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;

3条回答
  •  萌比男神i
    2020-12-16 04:00

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

提交回复
热议问题