Java Reference
In-Depth Information
Threads[1]=throw => RightHandSideThrow
Threads[1]=Thread => Okay!
Threads[throw]=throw => IndexThrow
Threads[throw]=StringBuffer => IndexThrow
Threads[9]=throw => RightHandSideThrow
Threads[9]=StringBuffer => ArrayIndexOutOfBoundsException
Threads[throw]=throw => IndexThrow
Threads[throw]=Thread => IndexThrow
Threads[9]=throw => RightHandSideThrow
Threads[9]=Thread => ArrayIndexOutOfBoundsException
The most interesting case of the lot is thirteenth from the end:
Threads[1]=StringBuffer => ArrayStoreException
which indicates that the attempt to store a reference to a
StringBuffer
into an array
whose components are of type
Thread
throws an
ArrayStoreException
. The code is type-
correct at compile time: the assignment has a left-hand side of type
Object[]
and a right-
hand side of type
Object
. At run time, the first actual argument to method
testFour
is a
reference to an instance of “array of
Thread
” and the third actual argument is a refer-
ence to an instance of class
StringBuffer
.
15.26.2. Compound Assignment Operators
A compound assignment expression of the form
E1 op= E2
is equivalent to
E1 = (T) ((E1)
op (E2))
, where
T
is the type of
E1
, except that
E1
is evaluated only once.
For example, the following code is correct:
short x = 3;
x += 4.6;
and results in
x
having the value
7
because it is equivalent to:
short x = 3;
x = (short)(x + 4.6);
At run time, the expression is evaluated in one of two ways.
If the left-hand operand expression is not an array access expression, then:
• First, the left-hand operand is evaluated to produce a variable. If this evaluation
completes abruptly, then the assignment expression completes abruptly for the
same reason; the right-hand operand is not evaluated and no assignment occurs.