Java Technology Home Page
A-Z Index

Java Developer Connection(SM)
Chat

Downloads, APIs, Documentation
Java Developer Connection
Tutorials, Tech Articles, Training
Online Support
Community Discussion
News & Events from Everywhere
Products from Everywhere
How Java Technology is Used Worldwide
Print Button
 
JavaLive Transcripts Index

Effective JavaTM Programming
June 27, 2000

Speaker Josh Bloch (jbloch)

Moderator Edward Ort (MDR-edO)

This is a moderated forum.

MDR-edO Welcome to Java Live! Today's guest is Joshua Bloch, Sun senior staff engineer. Josh is the architect of a number of core Java libraries. He'll answer your questions about JavaTM programming, particularly in the language and core library area (java.lang, java.util, and so forth) So let's begin. Who has the first question?

talk: Can you explain what are audio clips in Java?

jbloch: Audio clips are digitized sound samples. The term comes from the bad old days when you would actually clip the tape with a razor. Java originally had a minimal sound clip API as part of applet. Now it has a fancy full-featured sound API (javax.sound, introduced in 1.3).

ramsey: Hello, how do I create a shaped window in AWT and what is a peer and how does it relate to this question? Also, are there any good books or URL's you can recommend on on this ?

jbloch: This is a GUI question, which is pretty much outside my area of expertise. There are a *lot* of good online resources. I'd start with the Java tutorial.

zergworld: I have a language question. I read that private base class members are inherited, yet they are not visible to a subclass. Being non-visible, they cannot be overridden yet a subclass can contain a private member with the same signature. In what sense is an item inherited if it's not visible, and in what sense is it not overridden?

jbloch: While such members aren't visible to a subclass, they are visible to a nested (inner) class. Such a member is not overridden in the sense that a like-named member of a subclass really has nothing to do with the superclass member.

Ben Reaves: Hello. One of our developers is using java.util.Arrays (new with JDKTM 1.2) but we must support 1.1 for one of our customers. I've heard of a jar or zip file that we could send with our product to enable this. Where can I find it? Any restrictions on distribution?

jbloch: Ben: The 1.1 backport of the Collections Framework (including Arrays) is available off our InfoBus web page. It's still available, and it is redistributable. I don't have the URL handy, but I'll add it to the transcript of this chat later.

Chris Rijk: Athough the base language is mostly pretty simple, it seems there are often a lot of hidden gems in the libraries or using the base language. I for one only recently came across the java.lang.ref stuff, which made me very happy. How do you think this situation compares with other languages? Your book seems to be the first Java 'cookbook' and looks pretty cool from what I've seen so far.

jbloch: It is true that there are hidden gems lying around. Unfortunately, it's still sort of an "oral tradition" (gems passed down from programmer to programmer). There are a few good books out there (such as Doug Lea's threading book). I'm glad you like my book! I can't wait to finish it.

zergworld: Do you think Microsoft's C# will pose any threat to Java?

jbloch: Zergworl: Probably not. I only glanced at the documentation, but it's really a very different language. (Not WORA, allows unsafe pointer usage, etc.)

jw: Hello. Concerning the JMF API's and Java3DTM, how is it possible to stop the garbage collector from interrupting some media presentation without visible/audible artifacts?

jbloch: Unfortunately it's not possible to stop the GC, per se, but it is possible to minimize the effect. First of all, use a good JVM with an incremental GC (like Hotspot!). Second of all, minimize the amount of garbage that you generate. Finally, you might consider forcing a GC before you start doing "soft-realtime" activity with System.gc() (or whatever it's called).

jchu: to walk through a vector, should I use an enumeration or just a for loop?

jbloch: First of all, I'd use an Iterator rather than an Enumeration, because the method names are shorter/nicer:

    for (Iterator it = vec.iterator(); i.hasNext();  )
        foo(i.next());
If you want it to run as fast as possible, it's fine to index:
    for (int i=0, n=vec.size(); i<n; i++)
        foo(vec.get(i)); 
Both idioms are fine.

Chris Rijk: Comment to Zergworl: I don't think C# stands much chance of convincing current Java developers to switch to C#, though it may stop a few MS developers swapping to Java (now). Look at www.javalobby.com, which has quite a bit of technical discussion about this 'new' language.

zergworld: why don't collections support primitives?

jbloch: Because the Java programming language has a "bimodal" type system. There is no type that subsumes references and primitives. It really would be out of character, and would blow up the size of the collections library horribly.

jw: On what grounds should I decide to use ArrayList instead of Vector ?

jbloch: Vector is pretty much obsolete; just use ArrayList. (If you need automatic synchronization, you might consider Vector, but you could also use Collections.synchronizedList around an ArrayList). This stuff is discussed in excruciating detail in my Collections tutorial, which is part of The Java Tutorial (Addison Wesley, or free on our web site!)

ramsey: I am working on an applet for a client. I was hoping to distribute JMF classes with the applet via a jar file. Is this possible? If it is possible, can I rip out only those classes that I require? (What is the name of your book?)

jbloch: This question falls into the "deployment" area, which isn't really my area of expertise. It is certainly possible if the user has installed the JMF extension. In addition, we have a cool new deployment technology that was announced at JavaOne (I think it was code named "Jump") that can be used to download the extension on demand.

jbloch: To answer your other question, the book will be called Effective Java Programming. It will be available at the end of this year (or the beginning of next).

Chris Rijk: Java doesn't have many syntactic sugars. However, the only one I miss is 'unless' - ie doing 'unless (XX) {}' instead of 'if (!XX) {}'. There doesn't seem to be an RFE for this at the moment--do you think it has much chance of getting into the core if one was submitted?

jbloch: The bar for new keywords is ENORMOUSLY high at this point. I had a difficult time getting " assert" in, and that had 1000 "bug parade" votes. There are many nice pieces of syntactic sugar that Java is missing (such as a foreach keyword for Collections), but I'm afraid that it's a bit too late.

MDR-edO: What's your biggest pet peeve regarding bad/inefficient techniques that you often see in Java programs?

jbloch: One pet peeve is the excessive use of strings as keys in place of a class. Java has this lovely type system, but sometimes people don't use it. As a result, they get fragile programs that blow up with runtime errors.

jbloch: Another pet peeve is excessive use of the String concatenation operator. It's very easy to write quadratic code that looks like it should run in linear time.

jbloch: I'll have lots more such examples in my book (they aren't coming to mind right now). Watch out for more chapters on the JDC web site!

Chris Rijk: Comment on 'Jump' project. I think you mean JavaTM Web Start

jbloch: Chris: Thanks for the URL!!

Matt Welsh: I notice that in Gilad's examples of Generics the collection classes are often used as convenient examples that would benefit from their use. How do you feel about the addition of Generics?

jbloch: Matt: I'm glad that you asked that question! I'm an ARDENT supporter of Generics, one of the biggest proponents within Sun. There's a GREAT synergy with Collections. Now, many people don't use Collections in their APIs because they lose static (compile-time) type checking. They'll get it back once Java gets Generics! Besides, I hate typing all of those redundant casts.

jw: Can you please elaborate on your comment about using classes instead of String's as keys ?

jbloch: In some sense, the typesafe enum pattern is "classes instead of Strings." People sometimes write APIs where you pass in a String to identify some desired behavior. If you spell it wrong, you don't find out that you have a problem until runtime.

Ernesto: Where can I find more information about Generics and their use?

jbloch: You could look at the GJ website. (GJ is a research prototype of Java with Generics.) Also, you could look at the Community Process JSR for adding generics to Java. Finally, you could look at a textbook for a language that already supports generics.

Chris Rijk: From the recently published list of what's expected in Merlin (J2SE 1.4) it doesn't look like generics are going to be in for some time--it wasn't even listed! Was this an oversight, or will be have to wait until J2SE 1.5? Here is the list.

jbloch: I regret to say that it's not an oversight. When the powers that be were deciding what to pursue, they came to the conclusion that there was no business case for adding Generics to Merlin. However, the Merlin feature list is still in the Community Process (JSR-59). If you feel that it's important to add them to Merlin, make your voice heard. Public review is underway.

Matt Welsh: The Generic Java (GJ) website has articles of varying complexity dealing with the issues.

jtr: Re: Strings as keys... the internationalization (ResourceBundles) use strings as keys. It seems like something could be done about this, and I've hacked more typesafe ways, but I wish this could be looked at in more detail.

ramsey1: What do you mean by strings as keys in place of a class? When reading a file, I create a stringbuffer as StringBuffer sb = new StringBuffer( 1024*1024); Is that conside bad code? I am downloading a url in a loop.

jbloch: In regards to allocating your own 1-meg buffer, while it's generally a bad idea to try to "outsmart the GC", when you need a huge buffer frequently, (for instance, to hold an image) it's often a reasonable idea to maintain one (or several) buffers, rather than allocating one each time you need it. But generally speaking, you should ***NOT** do your own object pools. Just let the GC do what it's good at.

bingster: Does Java 2 support getting non-text items from the system clipboard?

jbloch: This is also beyond my area of expertise. I believe that it is possible with the DataFlavor API, but that there are limitations. Perhaps someone else out there knows the answer?

zergworld: Would it be of any value (or is this also out of character for Java) to create a flashrom or hot-swappable*javachip* that contains a hardware-based VM?

jbloch: This idea has been proposed, and even implemented. But even with the performance advantages of custom hardware, it's hard to compete with general purpose PCs that were developed to sell in the millions. Processor manufacturers can afford to put an awful lot of resources into making these things run fast.

jw: Are you planning to publish a CD-version of 'Effective Java' too (like Scott Meyers did on C++)? Maybe putting on (a CD) some clickable demo-App's for comparing effective vs. in-effective Java would have more impact on developer's efforts...

jbloch: Good idea! I'll bring it up to Addison Wesley and see what they have to say.

MDR-edO: For those of you who haven't yet done so, get a sneak peek at Josh's book by reading Chapter 2: Substitutes for Missing C Constructs.

jtr: Does your book talk about creating very large programs in Java? I have to deal with these on a daily basis. The number of classes can be a problem. When you have to download, classload, verify, and so forth ... a large number of classes, it is slow, and memory gets used up quickly.

jbloch: I do discuss coping with the complexity that arises, but not deployment issues (that is, coping with downloads). There is work underway to download things in smaller pieces to eliminate long pauses.

jw: Can you recommend a book about "Java-with-UML/Java-Design"?

jbloch: I'm not sure if it's strongly Java-centric, but I believe that Martin Fowler's UML book is pretty well liked. His "Refactoring" book IS Java-centric, and contains some very good advice, especially in the "Bad Smells in Code" chapter.

ramsey1: Thanks, I was going to ask for the url. You said that ArrayList is more suitable than Vector for list. Is ArrayList supported by applets?

jbloch: Oh, deployment issues raise their ugly head again. You correctly imply that the Collections framework is part of Java 2, and that applets are, practically speaking, restricted to 1.1 APIs. If you're writing an applet, you're probably stuck with Vector and Hashtable.

MDR-edO Well our hour has quickly drawn to a close. Thanks for participating. And special thanks to our guest, Josh Bloch.

jbloch: Wow, that was fast. Well, I'm sorry that I didn't get to all of your questions, but if you have any other questions you can send them to me at jbloch@eng.sun.com. My parting words of advice: keep it simple, and don't cut-and-paste code.

MDR-edO Signing off

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

_______
1 As used on this web site, the terms "Java virtual machine" or "JVM" mean a virtual machine for the Java platform.

coffeecup


Reader Feedback

Tell us what you think of this transcript.

Duke

Very worth reading Worth reading Not worth reading

If you have other comments or ideas for future articles, please type them here


Print Button
[ This page was updated: 21-Sep-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary | Feedback | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first.
Sun Microsystems, Inc.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved. Terms of Use. Privacy Policy.