Java Reference
In-Depth Information
Array Index Operator
The array index operator ( [] ) accesses an array element by presenting the location
of that element as an integer index . This operator is specified after an array variable's
name; for example, ages[0] .
Indexes are relative to 0, which implies that ages[0] accesses the first element,
whereas ages[6] accesses the seventh element. The index must be greater than or
equalto0andlessthanthelengthofthearray;otherwise,theJVMthrows ArrayIn-
dexOutOfBoundsException (consult Chapter 3 to learn about exceptions).
Anarray'slengthisreturnedbyappending“ .length ”tothearrayvariable.Forex-
ample, ages.length returnsthelengthof(thenumberofelementsin)thearraythat
ages references.Similarly, matrix.length returnsthenumberofrowelementsin
the matrix two-dimensionalarray,whereas matrix[0].length returnsthenum-
ber of column elements assigned to the first row element of this array.
Assignment Operators
Theassignmentoperator( = )assignsanexpression'sresulttoavariable(asin int x
= 4; ). The types of the variable and expression must agree; otherwise, the compiler
reports an error.
Javaalsosupportsseveralcompoundassignmentoperatorsthatperformaspecificop-
eration and assign its result to a variable. For example, the += operator evaluates the
numericexpressiononitsrightandaddstheresulttothecontentsofthevariableonits
left. The other compound assignment operators behave in a similar way.
Bitwise Operators
ThebitwiseoperatorsconsistofbitwiseAND( & ),bitwisecomplement( ~ ),bitwiseex-
clusiveOR( ^ ),andbitwiseinclusiveOR( | ).Theseoperatorsaredesignedtoworkon
thebinaryrepresentationsoftheircharacterorintegraloperands.Becausethisconcept
canbehardtounderstandifyouhaven'tpreviouslyworkedwiththeseoperatorsinan-
other language, the following example demonstrates these operators:
~0B00000000000000000000000010110101
results
in
11111111111111111111111101001010
0B00011010&0B10110111
results
in
00000000000000000000000000010010
0B00011010^0B10110111
results
in
00000000000000000000000010101101
Search WWH ::




Custom Search