Technical Articles

Java Tips

  • Though IE 4.0 is JDK1.1 compliant, it still calls the deprecated JDK1.0 method preferredSize() of components instead of getPreferredSize(). No you cannot define the actual method in getPreferredSize() and call it from preferredSize() because internally the two methods are interconnected which results in an infinite loop. The solution is to define both the methods with identical body and live with the compile time warning for using deprecated methods.
  • You request for a font which is not available with the JVM. Normally you wouldn't want the JVMs to switch the font family. At least it should not change a fixed width font to a proportional font. But with IE (4.01) you can never be sure. It might be possible to tinker around with the font.properties file to prevent this, but first of all I will have to locate it!
  • Multithreaded applet. Two threads to process requests and the AWT threads pushes requests into the processors request queue on action events. After inserting the request the AWT thread wakes up the processor thread. In a particular situation an AWT object had two action listeners registered. The first action listener inserts a request which results in disabling the component. This causes the JVM to freeze. As always, happens in IE only (4.01).
  • The z-order of components added to a panel in Netscape older versions (before 4.0) was reverse of the standard behavior. The new component floats up instead of sinking down.
  • You can embed images and configuration files in jar files and use getResource() to retrieve them. But Netscape considers it as a potential security hole and does not allow that. They suggest getResourceAsStream() instead. That does not work in Netscape 4.05 default version which is supposed to be JDK1.1 enabled.
  • If a component is not yet added to a container or is not visible, then some AWT methods like setFocus(), createImage() etc. might fail.
  • You opened a client socket to a server. If the server closes the socket, the client won't know about that till it tries to read. Write may go through without any errors.
  • If you do not close open sockets before closing IE 4.01 on Win95, IE gives GPF.
  • IE does not call stop() and destroy() of an active applet when the browser window is closed.
  • Older versions of Netscape call init() multiple times instead of start().
  • Off screen painting might not be faster on all machines! It is machine dependent. Also keep in mind that you will be using more memory by maintaining an offscreen image.
  • Scroll panel on IE 4.0 displays scroll bars even when the component size is exactly equal to the container size. It should be sufficiently less than the container size.
  • It helps a lot to make all un-inherited classes final. If you have lots of such classes, do this change and watch the performance boost!
  • Do not rely too much on the java optimizers, at least not at this stage and not the standard ones. Do as much optimizations as possible yourself. Use javap to look into the compiled byte code and you will see how an innocent looking line of code becomes a monster in byte codes.
  • Do not use too many unnecessary type conversions. Keep all types uniform as far as possible.
  • Reuse classes as much as possible. You will be saving calls to the memory manager, garbage collector, constructor, and god knows what else.
  • Use short names for packages, methods and classes. The full names are stored inside the java classes. So the shorter the names, the small are your classes.