?

Algorithms & Data Structures

Free Version

Upgrade subject to access all content

Moderate

Working with Wrapper Classes for Java Primitives

ALGOR-PKJOQK

For each primitive data type in Java, there is a corresponding wrapper class. A wrapper class is a Java class which "wraps around" a single primitive value, storing the value and providing methods for manipulating it. Java wrapper classes also provide static methods for parsing primitive values from strings, along with other useful functionality.

The following code snippet produces an Integer object (a wrapper for the int primitive type) holding the value 123. This object is then manipulated using methods of the Integer class along with the Float and String classes.

Integer x = new Integer(123);

String s = x.toString();
s += "." + s;

Float y = Float.parseFloat(s);
y *= 2;

s = y.toString().replace(".", "/");
String[] split = s.split("/");

Integer z = 0;
z += Integer.parseInt(split[0]);
z += Integer.parseInt(split[1]);

System.out.println(z);