Page 119 - Introduction to Programming with Java: A Problem Solving Approach
P. 119
3.21 Primitive Variables Versus Reference Variables 85 Specifically, your print statement should generate a tab, a HARD DISK SIZE column heading, two more
tabs, a RAM SIZE (“MEMORY”) column heading, and then two blank lines. Here’s one solution:
System.out.println('\t' + "HARD DISK SIZE" + '\t' + '\t' +
"RAM SIZE (" + '\"' + "MEMORY" + '\"' + ")" + '\n' + '\n');
That’s pretty cluttered. Fortunately, there’s a better way. An escape sequence is designed
to be used like any other character within a string of text, so it’s perfectly acceptable to embed
escape sequences within strings and omit the +’s and the single quotes. For example, here’s an
alternative solution for the PC specifications report heading problem where the +’s and single quotes have been removed:
System.out.println("\tHARD DISK SIZE\t\tRAM SIZE (\"MEMORY\")\n\n");
Everything is now all within just one string literal. By omitting the +’s and single quotes, the clutter is re- duced and that makes everyone happy. (Exception—author John’s preschoolers love clutter and would thus abhor this second solution.)
Origin of the Word “Escape” for Escape Sequences
Why is the word “escape” used for escape sequences? The backslash forces an “escape” from the normal
behavior of a specified character. For example, if t is in a print statement, the computer normally prints t.
If \t is in a print statement, the computer escapes from printing t; instead it prints the tab character. If the
double quote character (") is in a print statement, the computer normally treats it as the start or end of a
string literal. If \" is in a print statement, the computer escapes from the start/end string behavior; instead Apago PDF Enhancer
the computer prints the double quote character.
Later in the book, we present relatively advanced syntax details that pertain to the char type. You
don’t need those details now, but if you can’t wait, you can find the details in Chapter 11, Section 11.3.
3.21 Primitive Variables Versus Reference Variables
Throughout the chapter, we’ve defined and discussed various types of variables—String, int, long, float, double, and char variables. It’s now time to step back and get a big-picture view of the two dif- ferent categories of variables in Java—primitive variables and reference variables.
Primitive Variables
A primitive variable stores a single piece of data. It’s helpful to think of a primitive variable’s data item as being inherently indivisible. More formally, we say that it’s “atomic” because, like an atom, it’s a basic “building block” and it cannot be broken apart.13 Primitive variables are declared with a primitive type, and those types include:
Look for shortcuts.
int, long float, double char
(integer types) (floating-point types) (character type)
13 The word “atom” comes from the Greek a-tomos and means indivisible. In 1897, J. J. Thomson discovered one of the atom’s compo- nents—the electron—and thus dispelled the notion of an atom’s indivisibility. Nonetheless, as a holdover from the original definition of atom, the term “atomic” still refers to something that is inherently indivisible.