Sun has released another early access version of the JSR 14 "Generics for Java". Generics are expected to be delivered with the J2SE "Tiger" release. The early access allows you to play with Generics now.
Find information here:
Sun: Prototype for JSR014: Adding Generics to the JavaTM Programming Language v. 1.3
WebLog: Ted Newards comments
JSR 14: Add Generic Types To The JavaTM Programming Language
-
Java Generics a Step Closer? (13 messages)
- Posted by: Dion Almaer
- Posted on: November 06 2002 09:04 EST
Threaded Messages (13)
- Java Generics a Step Closer? by Marcus Brito on November 07 2002 05:32 EST
- Java Generics a Step Closer? by Jordan Zimmerman on November 07 2002 16:28 EST
- Java Generics a Step Closer? by Dion Almaer on November 07 2002 18:46 EST
- Java Generics a Step Closer? by Nils Kilden-Pedersen on November 07 2002 19:24 EST
-
Java Generics a Step Closer? by Scott Ferguson on November 07 2002 07:44 EST
-
Java Generics a Step Closer? by Uwe Ritzmann on November 08 2002 04:32 EST
- Java Generics a Step Closer? by Frank Wilhoit on November 08 2002 09:35 EST
-
Java Generics a Step Closer? by Uwe Ritzmann on November 08 2002 04:32 EST
-
Java Generics a Step Closer? by Scott Ferguson on November 07 2002 07:44 EST
- Fun? by Race Condition on January 20 2003 17:29 EST
- Java Generics a Step Closer? by Mirko Novakovic on November 08 2002 03:52 EST
- Java Generics a Step Closer? by Chris Burnley on November 11 2002 05:51 EST
-
Java Generics a Step Closer? by Mirko Novakovic on November 12 2002 05:08 EST
-
Java Generics a Step Closer? by Vlad Ender on November 12 2002 01:18 EST
- Java Generics a Step Closer? by Chris Knoll on November 13 2002 09:27 EST
-
Java Generics a Step Closer? by Vlad Ender on November 12 2002 01:18 EST
-
Java Generics a Step Closer? by Mirko Novakovic on November 12 2002 05:08 EST
- Java Generics a Step Closer? by Chris Burnley on November 11 2002 05:51 EST
-
Java Generics a Step Closer?[ Go to top ]
- Posted by: Marcus Brito
- Posted on: November 07 2002 05:32 EST
- in response to Dion Almaer
JSR14 has been available for quite some time now. Good to hear that there is an upgrade -- even when the last build I've downloaded was working fine so long.
Anyway, I'll go hunting for the changelog :) -
Java Generics a Step Closer?[ Go to top ]
- Posted by: Jordan Zimmerman
- Posted on: November 07 2002 16:28 EST
- in response to Dion Almaer
Fun! I can't wait for this to be rolled in. I would prefer an implementation that didn't need to be backword compatible (a List<String> should _not_ be assignable to a List), but I can understand the reasoning. -
Java Generics a Step Closer?[ Go to top ]
- Posted by: Dion Almaer
- Posted on: November 07 2002 18:46 EST
- in response to Jordan Zimmerman
It will be interesting to see this as "the norm".
When things like this are added to a language, you tend to see a lot of best practices and design patterns come from them, and people get inspired / a bit more excited at using something new. -
Java Generics a Step Closer?[ Go to top ]
- Posted by: Nils Kilden-Pedersen
- Posted on: November 07 2002 19:24 EST
- in response to Jordan Zimmerman
Fun! I can't wait for this to be rolled in. I would
> prefer an implementation that didn't need to be
> backword compatible (a List<String> should _not_ be
> assignable to a List), but I can understand the
> reasoning.
Why not? Isn't a List<String> of type List? -
Java Generics a Step Closer?[ Go to top ]
- Posted by: Scott Ferguson
- Posted on: November 07 2002 19:44 EST
- in response to Nils Kilden-Pedersen
Why not? Isn't a List<String> of type List?
If List<String> were of type List you would have the following weirdness.
The following is illegal and caught at compile-time.
List<String> stringList = ...;
stringList.add(new Integer(0));
But the following would not be caught at compile-time
List<String> stringList = ...;
List list = stringList;
list.add(new Integer(0));
So the code in the second case would corrupt the original stringList by putting a non-String in it. -
Java Generics a Step Closer?[ Go to top ]
- Posted by: Uwe Ritzmann
- Posted on: November 08 2002 04:32 EST
- in response to Scott Ferguson
So the code in the second case would corrupt the original > stringList by putting a non-String in it.
But this is nothing new:
String[] stringArray = new String[] { "0", "1" };
Object[] objectArray = stringArray; // no CompilerError
objectArray[0] = new Integer(0); // no CompilerError
// but ArrayStoreException at runtime
As far as i have understood the spec, List<String>
will not be silently corrupted, but will throw
a ClassCastException, so you won't be more unsafe
as you are with arrays now.
The real key point is whether the design of your
generic class honors the (sometimes attributed to
Barbara Liskov) Substitution Principle (LSP),i.e.
will a Generic<Subtype> under all circumstances
behave like a Generic<Supertype>.
Collection<Object> for example promises you the
add(Object object) method, which Collection<String>
will not provide as promised, what may cause more
trouble than one might expect on first look.
But as in general generic classes might be designed
properly it is better to allow things like
Generic<SuperType> var = new Generic<SubType>
(and List ist just a shorthand for List<Object>)
I guess it would need some AI for the compiler
to check source for compliance with the LSP. -
Java Generics a Step Closer?[ Go to top ]
- Posted by: Frank Wilhoit
- Posted on: November 08 2002 09:35 EST
- in response to Uwe Ritzmann
"...I guess it would need some AI for the compiler
to check source for compliance with the LSP...."
Well, SOME people think that the intelligence built into C++ compilers is highly artificial...
;-) -
Fun?[ Go to top ]
- Posted by: Race Condition
- Posted on: January 20 2003 17:29 EST
- in response to Jordan Zimmerman
Fun? You are a complete geek and a disgrace to the human race. -
Java Generics a Step Closer?[ Go to top ]
- Posted by: Mirko Novakovic
- Posted on: November 08 2002 03:52 EST
- in response to Dion Almaer
Looks and I am looking forward to have this feature in standard JDK.
One thing is really is a pity...that primitive types are not allowed as parameters so you still have this "spaghetti" code which decreases performance:
int i = 9;
...
List<Integer> iList= new List<Integer>();
iList.add(new Integer(i));
...
int j = iList.elementAt(0).intValue();
Ok, we saved one cast, but you still have to generate a lot of useless wrapper classes and calls to intValue().
So with Java Generics we have more type safety at compile time and we need less casts, but in my opinion it is still not as powerful as C++ Templates.
Can anybody explain me what this means:
"To enable a direct mapping into the class file format of the JVM, type variables are not allowed in
catch clauses..."
Does this mean, that this is not allowed:
try {
List<String> iList = new List<String>();
..
} catch(Exception e) {
iList.elementAt(x); // ERROR???
}
Thanks,
Mirko -
Java Generics a Step Closer?[ Go to top ]
- Posted by: Chris Burnley
- Posted on: November 11 2002 05:51 EST
- in response to Mirko Novakovic
That code is not compilable anyway. The iList is declared inside the try { } catch statement. -
Java Generics a Step Closer?[ Go to top ]
- Posted by: Mirko Novakovic
- Posted on: November 12 2002 05:08 EST
- in response to Chris Burnley
Ok. Maybe this is clearer:
List<String> iList = new List<String>();
try {
...
} catch(Exception e) {
iList.elementAt(x); // ERROR???
}
...how did you know that iList wasn't declared twice somewhere outside the code-snippet? :-)
Thanks,
Mirko Novakovic
:wq -
Java Generics a Step Closer?[ Go to top ]
- Posted by: Vlad Ender
- Posted on: November 12 2002 13:18 EST
- in response to Mirko Novakovic
I think they are referring to something like:
try {
...} catch (MyGenericException<Something> e) {
} -
Java Generics a Step Closer?[ Go to top ]
- Posted by: Chris Knoll
- Posted on: November 13 2002 09:27 EST
- in response to Vlad Ender
Yeah, I think ender is correct, they are just saying that parameterized exceptions isn't handled, which seems to be fine in my book, I think the main advantage to generics is dealing with collection-oriented problems.
-Chris