How do I check if a class represent a primitive type?

Java uses class objects to represent all eight primitive types. A class object that represents a primitive type can be identified using the isPrimitive() method call. void is not a type in Java, but the isPrimitive() method returns true for void.class.

package org.kodejava.lang.reflect;

public class IsPrimitiveDemo {
    public static void main(String[] args) {

    private static void get(Class<?> clazz) {
        if (clazz.isPrimitive()) {
            System.out.println(clazz.getName() +
                    " is a primitive type.");
        } else {
            System.out.println(clazz.getName() +
                    " is not a primitive type.");

Here is the result of the program:

int is a primitive type.
java.lang.String is not a primitive type.
double is a primitive type.
void is a primitive type.

What is Autoboxing?

Autoboxing is a new feature offered in the Tiger (1.5) release of Java SDK. In short auto boxing is a capability to convert or cast between object wrappers (Integer, Long, etc) and their primitive types.

Previously when placing primitive data into one of the Java Collection Framework object we have to wrap it to an object because the collection cannot work with primitive data. Also, when calling a method that requires an instance of object rather than an int or long, we have to convert it too.

Now, starting from version Java 1.5 we have a new feature in the Java Language which automate this process, its call the Autoboxing. When we place an int value into a collection, such as List, it will be converted into an Integer object behind the scene. When we read it back, it will automatically convert to the primitive type. In most way this simplifies the way we code, no need to do an explicit object casting.

Here an example how it will look like using the Autoboxing feature:

package org.kodejava.basic;

import java.util.HashMap;
import java.util.Map;

public class Autoboxing {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();

        // Here we put an int into the Map, and it accepted
        // as it will be autoboxed or converted into the wrapper
        // of this type, in this case the Integer object.
        map.put("Age", 25);

        // Here we can just get the value from the map, no need
        // to cast it from Integer to int.
        int age = map.get("Age");

        // Here we simply do the math on the primitive type
        // and got the result as an Integer.
        Integer newAge = age + 10;

How do I get the minimum and maximum value of a primitive data types?

To get the minimum or maximum value of a primitive data types such as byte, short, int, long, float and double we can use the wrapper class provided for each of them, the wrapper classes are Byte, Short, Integer, Long, Float and Double which is located in java.lang package.

package org.kodejava.lang;

public class MinMaxExample {
    public static void main(String[] args) {
        System.out.println("Byte.MIN    = " + Byte.MIN_VALUE);
        System.out.println("Byte.MAX    = " + Byte.MAX_VALUE);
        System.out.println("Short.MIN   = " + Short.MIN_VALUE);
        System.out.println("Short.MAX   = " + Short.MAX_VALUE);
        System.out.println("Integer.MIN = " + Integer.MIN_VALUE);
        System.out.println("Integer.MAX = " + Integer.MAX_VALUE);
        System.out.println("Long.MIN    = " + Long.MIN_VALUE);
        System.out.println("Long.MAX    = " + Long.MAX_VALUE);
        System.out.println("Float.MIN   = " + Float.MIN_VALUE);
        System.out.println("Float.MAX   = " + Float.MAX_VALUE);
        System.out.println("Double.MIN  = " + Double.MIN_VALUE);
        System.out.println("Double.MAX  = " + Double.MAX_VALUE);

The result of the code above shows the minimum and maximum value for each data types.

Byte.MIN    = -128
Byte.MAX    = 127
Short.MIN   = -32768
Short.MAX   = 32767
Integer.MIN = -2147483648
Integer.MAX = 2147483647
Long.MIN    = -9223372036854775808
Long.MAX    = 9223372036854775807
Float.MIN   = 1.4E-45
Float.MAX   = 3.4028235E38
Double.MIN  = 4.9E-324
Double.MAX  = 1.7976931348623157E308

How do I convert primitive data types into String?

There are times when we want to convert data from primitive data types into a string, for instance when we want to format output on the screen or simply mixing it with other string. Using a various static method String.valueOf() we can get a string value of them.

Here is the code sample:

package org.kodejava.lang;

public class StringValueOfExample {
    public static void main(String[] args) {
        boolean b = false;
        char c = 'c';
        int i = 100;
        long l = 100000;
        float f = 3.4f;
        double d = 500.99;

        String u = String.valueOf(b);
        String v = String.valueOf(c);
        String w = String.valueOf(i);
        String x = String.valueOf(l);
        String y = String.valueOf(f);
        String z = String.valueOf(d);

When called with boolean argument the String.valueOf() method return true or false string depending on the boolean argument value. When called with char argument, a 1 length sized string returned.

For int, long, float, double the results are the same as calling Integer.toString(), Long.toString(), Float.toString() and Double.toString() respectively.