Java Reference
In-Depth Information
variable's name can be any valid identifier—again, a series of characters consisting of letters,
digits, underscores ( _ ) and dollar signs ( $ ) that does not begin with a digit and does not con-
tain spaces. A variable's type specifies what kind of information is stored at that location in
memory. Like other statements, declaration statements end with a semicolon ( ; ).
Line 11
Scanner input = new Scanner(System.in);
is a variable declaration statement that specifies the name ( input ) and type ( Scanner ) of a
variable that's used in this program. A Scanner enables a program to read data (e.g., num-
bers and strings) for use in a program. The data can come from many sources, such as the
user at the keyboard or a file on disk. Before using a Scanner , you must create it and spec-
ify the source of the data.
The = in line 11 indicates that Scanner variable input should be initialized (i.e., pre-
pared for use in the program) in its declaration with the result of the expression to the right
of the equals sign— new Scanner(System.in) . This expression uses the new keyword to
create a Scanner object that reads characters typed by the user at the keyboard. The stan-
dard input object , System.in , enables applications to read bytes of data typed by the user.
The Scanner translates these bytes into types (like int s) that can be used in a program.
2.5.4 Declaring Variables to Store Integers
The variable declaration statements in lines 13-15
int number1; // first number to add
int number2; // second number to add
int sum; // sum of number1 and number2
declare that variables number1 , number2 and sum hold data of type int —they can hold in-
teger values (whole numbers such as 72 , -1127 and 0 ). These variables are not yet initial-
ized. The range of values for an int is -2,147,483,648 to +2,147,483,647. [ Note: The int
values you use in a program may not contain commas.]
Some other types of data are float and double , for holding real numbers, and char ,
for holding character data. Real numbers contain decimal points, such as in 3.4 , 0.0 and
-11.19 . Variables of type char represent individual characters, such as an uppercase letter
(e.g., A ), a digit (e.g., 7 ), a special character (e.g., * or % ) or an escape sequence (e.g., the
tab character, \t ). The types int , float , double and char are called primitive types .
Primitive-type names are keywords and must appear in all lowercase letters. Appendix D
summarizes the characteristics of the eight primitive types ( boolean , byte , char , short ,
int , long , float and double ).
Several variables of the same type may be declared in a single declaration with the vari-
able names separated by commas (i.e., a comma-separated list of variable names). For
example, lines 13-15 can also be written as:
int number1, // first number to add
number2, // second number to add
sum; // sum of number1 and number2
Good Programming Practice 2.7
Declare each variable in its own declaration. This format allows a descriptive comment
to be inserted next to each variable being declared.
 
 
Search WWH ::




Custom Search