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

(singke) #1
ptg7068951

Assigning Variable Types 51

that is used to store integer numbers. You can use the inttype to store
most of the nondecimal numbers you need in your computer programs. It
can hold any integer ranging from around –2.14 billion to 2.14 billion.


Create a blank line after the int topsstatement and add the following
statement:


floatgradePointAverage;


This statement creates a variable with the name gradePointAverage. The
floattext stands forfloating-point numbers. Floating-point variables are
used to store numbers that might contain a decimal point.


The floatvariable type holds decimal numbers of up to 38 figures. The
larger doubletype holds decimal numbers up to 300 figures.


Characters and Strings


Becausethe variables you have dealt with so far are numeric, you might
have the impression that all variables are used to store numbers. Think
again. You also can use variables to store text. Two types of text can be
stored as variables: characters and strings. Acharacteris a single letter,
number, punctuation mark, or symbol. Astringis a group of characters.


Your next step in creating the Variableprogram is to create a charvari-
able and a Stringvariable. Add these two statements after the line float
gradePointAverage:


charkey = ‘C’;
String productName = “Larvets”;


When you are using character values in your program, you must put sin-
gle quotation marks on both sides of the character value being assignedto
a variable. For string values, you must surround them with double quota-
tion marks.


Quotation marks prevent the character or string from being confused with
a variable name or another part of a statement. Take a look at the following
statement:


String productName = Larvets;


This statement might look like one telling the computer to create a string
variable called productNameand give it the text value of Larvets.
However, because there are no quotation marks around the word Larvets,


NOTE
Yo u c a n u s e a f l o a t i n g - p o i n t
variable to store a grade point
average such as 2.25 (to pick
my own at the University of
North Texas—hi,Professor
Wells!). You can also use it to
store a number such as 0,
which is the percentage chance
of getting into a good graduate
school with my grade point aver-
age,which is why I was avail-
able in the job market in 1996
when this publisher was looking
for computer book authors.
Free download pdf