Java 7 for Absolute Beginners

(nextflipdebug5) #1
CHAPTER 3 ■ DATA TYPES

Listing 3-4. String with escape sequence


String errorMessage = "The whatsit didn't work!\nCheck the doohickey.";
System.out.println(errorMessage);


That code snippet produces the output in Listing 3-5.

Listing 3-5. Newline output


The whatsit didn't work!
Check the doohickey.


Wrapper Classes


Each of the primitives (remember, String is an object rather than a primitive) has a corresponding class
that provides several useful abilities that are otherwise unavailable to a primitive. You can think of the
wrapper classes as each being a package of useful abilities wrapped around a primitive of the
corresponding type. Table 3-4 shows the corresponding class for each primitive:


Table 3-4. Wrapper classes


Primitive Class


byte Byte


short Short


int Integer


long Long


float Float


double Double


boolean Boolean


char Character


The most commonly used methods of those classes are the various parse, Value, and valueOf
methods, because those methods let you turn a String into a primitive. Each class also supports
toString methods to turn primitives into String objects. Listing 3-6 shows what you can do with an int
primitive.

Free download pdf