Re: Question about casts
J Leonard <John.Richard.Leonard@gmail.com> writes:
practice is to cast this Graphics object to type Graphics2D and [...]
I'm trying to understand how this can work. My concept of what is [...]
If answering to this post, please do not quote all of it,
but only short part you directly refer to.
This explanation of casts begins with some words about reference
types and references. It ends with the explanation of ?downcasts?,
such as a cast from ?Graphics? to ?Graphics2D? is. Best read all
of it in the given sequence.
Reference Types
===============
Supertypes
----------
A reference type ?S? here is called a ?supertype? of a type ?U? (S>=U)
iff ?U? directly or indirectly extends or implements ?S? or is ?S?.
Examples:
?java.lang.String? is a supertype of ?java.lang.String?.
?java.lang.Object? is a supertype of ?java.lang.String?.
?java.lang.String? is not a supertype of ?java.lang.Object?.
Exercises:
Is ?java.lang.Integer? a supertype of ?java.lang.String??
Is ?java.lang.String? a supertype of ?java.lang.Integer??
Proper Subtypes
---------------
If ?U? is not ?S?, then ?U? is called a ?proper subtype? of ?S? (U<S).
Examples:
java.lang.String is a proper subertype of java.lang.Object.
java.lang.Object is not a proper subtype of java.lang.Object.
Exercises:
Is java.lang.Integer a proper subtype of java.lang.String?
Is java.lang.String a proper subtype of java.lang.Integer?
Types of Expressions and Objects
--------------------------------
In Java, both expression (entities of the source-code model) and
objects (entities of the run-time model) have a type.
Types of expressions are known at compile time, while
types of objects are known only at run time in the general case.
Example:
In Java SE 1.6, the type of the expression ?java.lang.System.in? is
java.io.InputStream, the type of the object ?java.lang.System.in? is
java.io.BufferedInputStream.
Exercise:
What is the type of the expression ?"alpha"??
What is the type of the object ?"alpha"??
What is the type of the expression ?java.lang.System.out??
What is the type of the object ?java.lang.System.out??
References
==========
.-----------------------------------------------------.
| |
| Fundamental requirement for any reference |
| |
| B1 The type ?R? of a reference expression must |
| be a supertype of the type ?O? of the |
| object it refers to (R>=O) |
| |
'-----------------------------------------------------'
Example:
In Java SE 1.6, the expression ?java.lang.System.in? has the
type ?java.io.InputStream? and it references an object of the
type ?java.io.BufferedInputStream?. And indeed, ?java.io.InputStream?
is a supertype of the type ?java.io.BufferedInputStream?.
Casts of References
===================
Let r be a reference-valued expression (syntactically, a
?UnaryExpressionNotPlusMinus?).
Let T be a reference type (syntactically, a ?ReferenceType?).
Then
(T)r
is an expression, called a ?cast expression? (?CastExpression?).
.-----------------------------------------------------.
| |
| Fundamental properties of the reference cast |
| |
| A1 The type of the expression ?(T)r? is ?T?. |
| |
| A2 If the value of ?r? is null, the value of |
| ?(T)r? also is null. |
| |
| A3 If ?r? refers to an object, the expression |
| ?(T)r? refers to the same object ?o? as |
| the expression ?r? refers to. |
| |
'-----------------------------------------------------'
Example:
?( java.lang.Object )"alpha"? is an expression of type
?java.lang.Object? referencing an object of type ?java.lang.String.
Upcasts
=======
Let ?R? be the type of the expression ?r?. If ?r? refers to an object,
Let ?O? be the type of the object ?r? refers to. (?O? is a class.)
If ?T? is a supertype of ?R? (T>=R) the cast is always allowed and
is called an ?upcast?. (Proof: T>=R, and by B1, R>=O, thus, T>=O (B1)
is fulfilled; or r is null.)
Example:
?( java.lang.Object )"alpha"? is an upcast, because java.lang.Object >=
java.lang.String.
Downcasts
=========
Now, assume that ?T? is a proper subtype of ?R? (T<R): Because
a possible type of ?O? is only known at run time, it can only be known
at runtime, whether B1 is fulfilled (T<R and R>=O does not imply T>=O.)
Such a cast is called a ?downcast?.
The Java Machine will check at runtime whether a downcast fullfils B1,
but it does not check anything for an upcast, which always fullfils B1.
Example:
?( java.lang.String )( java.lang.Object )"alpha"? is a downcast,
because java.lang.String < java.lang.Object.
Exercises:
Assuming that ?String? is a proper subtype of ?Object? (String<Object),
which of the following expressions (one expression per line) are upcasts,
which are downcast, which will create an error during compilation and
which will create an exception during evaluation?
( Object )new String()
( String )new String()
( Object )new Object()
( String )new Object()