Sams Teach Yourself Java™ in 24 Hours (Covering Java 7 and Android)

(singke) #1
ptg7068951

Assigning Variable Types 53

When working with large numbers in Java, it can be difficult to see at a
glance the value of the number, as in this statement:


long salary = 264400000;


Unless you count the zeros, you probably can’t tell that it’s $264.4 million.
Java 7 makes it possible to organize large numbers with underscore (_)
characters. Here’s an example:


long salary = 264_400_000;


The underscores are ignored, so the variable still equals the same value.
They’re just a way to make numbers more human readable.


ThebooleanVariable Type


Java hasa type of variable called booleanthat only can be used to store the
value trueor the value false. At first glance, a booleanvariable might not
seem particularly useful unless you plan to write a lot of true-or-false
quizzes. However, booleanvariables are used in a variety of situations in
your programs. The following are some examples of questions that
booleanvariables can be used to answer:


. Has the user pressed a key?
. Is the game over?
. Is my bank account overdrawn?
. Do these pants make my butt look fat?
. Can the rabbit eat Trix?


The following statement creates a booleanvariable called gameOver:


booleangameOver = false;


This variable has the starting value of false, so a statement like this could
indicate in a game program that the game isn’t over yet. Later, when some-
thing happens to end the game, the gameOvervariable can be set to true.


Although the two possible booleanvalues look like strings in a program,
you should not surround them with quotation marks. Hour 7, “Using
Conditional Tests to Make Decisions,” describes booleanvariables more
fully.


CAUTION
All the improvements offered in
Java 7,including underscores in
numbers,will be flagged as an
error in the NetBeans source
code editor unless the IDE has
been set up to recognize
Java 7. You learn how to do this
in Hour 7,“Using Conditional
Te s t s t o M a ke D e c i s i o n s .”

NOTE
Boolean numbers are named
for George Boole (1815–1864).
Boole,a mathematician who
was mostly self-taught until
adulthood,invented Boolean
algebra,which has become a
fundamental part of computer
programming,digital electron-
ics,and logic. One imagines
that he did pretty well on true-
false tests as a child.
Free download pdf