JDC Tech Tips No. 1
September 4, 1997

Welcome to the first issue of the Java Developer Connection(sm) Tech Tips. You'll find out about using Java Archiver files and get a performance tip for garbage collection. And send your comments to JDCTechTips@Sun.com--we'll put your feedback to good use. The JDC Team-

Have you tried using JAR files for archiving? You may have heard of a UNIX tool called "tar," or tape archiver, that's used to group files together for backup purposes. In the Java Development Kit 1.1 and later releases, there's a similar facility known as "jar," or Java Archiver. Here's a way to use this tool.

A JAR archive is created with the following:

        $ jar cf archive.jar file1 file2 ...
After you create the archive, you can list the contents with:
        $ jar tf archive.jar
and files can be extracted with the following:
        $ jar xf archive.jar [file1 file2 ...]
Why is the JAR facility important? After all, various ZIP tool versions exist for archiving and compressing files. One important use of JAR is for bundling Java .class files. A group of such files could constitute a Java package or library. For example, with Windows 95 or NT you might say:
        CLASSPATH="lib1.jar;lib2.jar;"
Another very important JAR use is for optimizing applet loading. Consider this example with a simple applet:
        // applet.java
        import java.awt.*;
        import java.applet.*;

        public class applet extends Applet {
            public void paint(Graphics g)
            {
                String s = Message.getIntro(getParameter("intro"));
                g.drawString(s, 25, 25);
            }
        }
that accesses an auxiliary class:
        // Message.java
        public class Message {
            public static String getIntro(String t)
            {
                if (t != null)
                    return t;
                else
                    return "Hello world!";
            }
        }
You can invoke this applet with some HTML code, such as:
        <html>
        <head><title>Hello World Example Applet </title></head>
        <body>
        <applet code="applet.class" archive="applet.jar" width=150
            height=150>
        <param name="intro" value="good morning"></applet>
        </body>
        </html>
Note the "archive" attribute. Without this setting, each subsidiary class that's loaded, such as Message, would involve a separate request to the server holding the HTML page. But with the JAR file, the various .class files can be downloaded more efficiently.

In this example, you prepare archive.jar with the following:

        $ javac applet.java
        $ jar cf applet.jar *.class
that is, construct the JAR file from all of the .class files.

A final note: JAR files are also used with JavaBeans.

Here's a performance tip: garbage collection and setting to null. Java uses garbage collection, or reclaiming no-longer-used storage, rather than requiring you to explicitly manage storage. Garbage collection is automatic, but sometimes there are ways to help it out. Imagine a case where you're managing a stack of Object references:

        public class Stack {
            private static final int MAXLEN = 10;
            private Object stk[] = new Object[MAXLEN];
            private int stkp = -1;

            public void push(Object p) {stk[++stkp] = p;}

            public Object pop() {return stk[stkp--];}
        }
Now consider a case where the stack has two elements on it, and you pop one of them. At this point stk[0] will have a valid element in it, and stk[1] will have the element just popped. That is, stk[1] will have a reference to an Object, which could be a reference to anything, including a large data structure of many thousands of bytes. In such a case, this data structure cannot be garbage collected, even though it may no longer be in use.

To remedy this problem, you can rewrite pop like so:

        public Object pop()
        {
            Object p = stk[stkp];
            stk[stkp--] = null;
            return p;
        }
You haven't nullified the Object itself, just a reference to it that's no longer valid. The Stack object itself may have a long lifetime, and rewriting the pop method in this way helps ensure that garbage collection gets done in a timely fashion.

JDC Tech Tips No. 01
September 4, 1997

-- FEEDBACK --

Comments? Send your feedback on the JDC Tech Tips to: JDCTechTips@Sun.com

-- SUBSCRIBE/UNSUBSCRIBE --

There's good information coming your way from the JDC, so stay tuned. But if your mailbox is just too full, and you want to unsubscribe, log in to the JDC and go to: http://developer.javasoft.com/servlet/RegisterServlet

Type your password, and uncheck the box that says "It's okay to send me JDC Email."

-- COPYRIGHT --

Copyright Sun Microsystems, Inc. All rights reserved. 901 San Antonio road, Palo Alto, California 94303 U.S.A.

This document is protected by copyright. For more information see: http://developer.javasoft.com/developer/copyright.html