Java Programming Best Practices Collection by Ahmet Haliloglu

Do not require 100% conformance to rules of thumb!
Do not require 100% conformance to rules of thumb such as the ones listed here! Java allows you program in ways that do not conform to these rules for good reason. Sometimes they provide the only reasonable ways to implement things. And some of these rules make programs less efficient than they might otherwise be, so are meant to be conscientiously broken when performance is an issue.
Declare a local variable where you know its initial value
Declare a local variable only at that point in the code where you know what its initial value should be. Minimizes bad assumptions about values of variables.
Use a new local variable
Declare and initialize a new local variable rather than reusing (reassigning) an existing one whose value happens to no longer be used at that program point. Minimizes bad assumptions about values of variables.
Assign null to any reference variable that is no longer being used
Assign null to any reference variable that is no longer being used. (This includes, especially, elements of arrays.)Enables garbage collection.
Buffer your I/O
I/O operations in Java are by default unbuffered. This means that every single byte written to a stream is taken by the operating system and flushed to disk. This is incredibly inefficient. A common Java idiom is to explicitely insert a buffering class:
InputStream is = new BufferedInputStream( new FileInputStream(f) );

This improves performance a lot, but there's a better and easier way, simply by buffering data yourself. It depends on your application whether this is easy to do or not, but usually it is simply a matter of declaring a local buffer and using buffered I/O operations.

Avoid String concatenation “+=”
String concatenation is a bad practice that illustrates the adverse performance impact of creating large numbers of temporary Java objects. Because Strings are immutable objects, String concatenation results in temporary object creation
that increases Java garbage collection and consequently CPU utilization as well. The solution is to use java.lang.StringBuffer instead of string concatenation.
Minimize * forms of import
Minimize * forms of import. Be precise about what you are importing. Check that all declared imports are actually used. Otherwise readers of your code will have a hard time understanding its context and dependencies. Some people even prefer not using import at all (thus requiring that every class reference be fully dot-qualified), which avoids all possible ambiguity at the expense of requiring more source code changes if package names change.
Use the containers in the standard Java library. Become proficient with their use and you’ll greatly increase your productivity. Prefer ArrayList for sequences, HashSet for sets, HashMap for associative arrays, and LinkedList for stacks (rather than Stack) and queues.
Use method equals instead of operator == when comparing objects
Use method equals instead of operator == when comparing objects. In particular, do not use == to compare Strings. If someone defined an equals method to compare objects, then they want you to use it. Otherwise, the default implementation of Object.equals is just to use ==.
Generally prefer long to int, and double to float. But use int for compatibility with standard Java constructs and classes (for the major example, array indexing, and all of the things this implies, for example about maximum sizes of arrays, etc). Arithmetic overflow and underflow can be 4 billion times less likely with longs than ints; similarly, fewer precision problems occur with doubles than floats. On the other hand, because of limitations in Java atomicity guarantees, use of longs and doubles must be synchronized in cases where use of ints and floats sometimes would not be.
For each class you create, consider including a static public test( ) that contains code to test that class. You don’t need to remove the test code to use the class in a project, and if you make any changes you can easily rerun the tests. This code also provides examples of how to use your class.

 

1