• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generics bug in Java 5??  RSS feed

 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My boss, who is not usually pointy-haired, wrote...

BTW I read recently that Java 5 has a bad generics bug and that Java 6 is recommended if you are using a lot of that.


A few Bug Database and Google searches didn't reveal any obvious candidates for what this "bad generics bug" might be. Does he speak the truth?
[ April 17, 2007: Message edited by: Peter Chase ]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've never heard of a "bad generics bug" in Java 5. If your boss can't be more specific about it and he says it just because he vaguely remembers something, then I wouldn't put too much credibility in it.

It could very well be that he has read something and now wrongly thinks he remembers something about a bug.

There are people who believe that generics in Java are not good, see for example Ken Arnold's Blog: Generics Considered Harmful. But it's not because of a bug in Java 5; he thinks the whole concept is bad.

Java 5 and 6 implement generics using type erasure, which has some strange side effects (see Java generics quirks). It looks like in some future Java version type erasure might be replaced by something better, but for the moment you'll have to live with it.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have seen lots of people saying that generics are rather difficult and that they interact really badly with arrays. But neither of those things get better with Java 6, do they?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The boss says it was this bug.

I guess it's nasty if it happens to bite you, but it won't bite all that many people.

Fixed in Java 6. As it is a compiler bug, maybe Java 5 compatible code, compiled with Java 6 javac, will be immune?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:
I have seen lots of people saying that generics are rather difficult...

Difficult? That's a question of opinion. There are lots of other things that seem difficult at first but become very useful when you learn how to use them.
...and that they interact really badly with arrays. But neither of those things get better with Java 6, do they?

That's what my blog entry is about and I already explained above that Java 5 and 6 both use type erasure, so no, nothing has become better in Java 6 with regard to this.
As it is a compiler bug, maybe Java 5 compatible code, compiled with Java 6 javac, will be immune?

I've looked at the bug report. The bug is that the Java 5 compiler only works properly if you compile the source files in a specific order. I don't understand exactly what you mean with "...will be immune?". The Java 5 compiler does not produce bad byte code; the bug is in the compiler itself. The only problem is that if you're using Java 5 you could get a wrong compiler error if you compile the sources in the wrong order.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say I write some code that provokes the Java 5 compiler bug. I can't compile it with Java 5.

So I take it to my Java 6 installation and compile it there, with -source and -target switches specifying Java 5. The compiler bug will not be a problem, as it is fixed in this version. And the resulting code ought to run OK on Java 5, because the switches said so (and I know I didn't use any Java 6 API classes).

If I make that my usual procedure, I'd be "immune" from the Java 5 compiler bug, without requiring my clients to have Java 6. Wouldn't I?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:
Say I write some code that provokes the Java 5 compiler bug. I can't compile it with Java 5.


But you can. You just need to compile the files in a different order.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:


But you can. You just need to compile the files in a different order.


You gonna volunteer to persuade our arcane Jam build system to compile classes in a particular order? Good luck!
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:
If I make that my usual procedure, I'd be "immune" from the Java 5 compiler bug, without requiring my clients to have Java 6. Wouldn't I?

Yes, you wouldn't be bitten by the bug. Note however that using the "-source" and "-target" switches doesn't prevent you from using standard API classes, interfaces and methods that are new in Java 6; i.e. the Java compiler won't check if you are using Java 5 or older only classes, interfaces and methods. If you'd use such a new feature, you'd get a runtime error on Java 5 because it wouldn't exist there (this has been recently discussed in another thread).
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:

You gonna volunteer to persuade our arcane Jam build system to compile classes in a particular order? Good luck!


I'm not going to do it, but I'd bet that if you wanted to, you *could*. That doesn't mean it would be worth the effort, but that's different from "cannot".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!