Sunday 27 May 2018

How to check given object is primitive or not?

'java.lang.Class' provides 'isPrimitive()' method that is used to check whether given type is a primitive type or not.

public native boolean isPrimitive();
This method returns true, if object represents a primitive type, void type. 



Java has 8 primitive types.
a.   Boolean
b.   Byte
c.   Short
d.   Int
e.   Long
f.    Float
g.   Double
h.   Char

How to check primitive types?
Below statement checks whether given type is primitive or not.

boolean.class.isPrimitive()
byte.class.isPrimitive()
short.class.isPrimitive()
int.class.isPrimitive()
long.class.isPrimitive()
float.class.isPrimitive()
double.class.isPrimitive()
char.class.isPrimitive()

Test.java

package com.sample.test;

public class Test {

 public static void main(String args[]) {

  System.out.printf("Is 'boolean' primitive ? %b\n", boolean.class.isPrimitive());
  System.out.printf("Is 'byte' primitive ? %b\n", byte.class.isPrimitive());
  System.out.printf("Is 'short' primitive ? %b\n", short.class.isPrimitive());
  System.out.printf("Is 'int' primitive ? %b\n", int.class.isPrimitive());
  System.out.printf("Is 'long' primitive ? %b\n", long.class.isPrimitive());
  System.out.printf("Is 'float' primitive ? %b\n", float.class.isPrimitive());
  System.out.printf("Is 'double' primitive ? %b\n", double.class.isPrimitive());
  System.out.printf("Is 'char' primitive ? %b\n", char.class.isPrimitive());
 }
}


Output

Is 'boolean' primitive ? true
Is 'byte' primitive ? true
Is 'short' primitive ? true
Is 'int' primitive ? true
Is 'long' primitive ? true
Is 'float' primitive ? true
Is 'double' primitive ? true
Is 'char' primitive ? true

How to check wrapper types primitiveness?
Every primitive type has a correspondent wrapper type in java.

Primitive Type
Wrapper Type
byte
Byte
short
Short
int
Integer
long
Long
float
Float
double
Double
char
Character
boolean
Boolean

All the wrapper types are the sub classes of Object class, these are not primitive types.


Test.java

package com.sample.test;

public class Test {

 public static void main(String args[]) {

  Integer var1 = new Integer(10);

  System.out.printf("Is 'var1' primitive ? %b\n", var1.getClass().isPrimitive());

 }
}


Output
Is 'var1' primitive ? false

As you see the output, isPrimitive() method is returning false on Integer type.

Below application checks wrapper types primitiveness.


Test.java

package com.sample.test;

import java.util.HashSet;
import java.util.Set;

public class Test {

 private static final Set<Class<?>> PRIMITIVE_WRAPPER_MAP = new HashSet<>();

 static {
  loadPrimitiveWrapperMap();
 }

 private static void loadPrimitiveWrapperMap() {
  PRIMITIVE_WRAPPER_MAP.add(Boolean.class);
  PRIMITIVE_WRAPPER_MAP.add(Character.class);
  PRIMITIVE_WRAPPER_MAP.add(Byte.class);
  PRIMITIVE_WRAPPER_MAP.add(Short.class);
  PRIMITIVE_WRAPPER_MAP.add(Integer.class);
  PRIMITIVE_WRAPPER_MAP.add(Long.class);
  PRIMITIVE_WRAPPER_MAP.add(Float.class);
  PRIMITIVE_WRAPPER_MAP.add(Double.class);
  PRIMITIVE_WRAPPER_MAP.add(Void.class);

 }

 /**
  * This method return true, if the class is one of below types, else false.
  * <ol>
  * <li>Byte</li>
  * <li>Short</li>
  * <li>Integer</li>
  * <li>Long</li>
  * <li>Float</li>
  * <li>Double</li>
  * <li>Boolean</li>
  * <li>Character</li>
  * </ol>
  * 
  * @param clazz
  * @return
  */
 public static boolean isPrimitiveWrapper(Class<?> clazz) {
  return PRIMITIVE_WRAPPER_MAP.contains(clazz);
 }

 public static void main(String args[]) {

  Integer var1 = new Integer(10);

  System.out.printf("Is 'var1' primitive ? %b\n", isPrimitiveWrapper(var1.getClass()));

 }
}


Output
Is 'var1' primitive ? true



Previous                                                 Next                                                 Home

No comments:

Post a Comment