Java Data Type Tutorial - Java Numeric Data Type








Byte, Short, Integer, Long, Float, and Double classes are numeric wrapper classes.

They are all inherited from the abstract Number class. We cannot create an object of the Number class. However, er can declare reference variables of the Number class.

We can assign an object reference of any of the six numeric wrapper classes to a reference of the Number class.

The Number class contains six methods. They are named xxxValue() where xxx is one of the six primitive data types (byte, short, int, long, float, and double).

The return type of the methods is the same as xxx.





Example

The following code shows how to retrieve different primate type values from a numeric wrapper object:

public class Main {
  public static void main(String[] args) {
    Integer intObj = Integer.valueOf(100);
/*ww w.  j ava2  s  . co  m*/
    // Gets byte from Integer
    byte b = intObj.byteValue();

    // Gets double from Integer
    double dd = intObj.doubleValue();
    System.out.println("intObj = " + intObj);
    System.out.println("byte from  intObj = " + b);
    System.out.println("double from  intObj = " + dd);

    // Creates a Double object
    Double doubleObj = Double.valueOf("123.45");

    // Gets different types of primitive values from Double
    double d = doubleObj.doubleValue();
    float f = doubleObj.floatValue();
    int i = doubleObj.intValue();
    long l = doubleObj.longValue();

    System.out.println("doubleObj = " + doubleObj);
    System.out.println("double from  doubleObj   = " + d);
    System.out.println("float from  doubleObj   = " + f);
    System.out.println("int from  doubleObj   = " + i);
    System.out.println("long from  doubleObj   = " + l);
  }
}

The code above generates the following result.





Methods

Java 8 has added some methods like sum(), max(), and min() in some of the numeric wrapper classes such as Integer, Long, Float, and Double.

For example, Integer.sum(10, 20) simply returns the result of 10 + 20.

Their references are used in lambda expressions working with collections.

Wrapper classes help in working with strings containing primitive values.

  • Use the valueOf() methods to convert strings into wrapper objects.
  • Use the parseXxx() methods to convert strings into primitive values.

The Byte, Short, Integer, Long, Float, and Double classes contain parseByte(), parseShort(), parseInt(), parseLong(), parseFloat() and parseDouble() methods to parse strings into primitive values, respectively.

The following code converts a string containing an integer in binary format into an Integer object and an int value:

public class Main {
  public static void main(String[] args) {
    String str = "01111111";
    int radix = 2;
//from w w w  .  j a va 2  s . c om
    // Creates an Integer object from the string
    Integer intObject = Integer.valueOf(str, radix);

    // Extracts the int value from the string
    int intValue = Integer.parseInt(str, 2);

    System.out.println("str = " + str);
    System.out.println("intObject = " + intObject);
    System.out.println("intValue = " + intValue);

  }
}

The code above generates the following result.

Value

All numeric wrapper classes contain several useful constants. Their MIN_VALUE and MAX_VALUE constants represent the minimum and maximum values. They also have a SIZE constant that represents the size in bits that a variable of the corresponding primitive type occupies.

The following code attempts to parse two strings into double values.

The first string contains a valid double and the second one an invalid double. A NumberFormatException is thrown when the parseDouble() method is called to parse the second string.

public class Main {
  public static void main(String[] args) {
    String str1 = "123.45";
    try {
      double value1 = Double.parseDouble(str1);
      System.out.println("value1 = " + value1);
    } catch (NumberFormatException e) {
      System.out.println("Error in parsing " + str1);
    }

    String str2 = "8H.9"; // An invalid double
    try {
      double value2 = Double.parseDouble(str2);
      System.out.println("value2 = " + value2);
    } catch (NumberFormatException e) {
      System.out.println("Error in parsing " + str2);
    }

  }
}

The code above generates the following result.