Java Reference
In-Depth Information
Section 2.4 Displaying Text with
printf
•
System.out.printf
method (p. 43;
f
means “formatted”) displays formatted data.
•Method
printf
's first argument is a format string (p. 44) containing fixed text and/or format
specifiers. Each format specifier (p. 44) indicates the type of data to output and is a placeholder
for a corresponding argument that appears after the format string.
• Format specifiers begin with a percent sign (
%
) and are followed by a character that represents the
data type. The format specifier
%s
(p. 44) is a placeholder for a string.
•The
%n
format specifier (p. 44) is a portable line separator. You cannot use
%n
in the argument to
System.out.print
or
System.out.println
; however, the line separator output by
Sys-
tem.out.println
after it displays its argument is portable across operating systems.
Section 2.5 Another Application: Adding Integers
•An
import
declaration (p. 46) helps the compiler locate a class that's used in a program.
• Java's rich set of predefined classes are grouped into packages (p. 45)—named groups of classes.
These are referred to as the Java class library (p. 46), or the Java Application Programming Inter-
face (Java API).
• A variable (p. 46) is a location in the computer's memory where a value can be stored for use later
in a program. All variables must be declared with a name and a type before they can be used.
• A variable's name enables the program to access the variable's value in memory.
•A
Scanner
(package
java.util
; p. 47) enables a program to read data that the program will use.
Before a
Scanner
can be used, the program must create it and specify the source of the data.
• Variables should be initialized (p. 47) to prepare them for use in a program.
• The expression
new Scanner(System.in)
creates a
Scanner
that reads from the standard input
object (
System.in
; p. 47)—normally the keyboard.
•Data type
int
(p. 47) is used to declare variables that will hold integer values. The range of values
for an
int
is -2,147,483,648 to +2,147,483,647.
•Types
float
and
double
(p. 47) specify real numbers with decimal points, such as
3.4
and
-11.19
.
• Variables of type
char
(p. 47) 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., tab,
\t
).
•Types such as
int
,
float
,
double
and
char
are primitive types (p. 47). Primitive-type names are
keywords; thus, they must appear in all lowercase letters.
• A prompt (p. 48) directs the user to take a specific action.
•
Scanner
method
nextInt
obtains an integer for use in a program.
•The assignment operator,
=
(p. 48), enables the program to give a value to a variable. It's called
a binary operator (p. 48) because it has two operands.
• Portions of statements that have values are called expressions (p. 49).
• The format specifier
%d
(p. 49) is a placeholder for an
int
value.
Section 2.6 Memory Concepts
• Variable names (p. 50) correspond to locations in the computer's memory. Every variable has a
name, a type, a size and a value.
• A value that's placed in a memory location replaces the location's previous value, which is lost.
Section 2.7 Arithmetic
• The arithmetic operators (p. 51) are
+
(addition),
-
(subtraction),
*
(multiplication),
/
(division)
and
%
(remainder).