Page 195 - Introduction to Programming with Java: A Problem Solving Approach
P. 195

                5.4 Wrapper Classes for Primitive Types 161 5.4 Wrapper Classes for Primitive Types
A wrapper is a construct that wraps (contains) a primitive data type and converts it to an object with a similar name, so it can be used in a situation where only objects are allowed. Wrapper classes do more than wrapping, however. They also provide some useful class methods and class constants. The java.lang package provides wrapper classes for all of the Java primitive types. Since this package is always available, you don’t need to use import to access these classes. Here are the wrapper classes that we’ll consider, along with the primitive types they encapsulate:
 Wrapper Class
Integer
Long
Float
Double
Character
Primitive Type
int
long
float
double
char
  For most wrapper classes, the wrapper class’s name is the same as its associated primitive type except that it uses an uppercase first letter. There are two exceptions. The wrapper class for int is Integer, and the wrapper class for char is Character.
Methods
Like the Math class, wrapper classes contain methods and constants. We start with methods. We limit our coverage to just two sets of methods—methods that convert strings to primitives and methods that convert primitives to strings. So when would you need to convert a string to a primitive? For example, when would you need to convert the string “4” to the int 4? If you need to read a value in as a string and then later ma- nipulate the value as a number, you’ll need to perform a string-to-number conversion. Later in this section, we’ll show a program that reads a value that could be either a number (for a lottery-number choice) or a “q” (for quitting). The program reads the user entry as a string, and if the value is not a “q,” then the program converts the user entry to a number.
Now for the other direction—when would you need to convert a primitive to a string? If you need to call a method that takes a string argument and what you’ve got is a number argument, then you’ll need to perform a number-to-string conversion. With graphical user interface (GUI) programs, all numeric output is string based. So to display a number, you need to convert the number to a string prior to calling the GUI dis- play method. With GUI programs, all numeric input is string based, too. So to read a number, you first read the input as a string and then convert the string to a number. You’ll see many examples of these processes later, in Chapters 16 and 17.
Here’s the syntax for converting strings to primitives and primitives to strings:
Wrapper Class
Integer
Long
Float
Double
String → Primitive Integer.parseInt(<string>) Long.parseLong(<string>) Float.parseFloat(<string>) Double.parseDouble(<string>)
Primitive → String Integer.toString(<#>) Long.toString(<#>) Float.toString(<#>) Double.toString(<#>)
Apago PDF Enhancer
   All the number wrapper classes work similarly. So if you understand how to convert from a string to an int, then you’ll also understand how to convert from a string to another primitive type. To convert from a







































































   193   194   195   196   197