Java Reference
In-Depth Information
Section 7.3 Declaring and Creating Arrays
• To create an array object, specify the array's element type and the number of elements as part of
an array-creation expression (p. 246) that uses keyword
new
.
• When an array is created, each element receives a default value—zero for numeric primitive-type
elements,
false
for boolean elements and
null
for references.
• In an array declaration, the type and the square brackets can be combined at the beginning of the
declaration to indicate that all the identifiers in the declaration are array variables.
• Every element of a primitive-type array contains a variable of the array's declared type. Every el-
ement of a reference-type array is a reference to an object of the array's declared type.
Section 7.4 Examples Using Arrays
• A program can create an array and initialize its elements with an array initializer (p. 248).
• Constant variables (p. 250) are declared with keyword
final
, must be initialized before they're
used and cannot be modified thereafter.
Section 7.5 Exception Handling: Processing the Incorrect Response
• An exception indicates a problem that occurs while a program executes. The name “exception”
suggests that the problem occurs infrequently—if the “rule” is that a statement normally executes
correctly, then the problem represents the “exception to the rule.”
• Exception handling (p. 256) enables you to create fault-tolerant programs.
• When a Java program executes, the JVM checks array indices to ensure that they're greater than
or equal to 0 and less than the array's length. If a program uses an invalid index, Java generates
an exception (p. 256) to indicate that an error occurred in the program at execution time.
• To handle an exception, place any code that might throw an exception (p. 256) in a
try
state-
ment.
•The
try
block (p. 256) contains the code that might throw an exception, and the
catch
block
(p. 256) contains the code that handles the exception if one occurs.
• You can have many
catch
blocks to handle different types of exceptions that might be thrown in
the corresponding
try
block.
•When a
try
block terminates, any variables declared in the
try
block go out of scope.
•A
catch
block declares a type and an exception parameter. Inside the
catch
block, you can use
the parameter's identifier to interact with a caught exception object.
• When a program is executed, array element indices are checked for validity—all indices must be
greater than or equal to 0 and less than the length of the array. If an attempt is made to use an in-
valid index to access an element, an
ArrayIndexOutOfRangeException
(p. 257) exception occurs.
•An exception object's
toString
method returns the exception's error message.
Section 7.6 Case Study: Card Shuffling and Dealing Simulation
•The
toString
method of an object is called implicitly when the object is used where a
String
is
expected (e.g., when
printf
outputs the object as a
String
using the
%s
format specifier or when
the object is concatenated to a
String
using the
+
operator).
Section 7.7 Enhanced
for
Statement
• The enhanced
for
statement (p. 262) allows you to iterate through the elements of an array or a
collection without using a counter. The syntax of an enhanced
for
statement is:
for
(
parameter
:
arrayName
)
statement