The Java wrapper classes corresponding to the primitive types are these:
68 PROGRAMMING IN JAVA [CHAP. 5
Primitive Wrapper Class
byte Byte
short Short
int Integer
long Long
char Character
float Float
double Double
boolean Boolean
For example, if the programmer needs an object corresponding to the integer 22148, the programmer can
use this code:
Integer ix;
ix = new Integer( 22148 );
The first line declares that ix is a variable of type Integer. Since Integer is a class, not a primitive
type, ix is an object. The JVM will reserve space for an Integer object, not just 32 bits for an int.
The second line says to create a new Integer object whose value is 22148, and assign that object to the variable ix.
Another built-in Java class that you will use very, very often is the class String. A String object
consists of none, one, several or many characters which are treated as one object. For instance:
String myName;
myName = "Carl";
The first line declares that the variable myName is of type String. If the programmer prints the variable
myName, all the characters in the word Carl will be printed:
System.out.println( "name: " + myName );
The plus sign in the println statement says to ???concatenate??? (combine together) the characters "name: "
and "Carl".
Pages:
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194