DEV Community

Cover image for Processing Primitive Data Type Values as Objects
Paul Ngugi
Paul Ngugi

Posted on

Processing Primitive Data Type Values as Objects

A primitive type value is not an object, but it can be wrapped in an object using a wrapper class in the Java API. Owing to performance considerations, primitive data type values are not objects in Java.
Because of the overhead of processing objects, the language’s performance would be adversely affected if primitive data type values were treated as objects. However, many Java methods require the use of objects as arguments. Java offers a convenient way to incorporate, or wrap, a primitive data type into an object (e.g., wrapping int into the Integer class, wrapping double into the Double class, and wrapping char into the Character class,). By using a wrapper class, you can process primitive data type values as objects. Java provides Boolean, Character, Double, Float, Byte, Short, Integer, and Long wrapper classes in the java.lang package for primitive data types. The Boolean class wraps a Boolean value true or false. This section uses Integer and Double as examples to introduce the numeric wrapper classes. Most wrapper class names for a primitive type are the same as the primitive data type name with the first letter capitalized. The exceptions are Integer and Character.

Numeric wrapper classes are very similar to each other. Each contains the methods doubleValue(), floatValue(), intValue(), longValue(), shortValue(), and byteValue(). These methods “convert” objects into primitive type values. The key features of Integer and Double are shown in Figure below.

Image description

You can construct a wrapper object either from a primitive data type value or from a string representing the numeric value—for example, new Double(5.0), new Double("5.0"), new Integer(5), and new Integer("5").

The wrapper classes do not have no-arg constructors. The instances of all wrapper classes are immutable; this means that, once the objects are created, their internal values cannot be
changed.

Each numeric wrapper class has the constants MAX_VALUE and MIN_VALUE. MAX_VALUE represents the maximum value of the corresponding primitive data type. For Byte, Short, Integer, and Long, MIN_VALUE represents the minimum byte, short, int, and long values. For Float and Double, MIN_VALUE represents the minimum positive float and double values. The following statements display the maximum integer (2,147,483,647), the minimum positive float (1.4E–45), and the maximum double floating-point number (1.79769313486231570e + 308d).

System.out.println("The maximum integer is " + Integer.MAX_VALUE);
System.out.println("The minimum positive float is " +
Float.MIN_VALUE);
System.out.println(
"The maximum double-precision floating-point number is " +
Double.MAX_VALUE);

Each numeric wrapper class contains the methods doubleValue(), floatValue(), intValue(), longValue(), and shortValue() for returning a double, float, int, long, or short value for the wrapper object. For example,

new Double(12.4).intValue() returns 12;
new Integer(12).doubleValue() returns 12.0;

Recall that the String class contains the compareTo method for comparing two strings. The numeric wrapper classes contain the compareTo method for comparing two numbers and returns 1, 0, or -1, if this number is greater than, equal to, or less than the other number. For example,

new Double(12.4).compareTo(new Double(12.3)) returns 1;
new Double(12.3).compareTo(new Double(12.3)) returns 0;
new Double(12.3).compareTo(new Double(12.51)) returns -1;

The numeric wrapper classes have a useful static method, valueOf (String s). This method creates a new object initialized to the value represented by the specified string. For example,

Double doubleObject = Double.valueOf("12.4");
Integer integerObject = Integer.valueOf("12");

You have used the parseInt method in the Integer class to parse a numeric string into an int value and the parseDouble method in the Double class to parse a numeric string into a double value. Each numeric wrapper class has two overloaded parsing methods to parse a numeric string into an appropriate numeric value based on 10 (decimal) or any specified radix (e.g., 2 for binary, 8 for octal, and 16 for hexadecimal).

// These two methods are in the Byte class
public static byte parseByte(String s)
public static byte parseByte(String s, int radix)
// These two methods are in the Short class
public static short parseShort(String s)
public static short parseShort(String s, int radix)
// These two methods are in the Integer class
public static int parseInt(String s)
public static int parseInt(String s, int radix)
// These two methods are in the Long class
public static long parseLong(String s)
public static long parseLong(String s, int radix)
// These two methods are in the Float class
public static float parseFloat(String s)
public static float parseFloat(String s, int radix)
// These two methods are in the Double class
public static double parseDouble(String s)
public static double parseDouble(String s, int radix)

For example,

Integer.parseInt("11", 2) returns 3;
Integer.parseInt("12", 8) returns 10;
Integer.parseInt("13", 10) returns 13;
Integer.parseInt("1A", 16) returns 26;

Integer.parseInt("12", 2) would raise a runtime exception because 12 is not a binary number.

Note that you can convert a decimal number into a hex number using the format method. For example,

String.format("%x", 26) returns 1A;

Top comments (0)