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.example.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 do a data conversion from other primitive data types into string expression, for instance when we want to format the screen output 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.example.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;

        System.out.println("b = " + String.valueOf(b));
        System.out.println("c = " + String.valueOf(c));
        System.out.println("i = " + String.valueOf(i));
        System.out.println("l = " + String.valueOf(l));
        System.out.println("f = " + String.valueOf(f));
        System.out.println("d = " + 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.

The result of this program execution is:

b = false
c = c
i = 100
l = 100000
f = 3.4
d = 500.99