开发者

What are the dangers in upgrading to Java 1.5 and beyond?

I am a long-term user of Java (but I am a scientist and not a professional developer) . I upgrade whenever new versions come out but often I only use a small portion of the new features at first as I don't always have time to study everything.

Recently I learned that unless I fully understood generics I was likely to have serious dangers in using (or in this case not using them) - dangers which I was unaware of and which are not obvious. (I appreciate that I was warned by Eclipse but I didn't understand the warning!)

The particular case in point was that it is now (since 1.5) bad practice to use raw types and that Class (rather than Class<?> or Class<MyStuff>) had serious dangers. see Why is Class<?> preferred to Class.

My question is - what others dangers am I likely to encounter and be unaware of as a result of upg开发者_如何转开发rading to Java 1.5 (or 1.6, 1.7) but using my old code or style of programming?


I'll speak about the converse (i.e. the benefits):

I'm not aware of dangers (and I've ported Java 1.4 codes to Java 6 without hickups as well as java 5 codes to java 6, java meaning JDK). As for warnings, it's basically a warning (since your code will compile) and you are free to resolve these warnings.

As for as Generics is concerned, Generics are used for Compile-time checking by the compiler, i.e. once the compiler has the element type (e.g. in Collection), it can see whether you have used the object correctly with the correct cast.

Generics are implented by type erasure. They are present only at compile-time after that the compiler erases it. This is to provide interoperability with legacy codes which uses raw types (e.g. List in JDK 1.4).

I don't know if these would be the dangers, but it wouldn't affect your JDK 4 applications:

  • Autoboxing
  • Enums
  • Varargs
  • foreach
  • Annotations

And you have better performance of the Java Collections Framework.

PS the only problem migrating from JDK 1.4 to a new JDK (5 and higher) is you'll face issues with enum (as it is a reserved keyword from JDK 5 and higher). OTher than that, you're ok.


I would say benefits from better performance and new features greatly outweigh any "dangers".

The only danger I can foresee is that you'll get a lot of compiler warnings about the very Class/Class you raise. These are more of a nuisance than a danger though.


Problems we ran into:

  • enum is a reserved keyword in java1.5 (we had to rename some packages).
  • External-Jars may depend on java1.4 (weblogic8.2 doesn't run with jdk1.5 we had to upgrade to weblogic9.2)

Consider upgrading your code to use generic-syntax. Depending on your codebase this can be a large project. Using generic-classes and functions is easy. Writing them is more difficult and might force you to refactor your code. It's good practice to have few/no warnings in your java-code, so if you don't plan to use generics in your code in short time i would disable the warning, because you might miss a more important warning.


One thing that breaks with Java5+ JVMs, when migrating from 1.4 code, is the old-style enums.

public class Foo {
  public static final INSTANCE1 = new Foo("foo");
  public static final INSTANCE2 = new Foo("bar");
  ...
}

Because of the new classloading policy on Java 1.5+ VMs, your "enums" may not be loaded at all, causing NullPointerExceptions. The solution is obvious : replace them with standart Java5 enums.

Another thing you may consider is the need to train your developers to the new language constructs, mostly generics, enums and the java.util.concurrent stuff.

Aside from this, I tend to think there are only benefits to switching to the most recent JVM version available, both in terms of performance and code quality.


I haven't experienced any issues with upgrading to later versions of Java. The problem I usually face when rebuilding old code with a new compiler version is that Eclipse show a sea of warnings and I feel obliged to fix them.

Generics being a case in point. Collection classes were a mess before generics, but now they're mostly tolerable assuming you fix the warnings. I don't think it's as clean as .NET because generic information doesn't survive to runtime, so it's still possible to break a collection by adding an object of the wrong type. At least with generics the compiler has a chance of catching most errors and generics minimize the amount of casting too. On the flip side, code can get pretty ugly with angle brackets. It's too bad that Java can't infer generic information on occasion. For example it would be nice to be able to say List<> myList = new ArrayList<String> and the left side infers its args from the right side.

Likewise, when iterating collections most code goes from one end to the other. It's probably the most common pattern in any code. Therefore it makes sense to have a simplified for loop block which generates all that boiler plate. It doesn't work in every case though, e.g. if you selectively remove elements so there is still the Iterator fallback.

Things like autoboxing are IMO a little more questionable in their application. While it's great that you can cast back and forth between reference and type, I suspect that the risk of comparing inadvertantly comparing references when you meant values or vice versa is much higher. So I would try to minimize or avoid them until you understand the difference between compare by reference and compare by value.

But all these things are opt-in. Unless you use them, your old code should behave the way it did before. The proof is in the pudding of course. Just compile it with JDK 6, see what errors / warnings occur and and see if it runs.


I can understand not going to latest and greatest but Java 5.0 is six years old and has been end of life for over a year. I would suggest going to Java 6, which four years old now. These versions tried hard to have backward compatilibilty with Java 1.4. However Java 7, has reduced the backward compatilibity with Java 1.4, so I would suggest migrating to Java 6 before Java 7.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜