Java Reference
In-Depth Information
Converting the new doubleTest value to a long variable would result in a value of 9876543210987. Long variables
can handle very large whole numbers, so most of the value is saved but the fractional value (.99) is truncated.
Converting to an
int
variable would result in a value of 2147483647, the largest value an
int
variable can hold.
This is really bad because the
int
value is not even close to the original
double
value. The
short
and
byte
values are
equally bad at -1 and -1.
In summary, the syntax for casting is a little complicated; however, the real complication comes from trying to fit
larger values into smaller variable types. Be very careful when casting because of the possibly unexpected results of
moving from larger to smaller types.
Strings to Primitives and Vice Versa
The
String
class has a very nice set of static toString methods that will convert primitive types to strings. Again,
a static method can simply be invoked without creating a
String
object. For instance, the following are all valid
statements:
String byteString = String.valueOf(byteTest);
String shortString = String.valueOf(shortTest);
String intString = String.valueOf(intTest);
String longString = String.valueOf(longTest);
String floatString = String.valueOf(floatTest);
String doubleString = String.valueOf(doubleTest);
Converting from a string to a primitive is a little more complicated. Unlike
String
, primitives are not classes and
do not have useful methods such as valueOf. However, the Java developers have created a series of classes for each of
the primitive types that contain many useful methods. These classes are called
wrapper classes
.
Each wrapper class has the same name as the primitive type (except
int
), but the wrapper class name begins with
a capital letter. For example, the wrapper class associated with
double
is
Double
, the
long
wrapper class is
Long
, but
the
int
wrapper class is
Integer
.
You can think of the wrapper classes as helper classes: they have methods that help programmers work with
primitive variables. For example, each wrapper class has a parse method that will convert a
String
variable to
the associated primitive type. Each parse method has a unique name that begins with the text “parse” and then is
followed by the primitive variable type. For instance, the following are all valid statements:
byteTest = Byte.parseByte(byteString);
shortTest = Short.parseShort(shortString);
intTest = Integer.parseInt(intString);
longTest = Long.parseLong(longString);
floatTest = Float.parseFloat(floatString);
doubleTest = Double.parseDouble(doubleString);
Notice that the
Integer
parse method is called
parseInt
, not parseInteger. In addition, the wrapper classes have
static toString methods that perform the same function as the
String
valueOf methods (i.e., convert from primitive to
string). The syntax would be as follows:
byteString = Byte.toString(byteTest);
shortString = Short.toString(shortTest);
intString = Integer.toString(intTest);
longString = Long.toString(longTest);
floatString = Float.toString(floatTest);
doubleString = Double.toString(doubleTest);