Java Reference
In-Depth Information
• A reference to an object (p. 81) is required to invoke an object's methods. A primitive-type vari-
able does not refer to an object and therefore cannot be used to invoke a method.
Section 3.4 Account Class: Initializing Objects with Constructors
• Each class you declare can optionally provide a constructor with parameters that can be used to
initialize an object of a class when the object is created.
• Java requires a constructor call for every object that's created.
• Constructors can specify parameters but not return types.
• If a class does not define constructors, the compiler provides a default constructor (p. 83) with
no parameters, and the class's instance variables are initialized to their default values.
• If you declare a constructor for a class, the compiler will not create a default constructor for that
class.
• The UML models constructors in the third compartment of a class diagram. To distinguish a
constructor from a class's operations, the UML places the word “constructor” between guillemets
(« and »; p. 84) before the constructor's name.
Section 3.5 Account Class with a Balance; Floating-Point Numbers and Type dou-
ble
• A floating-point number (p. 84) is a number with a decimal point. Java provides two primitive
types for storing floating-point numbers in memory— float and double (p. 84).
• Variables of type float represent single-precision floating-point numbers and have seven signif-
icant digits. Variables of type double represent double-precision floating-point numbers. These
require twice as much memory as float variables and provide 15 significant digits—approxi-
mately double the precision of float variables.
• Floating-point literals (p. 84) are of type double by default.
Scanner method nextDouble (p. 88) returns a double value.
• The format specifier %f (p. 88) is used to output values of type float or double . The format spec-
ifier %.2f specifies that two digits of precision (p. 88) should be output to the right of the decimal
point in the floating-point number.
• The default value for an instance variable of type double is 0.0 , and the default value for an in-
stance variable of type int is 0 .
Self-Review Exercises
3.1
Fill in the blanks in each of the following:
a)
Each class declaration that begins with keyword must be stored in a file that
has exactly the same name as the class and ends with the .java filename extension.
b)
Keyword
in a class declaration is followed immediately by the class's name.
c)
Keyword requests memory from the system to store an object, then calls the
corresponding class's constructor to initialize the object.
d)
Each parameter must specify both a(n)
and a(n)
.
e)
By default, classes that are compiled in the same directory are considered to be in the
same package, known as the
.
f)
Java provides two primitive types for storing floating-point numbers in memory:
and
.
g)
Variables of type double represent
floating-point numbers.
h) Scanner method
returns a double value.
i)
Keyword public is an access
.
 
Search WWH ::




Custom Search