Java Reference
In-Depth Information
FIGURE 1.7
The
ArrayList
object can be accessed using either
a1
or
a2
.
The ArrayList<Integer> object
An Integer object
a1
12345
An Integer object
a2
54321
The
for
loop compiles successfully and the output looks like this:
12345
54321
Let's look at a different example. Examine the following code that assigns two
references to each other and determine if it compiles successfully:
java.math.BigDecimal bd = new java.math.BigDecimal(2.75);
String s = bd;
The reference
bd
is of type
BigDecimal
, and
s
is of type
String
. These two classes are
not compatible, so assigning
s
to
bd
generates the following compiler error:
incompatible types
found : java.math.BigDecimal
required: java.lang.String
String s = bd;
Even using the cast operator does not fi x the problem. The following code generates a
similar compiler error, except this time the compiler complains the types are inconvertible:
java.math.BigDecimal bd = new java.math.BigDecimal(2.75);
String s = (String) bd;
Search WWH ::
Custom Search