Java version 1.1.x is recommended. New code should avoid using features from the 1.0.x version, mainly in the graphical interface and I/O areas, that are deprecated.
For Windows and Solaris operating systems, the Java Development Kit (JDK) version 1.1.5 from Sun/Javasoft is recommended. For Solaris systems running 2.6 of the operating system, the 1.1.4 JDK from SunSoft is recommended. This version includes a JIT for much better performance, and uses operating system threads, which may be necessary for applications that use sockets in "C" code. For Windows systems, the performance pack add-on seems to work OK.
For SGI systems running IRIX 5.3, the JDK for IRIX version 3.0.1 (based on Sun JDK 1.1.3) from SGI is recommended. For SGI systems running IRIX 6.X, the JDK for IRIX version 3.1 (based on Sun JDK 1.1.5) is recommended.
The Microsoft Java Software Development Kit (SDK) is not recommended. It uses a different interface to C code, has some subtle incompatibilities with the Sun versions and does not support the complete JDK 1.1 class libraries.
Version 1.0.1 of the Java Foundation Classes (aka "Swing") has been released. This is the primary recommended GUI library. Note that while mixing of core AWT as well as other third party widgets will work at some level, there will be odd effects and the performance will be dramatically lower. Best performance is obtained by using only JFC widgets, and making use of the double buffering option. The JFC infrastructure is then able to track what parts of a display must be (re)drawn.
Documentation may be found on the Sun web site:
Aside from their enhanced functionality, another distinguishing aspect of the JFC and JClass packages is their components are not built on top of the analogous Motif or Windows native components. Instead the components are built from primitive graphics operations using Java code. Hence they will look the same on both unix and Windows platforms, and are more likely to function the same on both as well. And the JFC features a "pluggable look-and-feel" capability in that an application may adopt the "look-and-feel" of Motif, Windows 95, Mac, or various other options with only a single initialization call.
Use of the Microsoft AFC is not recommended. The version released with Internet Explorer 4.0/Microsoft SDK 2.0 only works with the Microsoft JVM. Also, some care is required in mixing it with the core AWT and other 3rd party packages.
The purpose of the recommendations below are to help write code that can be developed and debugged more rapidly, by avoiding a number of common pitfalls. Also, following them will result in code that is more easily maintained and updated in the future.
Many of these recommendations are adapted from Doug Lea as of December 31, 1996. Note there are some differences however.
See also suggested coding standards from Sun
The form of those guidelines is based on example coding standards and checklists in Watts Humphrey's book A Discipline for Software Engineering, Addison-Wesley, 1995, along with feedback (to Doug Lea) from people using previous versions of this document.
Additional useful guidelines may be found in Appendix C of Eckel's book which is available on the web.
Create a new java package for each self-contained
project or group of related functionality. Create and use
directories in accord with java package conventions.
Note that if a product contains multiple subpackages of some
package, the source directory name must end with the complete
package name in order for javadoc to
generate fully cross-referenced documentation. For example for
package x.y.z, the source directory should end in .../x/y/z.
Package names for CDF online products that are intended for possible use outside the lab (such as FISION) should begin with gov.fnal. Package names for products that will not be exported beyond CDF need not begin with anything special. Packages that may be used elsewhere at the lab should perhaps begin with cdf. Each "product" should have a separate package name. Sometimes it may be worthwhile for a single product to contain multiple sub-packages.
Consider writing an index.html file in the doc
sub-directory of a product briefly outlining the purpose and
structure of the package.
Place each class in a separate file. This applies even to non-public classes (which are allowed by the Java compiler to be placed in the same file as the main class using them) except in the case of one-shot usages where the non-public class cannot conceivably be used outside of its context.
Begin each file with a comment including:
package,
briefly describe the rationale for constructing the
package. Immediately follow each file header with:
package name import list. Example:
/* File: Example.java */ package demo; import java.util.NoSuchElementException;
Write all /** ... */ comments using javadoc
conventions.
Preface each class with a /** ... */ comment
describing the purpose of the class, guaranteed invariants, usage
instructions, and/or usage examples. Also include any reminders
or disclaimers about required or desired improvements. Use HTML
format, with added tags:
@author author-name @version version number of class @see string @see URL @see classname#methodname Example:
/**
* A class representing a window on the screen.
* For example:
* <pre>
* Window win = new Window(parent);
* win.show();
* </pre>
*
* @see awt.BaseWindow
* @see awt.Button
* @version 1.2 31 Jan 1995
* @author Bozo the Clown
*/
class Window extends BaseWindow {
...
}
Use javadoc conventions to describe nature, purpose, constraints, and usage of instances variables and static variables. Use HTML format, with added tags:
@see string @see URL @see classname#methodname Example:
/**
* The current number of elements.
* must be non-negative, and less than or equal to capacity.
*/
protected int count_;
Use javadoc conventions to describe nature, purpose, preconditions, effects, algorithmic notes, usage instructions, reminders, etc. Use HTML format, with added tags:
@param paramName description.@return description of return value @exception exceptionName
description @see string @see URL @see classname#methodname Be as precise as reasonably possible in documenting effects.
Use /* ... */ comments to describe algorithmic
details, notes, and related documentation that spans more than a
few code statements.
Example:
/*
* Strategy:
* 1. Find the node
* 2. Clone it
* 3. Ask inserter to add clone
* 4. If successful, delete node
*/
Use Running // comments to clarify non-obvious
code. But don't bother adding such comments to obvious code;
instead try to make code obvious!
Example:
int index = -1; // -1 serves as flag meaning the index isn't valid
Use any consistent set of choices for code layout, including:
{'') placement at end of line
or beginning of next line. lowercase. CapitalizedWithInternalWordsAlsoCapitalized ClassNameEndsWithException. InterfaceNameEndsWithInterface. ClassNameEndsWithImplUPPER_CASE_WITH_UNDERSCORES trailingUnderscore_ (Yes, this is ugly. It
is good that it is ugly!) twoTrailingUnderscores__ firstWordLowerCaseButInternalWordsCapitalizedfirstWordLowerCaseButInternalWordsCapitalized()
newX toX X getX(). (Adhere to javabeans
specification)setX(X value). (Adhere to javabeans
specification)imports are actually used. main for
the principal class in each program file. The main
should provide a simple unit test or demo. main
should be separate from those containing normal classes. Cloneable
and/or Serializable. final only if it is a
subclass or implementation of a class or interface
declaring all of its non-implementation-specific methods.
(And similarly for final methods). null). final and/or comment conventions to
indicate whether instance variables that never have their
values changed after construction are intended to be
constant (immutable) for the lifetime of the object
(versus those that just so happen not to get assigned in
a class, but could in a subclass). protected to private.
protected
variables and methods is harder, since you you have to
either loosen or check assumptions about their
properties. (Note that in Java, protected
methods are also accessible from unrelated classes in the
same package. There is hardly every any reason to exploit
this though.) get/set-style methods only
when they are intrinsic aspects of functionality. protected access and
update methods instead (or sometimes public
ones if they exist anyway). Type[] arrayName
rather than Type arrayName[]. statics have
sensible values even if no instances are ever created.
(Similarly ensure that static methods can be
executed sensibly.) Use static intitializers (static
{ ... } ) if necessary. Stack,
prefer having two methods Object top() and void
removeTop() versus the single method Object
pop() that does both. Object) and
using conditionals checking instanceof.
Alternatives include techniques such as
double-dispatching, or often best, reformulating methods
(and/or those of their arguments) to remove dependence on
exact argument type.
class Classifier {
String identify(Object x) { return "object"; }
String identify(Integer x) { return "integer"; }
}
class Relay {
String relay(Object obj) { return (new Classifier()).identify(obj); }
}
public class App {
public static void main(String[] args) {
Relay relayer = new Relay();
Integer i = new Integer(17);
System.out.println(relayer.relay(i));
}
}
synchronized methods to synchronized
blocks. readObject and WriteObject
if a Serializable class relies on any state
that could differe across processes, including, in
particular, hadhCodes. implement Cloneable). clone
might not do what you want. wait
abstract methods in base classes to
those with default no-op implementations. (Also, if there
is a common default implementation, consider instead
writing it as a protected method so that
subclass authors can just write a one-line implementation
to call the default.) abstract methods, avoiding
problems occurring when they forget to do so but should
have. equals instead of operator ==
when comparing objects. In particular, do not use ==
to compare Strings. equals
method to compare objects, then they want you to use it.
Otherwise, the default implementation of Object.equals
is just to use ==. wait statements in while
loops that re-wait if the condition being waited for does
not hold. wait wakes up, it does not
know if the condition it is waiting for is true or not. notifyAll instead of notify
or resume. notify can
normally only support at most one kind of wait condition
across all methods in the class and all possible
subclasses. And unguarded suspends/resumes
are even more fragile. null to any reference variable that
is no longer being used. (This includes, especially,
elements of arrays.) ='') inside if
and while conditions. ='' should
have been ``=='' except when the
variable is a boolean. catch for
all unchecked exceptions that can be dealt with.
java.util.NoSuchElementException.
Declare and catch them anyway.
C cx = null;
if (x instanceof C) cx = (C)x;
else evasiveAction();
Rationale: This forces you to consider what to do if
the object is not an instance of the intended
class rather than just generating a ClassCastException.
For some other standards and style guides, see
Last modified: March 2, 1998