Java Reference
In-Depth Information
j) Return type indicates that a method will not return a value.
k)
Scanner
method reads characters until it encounters a newline character,
then returns those characters as a
String
.
l)
Class
String
is in package
.
m) A(n)
is not required if you always refer to a class with its fully qualified class
name.
n)
A(n)
is a number with a decimal point, such as 7.33, 0.0975 or 1000.12345.
o)
Variables of type
float
represent
_ -precision floating-point numbers.
p)
The format specifier
is used to output values of type
float
or
double
.
q)
Types in Java are divided into two categories—
types and
types.
3.2
State whether each of the following is
true
or
false
. If
false
, explain why.
a) By convention, method names begin with an uppercase first letter, and all subsequent
words in the name begin with a capital first letter.
b) An
import
declaration is not required when one class in a package uses another in the
same package.
c) Empty parentheses following a method name in a method declaration indicate that the
method does not require any parameters to perform its task.
d) A primitive-type variable can be used to invoke a method.
e) Variables declared in the body of a particular method are known as instance variables
and can be used in all methods of the class.
f) Every method's body is delimited by left and right braces (
{
and
}
).
g) Primitive-type local variables are initialized by default.
h) Reference-type instance variables are initialized by default to the value
null
.
i) Any class that contains
public
static
void
main(String[] args)
can be used to exe-
cute an app.
j) The number of arguments in the method call must match the number of parameters in
the method declaration's parameter list.
k) Floating-point values that appear in source code are known as floating-point literals and
are type
float
by default.
3.3
What is the difference between a local variable and an instance variable?
3.4
Explain the purpose of a method parameter. What is the difference between a parameter
and an argument?
Answers to Self-Review Exercises
3.1
a)
public
. b)
class
. c)
new
. d) type, name. e) default package. f)
float
,
double
. g) double-
precision. h)
nextDouble
. i) modifier. j)
void
. k)
nextLine
. l)
java.lang
. m)
import
declaration. n)
floating-point number. o) single. p)
%f
. q) primitive, reference.
3.2
a) False. By convention, method names begin with a lowercase first letter and all subse-
quent words in the name begin with a capital first letter. b) True. c) True. d) False. A primitive-type
variable cannot be used to invoke a method—a reference to an object is required to invoke the ob-
ject's methods. e) False. Such variables are called local variables and can be used only in the method
in which they're declared. f) True. g) False. Primitive-type instance variables are initialized by de-
fault. Each local variable must explicitly be assigned a value. h) True. i) True. j) True. k) False. Such
literals are of type
double
by default.
3.3
A local variable is declared in the body of a method and can be used only from the point at
which it's declared through the end of the method declaration. An instance variable is declared in a
class, but not in the body of any of the class's methods. Also, instance variables are accessible to all
methods of the class. (We'll see an exception to this in Chapter 8.)