Sun Java Solaris Communities My SDN Account Join SDN
 
SDN Chat Sessions

New Language features in J2SE 5.0

 

Java Live Transcripts Index

October 12, 2004
Guests: Scott Seligman and Peter von der Ahé
Moderator: Edward Ort (MDR-EdO)

This is a moderated forum.

MDR-EdO: Welcome to today's Java Live chat on New Language Features in Java 2 Platform, Standard Edition (J2SE) 5.0. J2SE 5.0 includes the biggest set of language changes since the original release of the Java programming language. In today's chat, you'll have a chance to ask questions about these new language features and get answers from two key members of the Core Java team: Scott Seligman and Peter von der Ahé. Our guests are ready, so let's begin -- who has the first question?

THE_Void: What do you think about the 'static import' feature? When do you recommend it be used? Don't you think it 'blurs' the OO paradigm?

Scott Seligman: This feature, as some of the others, can be abused fairly easily. I've already seen people going "static import happy" and importing every field or method they can get their hands on. I don't know about blurring the paradigm, but it certainly makes for less transparent code. So, only use static imports when they add readability and don't compromise clarity. Importing the statics RED, GREEN, and BLUE from class Color is good, and PI reads better than Math.PI.

neh1: What is the difference between Abstract class and Interface? When should we use these concepts?

Peter von der Ahé: I prefer using interfaces over abstract classes. Although both define an "interface", abstract classes force implementors to use that exact implementation. For example, had java.util.List not been an interface, no classes could add the List functionality without being a subclass of List. Fortunately, List is an interface and any class can implement it. If you want to provide a default abstract class, look at java.util.AbstractList. Also, see Joshua Bloch's book Effective Java.

Guest: I need help installing JDK1.5 on red hat es 3.0

Scott Seligman: Be sure to let Red Hat know your requirements. :-)

Vladimir Pogorelenko: It may be that generics is the most valuable thing in J2SE 5.0. How does generics support in J2SE 5.0 core java classes affect back-compatibility with previous versions of the language?

Scott Seligman: Backward compatibility was a fundamental consideration in the generics design. Java goes even further, and ensures that it's possible for the clients of an API to be generified before the API is, or for the API to be generified before the clients are. To do this, generics are based on "erasure". The general idea is that generics are essentially visible only at compile time. The compiler uses the generics information to reason about types, and make determinations about what is type-safe and what is not. But the generics information doesn't make it down to the runtime system. That's a bit less flexible, but maximally compatible.

Guest: How do generics influence performance or are they performance neutral?

Scott Seligman: This is almost a follow-up to my previous answer. Since generics are dealt with (almost) entirely at compile-time only, there's no performance impact, either better or worse.

tuxteno: When will javac support SuppressWarnings annotation?

Peter von der Ahé: A personal favorite of mine. Unfortunately, we had to drop it from Tiger, but we are working on it now.

Vladimir Pogorelenko: How does the for-each feature work inside? Does the compiler simply translate code in a regular iterating manner?

Peter von der Ahé: Yes, we don't do any black magic, the compiler does a simple transformation to the code you probably would have written.

glauber: Is the Java language stable now? Or are there more important changes lurking ahead?

Scott Seligman: Expect stability during the very near term -- we don't want to shake up the language with every release. But we're also starting to think about what might be done perhaps two releases out. We've only just started the process, so I don't yet know what changes are coming. Tighter integration with XML? With scripting languages? More generics information propagated to the runtime? A few small tweaks to make programming more fun? Start thinking about what you want to see, and let us know. We'll be very careful to preserve the overall character of the language -- no changes will be considered if they don't "feel" like Java.

Peter von der Ahé: Good examples of stuff you probably never see in Java: operator overloading and multiple inheritance.

skelter: Will I need to use a command line option to the compiler to use Java 1.5 language features? If so, why? It is annoying.

Scott Seligman: No! Sometimes, the good guys win.

daverc: Do you think SuppressWarnings will appear in 1.5.x, or will it have to wait for Java SE 6?

Peter von der Ahé: I have high hopes.

Guest: Will autoboxing performance be comparable to manual casting?

Peter von der Ahé: Yes and no. Manual casting will do autoboxing anyway (you couldn't cast a char to Character before autoboxing). However if you simply do the boxing yourself, you could probably be more clever than the compiler. But don't worry about performance unless you have profiled your application and discovered a bottleneck.

daverc: What is the granularity of annotations? Is it possible to annotate individual lines of code, or just methods? Classes? Entire packages?

Scott Seligman: Annotations are modifiers, much like public, static, or final. So only declarations can be annotated, not individual blocks, statements, or expressions. Annotations may appear on classes and interfaces, packages, methods and constructors, fields, method parameters, and local variables.

Guest: What's the possibility that "Variable args" would interfere with Method overloading?

Peter von der Ahé: You have to be very disciplined when using overloading and varargs to avoid confusion. Generally, I would recommend that you try not to overload varargs methods. On the other hand: method resolution has actually been split in to several stages so varargs shouldn't interfere with existing programs using overloaded methods. The staged overload resolution is described in the language specification.

Aks: Are the libraries (packages such as swing) eventually going to be rewritten to use generics?

Scott Seligman: Some already have been, the most obvious and comprehensive being Collections. There are also many (hundreds) of other places throughout the J2SE 5 APIs where generics are already being used to at least a small extent. This all happened fairly late (too late) in the J2SE 5 development cycle, so yes, there is definitely more being done for the next release. I don't know specifically what's happening with Swing. Sometimes it's tricky to retrofit generics on a library that wasn't designed with generics in mind, so "generifying" a library isn't always the right thing to do. One thing we don't plan to do is create "alternative" generic versions of packages that don't generify well. Better to have an OK but imperfect way of doing things, than to have two different ways.

Guest: What version of Java do I want? I ran update from within Java and it updated to something like SE v 1.4.2.06 not 5.0. Any ideas?

Scott Seligman: After J2SE 5.0 has been out for a little and proven its reliability, the update feature will be set to go grab it. I hope our servers are up to the task -- there will be a lot of clients updating pretty much all at once!

michael: Is there a public place for listening to (and contributing to) the discussion about future of Java?

Peter von der Ahé: You can file a detailed RFE at bugs.sun.com, join the forums at developers.sun.com or java.net. Very soon you should have even more possibilities.

elgi: Does autoboxing affect the performance? Because each time, the compiler has to take care of the conversion between objects, which is not what it says in the manual.

Peter von der Ahé: Since autoboxing can cause a large number of small objects to be created, it can impact performance negatively. However, my advice is: go ahead and use it. If you profile your application and then discover a bottleneck related to autoboxing, then address the issue, for example, switch to arrays.

michael: The spec mentions that Raw types might at some time in the future be discontinued -- that parameterized types must be made concrete to be used. Is this a realistic possibility or just a theoretical point?

Scott Seligman: Consider it a goal. Raw types are a part of the mechanism that enables the compatibility I mentioned earlier. They're inelegant, though, in that what they're doing essentially is bypassing all the typechecking that generics afford, lowering a program's type-safety to the level of pre-generics releases. So J2SE 5 programmers -- try to avoid them. Perhaps some day in the future, after increasingly stringent warnings have been emitted by future javac compilers, we'll be able to do something more to get rid of them.

Guest: I've started moving a large code base over to 5.0. While there's some good information on the changes in 5.0 available, it's scattered across forum postings, blogs, newsletters, etc. Are there plans to provide a single document that explains all of the changes, perhaps with a focus on how best to upgrade an existing code base? (If one already exists, a URL would be much appreciated.)

Peter von der Ahé: I think the single best document right now is this whitepaper on Generics. I expect that the Java Tutorial will be updated with more details on this issue. Hopefully, some good books will come out in the near future.

elgi: Could you please tell me how the metadata feature will be helpful?

Scott Seligman: The key idea is that annotations support a declarative style of programming. Here's one example of things you can do. Instead of writing boilerplate code to enable some feature or service that's being provided by, say, a method, you will now be able to simply declare that information outside of the method. Your code can then rely on tools to generate the boilerplate for it.

MDR-EdO: Well, we've quickly come to the end of our session. I'd like to thank everyone who participated today. We had a nice range of questions. And of course, I'd especially like to thank our guests Scott and Peter for their answers.

Scott Seligman: Thanks for joining us. Keep the feedback coming. Have fun with Tiger!

Peter von der Ahé: Goodbye! If you feel we missed something today, please go to bugs.sun.com and let us know if you have any features you would like to have added or if you discovered a bug. Also the forums at developers.sun.com and java.net are good places to hang out!

MDR-EdO: Last moderator (me) signing off. The forum is now unmoderated.

Rate and Review
Tell us what you think of the content of this page.
Excellent   Good   Fair   Poor  
Comments:
Your email address (no reply is possible without an address):
Sun Privacy Policy

Note: We are not able to respond to all submitted comments.