Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Whatever doesn't kill us ...<br />Is probably circling back for another try.<br />SCJP 1.4
5.5 Casting Conversion
......
The remaining cases involve conversion between reference types. The detailed rules for compile-time correctness checking of a casting conversion of a value of compile-time reference type S (source) to a compile-time reference type T (target) are as follows:
If S is a class type:
If T is a class type, then S and T must be related classes-that is, S and T must be the same class, or S a subclass of T, or T a subclass of S; otherwise a compile-time error occurs.
If T is an interface type:
If S is not a final class (�8.1.1), then the cast is always correct at compile time (because even if S does not implement T, a subclass of S might).
If S is a final class (�8.1.1), then S must implement T, or a compile-time error occurs.
If T is an array type, then S must be the class Object, or a compile-time error occurs.
If S is an interface type:
If T is an array type, then T must implement S, or a compile-time error occurs.
If T is a class type that is not final (�8.1.1), then the cast is always correct at compile time (because even if T does not implement S, a subclass of T might).
If T is an interface type and if T and S contain methods with the same signature (�8.4.2) but different return types, then a compile-time error occurs.
If S is an array type SC[], that is, an array of components of type SC:
If T is a class type, then if T is not Object, then a compile-time error occurs (because Object is the only class type to which arrays can be assigned).
If T is an interface type, then a compile-time error occurs unless T is the type java.io.Serializable or the type Cloneable, the only interfaces implemented by arrays.
If T is an array type TC[], that is, an array of components of type TC, then a compile-time error occurs unless one of the following is true:
TC and SC are the same primitive type.
TC and SC are reference types and type SC can be cast to TC by a recursive application of these compile-time rules for casting.
Whatever doesn't kill us ...<br />Is probably circling back for another try.<br />SCJP 1.4
Originally posted by Shishio San:
If S is not a final class (�8.1.1), then the cast is always correct at compile time (because even if S does not implement T, a subclass of S might).
The compiler understands that a person is free to declare a class that extends AbstractMap and implements Collections. Although is is unlikely that anyone would do that the compiler understands that it could be done. At this time, no Java compiler is able to throw an IDoubtItException in response to a situation that is possible but unlikely.
Actually you can't create a class that extends AbstractMap and implements Collection!
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
If S is an interface type:
If T is an interface type and if T and S contain methods with the same signature (�8.4.2) but different return types, then a compile-time error occurs.
If a cast to a reference type is not a compile-time error, there are two cases:
The cast can be determined to be correct at compile time. A cast from the compile-time type S to compile-time type T is correct at compile time if and only if S can be converted to T by assignment conversion (�5.2).
The cast requires a run-time validity check. If the value at run time is null, then the cast is allowed. Otherwise, let R be the class of the object referred to by the run-time reference value, and let T be the type named in the cast operator. A cast conversion must check, at run time, that the class R is assignment compatible with the type T, using the algorithm specified in �5.2 but using the class R instead of the compile-time type S as specified there. (Note that R cannot be an interface when these rules are first applied for any given cast, but R may be an interface if the rules are applied recursively because the run-time reference value may refer to an array whose element type is an interface type.) The modified algorithm is shown here:
If R is an ordinary class (not an array class):
If T is a class type, then R must be either the same class (�4.3.4) as T or a subclass of T, or a run-time exception is thrown.
If T is an interface type, then R must implement (�8.1.4) interface T, or a run-time exception is thrown.
If T is an array type, then a run-time exception is thrown.
If R is an interface:
If T is a class type, then T must be Object (�4.3.2), or a run-time exception is thrown.
If T is an interface type, then R must be either the same interface as T or a subinterface of T, or a run-time exception is thrown.
If T is an array type, then a run-time exception is thrown.
If R is a class representing an array type RC[]-that is, an array of components of type RC:
If T is a class type, then T must be Object (�4.3.2), or a run-time exception is thrown.
If T is an interface type, then a run-time exception is thrown unless T is the type java.io.Serializable or the type Cloneable, the only interfaces implemented by arrays (this case could slip past the compile-time checking if, for example, a reference to an array were stored in a variable of type Object).
If T is an array type TC[], that is, an array of components of type TC, then a run-time exception is thrown unless one of the following is true:
TC and RC are the same primitive type.
TC and RC are reference types and type RC can be cast to TC by a recursive application of these run-time rules for casting.
Whatever doesn't kill us ...<br />Is probably circling back for another try.<br />SCJP 1.4
Originally posted by Thomas Paul:
It can catch it for comparisons between Map and Collection buit fails for comparisons between AbstractMap and Collection.
This seems like a bug/mistake/or (in Microsoft terms) an undocumented feature.
RelationalExpression instanceof ReferenceType
...
If a cast of the RelationalExpression to the ReferenceType would be rejected as a compile-time error, then the instanceof relational expression likewise produces a compile-time error. In such a situation, the result of the instanceof expression could never be true.
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
The compile ignores inherited methods when performing compile time checks on a class cast to an interface but does pay attention to them when interfaces are cast to interfaces. This seems inconsistent to me. There seems to be no technical reason for the compiler to be unable to reject the comparison. It is, in my humble opinion, a mistake in the language specification.Originally posted by Dan Chisholm:
Your point is that the compiler appears to ignore inherited methods when performing the compile time checks.
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
# If S is a class type:
* If T is an interface type:
o If S is not a final class (�8.1.1), then the cast is always correct at compile time (because even if S does not implement T, a subclass of S might).
# If S is an interface type:
* If T is an interface type and if T and S contain methods with the same signature (�8.4.2) but different return types, then a compile-time error occurs.
SCJP2. Please Indent your code using UBB Code
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |