Java Type System

M

Mike Schilling

Putting the following down on paper (OK, pixels) helped me to understand how
generics extend the Java type system. Since I haven't seen any texts that
look at things in quite this fashion, I thought I'd post it on the chance
that it would help others as well. Note that this only discusses object
types, not primitives, since it's describes the ability to refer to an
object of type A by a reference of type B, with B a supertype of A. It also
doesn't discuss conversions of the sort found in

float f = 2;

or

Integer i = 12;

In both cases, the entity on the right-hand side is converted to an entity
of a different type, quite different from what happens in

Object o = "hello";

in which the left hand-side is made to refer to precisely the entity that is
the right-hand side. OK, here we go:

The assignment statement in Java looks like

reference = expression;

This is legal whenever the type R of the reference is a supertype of the
type E of the expression, where "supertype" is defined so that, for all
types T, T is a supertype of itself. (As mentioned above, we're going to
ignore primitives and built-in conversion.) Before generics, the rules for
supertype were relatively simple:

1. T is a supertype of T
2. If T is a class other then Object, T's superclass is a supertype of T
3. Object is a supertype of all interfaces and array types
4. All interfaces that T implements or extends are supertypes of T
5. If T is a supertype of U:
T[] is a supertype of U[] (i.e. arrays are covariant)
6. If T is a supertype of U and U is a supertype of V, then T is a
supertype if V (i.e. "supertype" is transitive)

There rules are all pretty clear, even if you don't ordinarily think of them
that way.

Generics complicate this somewhat, by adding these extra rules.

If T is a supertype of U, and A is a supertype of B:
1. A<T> is a supertype of B<T>
2. A<T> is not a supertype of A<U> (i.e. generics are not covariant)
3. T is a supertype of <? extends T>
4. There is no type V for which <? extends T> is a supertype of V
5. <? super T> is a supertype of T
6. The only supertype of <? super T> is Object
7. A<? extends T> is a supertype of A<? extends U>
8. A<? super U> is a supertype of A<? super T>
 
R

Roedy Green

t also
doesn't discuss conversions of the sort found in

float f = 2;

or

Integer i = 12;

For the first look under "widening", for the second look under
"autoboxing".
--
Roedy Green Canadian Mind Products
http://mindprod.com

Responsible Development is the style of development I aspire to now. It can be summarized by answering the question, “How would I develop if it were my money?” I’m amazed how many theoretical arguments evaporate when faced with this question.
~ Kent Beck (born: 1961 age: 49) , evangelist for extreme programming.
 
M

Mike Schilling

Roedy said:
For the first look under "widening", for the second look under
"autoboxing".



Yes, Roedy, I know what thty are, but they're not relevant to what I was
exploring.
 
A

Arne Vajhøj

Yes, Roedy, I know what thty are, but they're not relevant to what I was
exploring.

Most likely Roedy (as usual) did not read the entire message that he
was replying to.

Arne
 
S

Stefan Ram

Arne Vajhøj said:
Most likely Roedy (as usual) did not read the entire message
that he was replying to.

Mike posted some very helpful observations at the end of
his post. But he started his post with distracting and
boring preliminaries. Possibly Roady was not the only one
who was not reading it to the end. Anyway, I thank Mike
for sharing his insights!
 
M

Mike Schilling

Stefan said:
Mike posted some very helpful observations at the end of
his post. But he started his post with distracting and
boring preliminaries. Possibly Roady was not the only one
who was not reading it to the end. Anyway, I thank Mike
for sharing his insights!

You're welcome. (I think.)
 
L

Lew

Mike said:
Putting the following down on paper (OK, pixels) helped me to understand how
generics extend the Java type system. ...

Nicely done.

Abstracting one layer, Mike has pinned an aspect of Java that particularly
excites me, its type expression system.

"Type-oriented programming" is next-gen object-oriented programming. Taken
together, Java interfaces and generics form a declarative syntax of assertions
about type relationships.

Properly used, this syntax lets the programmer direct the compiler to enforce
assertions that lock a program into correct behavior. Typifying the benefit,
we eliminate runtime type checks (or crashes from lack of enforcement), e.g.,
that a collection contain only 'Person' instances.

In cases where we need a runtime type check to cooperate with compile-time
enforcement, we coerce a 'Class<T>' variable into the mix, usually a private
final member. A little judicious reflection off that instance (I use the name
'this.clazz') with conventions like that 'T' not need a complex constructor,
and you can do anything you need for type 'T'. The compiler infers type
correctness from the 'Class' instance.

public <T> T find( Class<T> clazz, Object key );

or

public <T, K> T find( Class<T> clazz, K key );

which lets you call

Person person = find( Person.class, name );

If the generic were applied to the type, you store the class in the instance
rather than the argument list, e.g.,

public class PersonManager extends Manager<Person, String>
{
public PersonManager()
{
super( Person.class ); // stores in Manager#clazz
}
@Override
public Person find( String name )
{
return getEm().find( getClazz(), name );
} // actually, this duplicates the superclass code
// sorta - superclass is generic
}

Superclass 'Manager<T, K>' left as an exercise. I think of 'getEm()' and
'getClazz()' as protected final methods.
 
R

Roedy Green

Yes, Roedy, I know what thty are, but they're not relevant to what I was
exploring.

You were complaining that generic docs do not cover autoboxing and
widening. I was pointing out why there was no reason to expect them
to.
--
Roedy Green Canadian Mind Products
http://mindprod.com

Responsible Development is the style of development I aspire to now. It can be summarized by answering the question, “How would I develop if it were my money?” I’m amazed how many theoretical arguments evaporate when faced with this question.
~ Kent Beck (born: 1961 age: 49) , evangelist for extreme programming.
 
R

Roedy Green

. Possibly Roady was not the only one
who was not reading it to the end

My comment still stands whether you read the whole post or not.
--
Roedy Green Canadian Mind Products
http://mindprod.com

Responsible Development is the style of development I aspire to now. It can be summarized by answering the question, “How would I develop if it were my money?” I’m amazed how many theoretical arguments evaporate when faced with this question.
~ Kent Beck (born: 1961 age: 49) , evangelist for extreme programming.
 
M

Mike Schilling

Thomas said:
Basically, after using Java and generics quite extensively, I simply
do not buy the marketing slogan "generics add powerful compile-time
bug detection". I also note that the same slogan has been used for
C++ (with regards to C) and I do not buy it either. In both situation
it is a "nice intent", the kind Hell is paved of.

Function prototypes, invented in C++ and then retrofutted into C, prevent a
large number of bugs caused by not getting the calling sequence of a
function quite right.
 
E

Eric Sosman

Where the hell did that come from?

Don't feel bad that you don't see your complaints, Mike.
Neither can I. Perhaps we have similar difficulties with
reading comprehension.
 
M

Martin Gregorie

^^ s/of/with/

....please. Like the UK nastiness, "the england team", I find the
Americanism of using 'of' instead of 'with' very irritating. Arrrr, I
feel better with that off my chest.
Function prototypes, invented in C++ and then retrofutted into C,
prevent a large number of bugs caused by not getting the calling
sequence of a function quite right.
So that's where it came from. I'd always assumed it was an ANSI C import
from one of the Algols or Pascal. I still have a system with a K&R C
compiler and really miss ANSI function prototypes every time I write
something for it.
 
T

Tom Anderson

You're welcome. (I think.)

Yeah, too long for me, so didn't read it, but i'm looking forward to the
movie. And only partly because i heard they cast Megan Fox as a bounded
wildcard.

tom
 
T

Tom Anderson

I am not claiming that generics are ill-designed by themselves; hard and
smart work went into them, and they do what they were meant to do. I
claim that they are a revival of an old theme, and flow opposite to the
rest of the design of Java.

Interesting. Do you see Java falling into the gravity well of
Smalltalk/Self, or something different?

tom
 
T

Tom Anderson

^^ s/of/with/

...please. Like the UK nastiness, "the england team", I find the
Americanism of using 'of' instead of 'with' very irritating. Arrrr, I
feel better with that off my chest.

I believe it is the road to hell that is so paved, rather than hell
itself. Hell has parquet floors throughout.

tom
 
L

Lew

That's an Americanism? I'm American, and I'm not familiar with it.

Tom said:
I believe it is the road to hell that is so paved, rather than hell
itself. Hell has parquet floors throughout.

If the road *to* Hell is paved with good intentions (not "intent"),
and one is traversing that road in the opposite direction, i.e., *away
from* Hell, is it not still paved with good intentions?

So the road out of Hell is paved with good intentions, yes?

The issue isn't the intentions, it's the direction in which one is
traveling atop them.
 
M

Martin Gregorie

That's an Americanism? I'm American, and I'm not familiar with it.
I've always assumed it must be, since I've never seen it used by anybody
other than an American. Its possible its written rather than spoken
though I must admit that would surprise me.
 
A

Arne Vajhøj

Where the hell did that come from?

Out of thin air.

But hey it managed to squeeze 3 Java concepts into one
sentence.

That may actually impress some people. Especially
if they do not know Java.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top