Java Programming Tips

The following Java programming tips should help developers write Java programs that will have fewer bugs / problems and run faster:

1. Avoid “equals” conditional statements where the variable is checked against a constant. Always check the constant against the variable.

If myStr (String) is ever null then the following conditional statement will throw an exception

if (myStr.equals(""))
{
   //  blah, blah, blah
}

Whereas if myStr is null then doing the conditional statement as follows will not generate an exception

if ("".equals(myStr))
{
   //  blah, blah, blah
}

2. Avoid “==” (equal to operator) conditional statements where the variable is on the left of the “==” operator. It is best to have the variable on the right of the “==” operator.

Example: You meant to type “==” but you accidentally typed “=”.

if (myInt = 7)
{
   //  blah, blah, blah
}

“myInt” will be set to 7 and the conditional statement will return true but this is not what you wanted to happen.

The safest way to handle this potential problem from ever happening is to flip around the test:

If (7 == myInt)
{
   //  blah, blah, blah
}

The Java compiler will not allow you to have “7 = myInt”. So, these types of typos will be immediately caught by the developer.

3. Use the following simple method to safely test if a string is blank or null:

public static boolean isNullOrBlank(String myStr)
{
   if (null == myStr)
      return true;
   else
      return ("".equals(myStr));
}

4. Avoid using string concatenation operator (+) since it is extremely slow. Use StringBuffer instead.

Use StringBuffer rather than the string concatenation operator (+). String concatenation is extremely slow.

5. Use compound operators with integers as they are faster.

Compound operators such as n += 4; are faster than n = n + 4; because less bytecode is generated by the Java compiler.

6. Use shifting by powers instead of multiplying since shifting is faster.

Shifting by powers of two is faster than multiplying.

7. Limit the number of temporary objects especially in methods and loops. The creation and deletion of objects is time consuming.

8. Reuse objects where possible.

This is easy to implement.

9. Us Double.toString instead of java.text.DecimalFormat for converting Doubles to Strings.

Note: Double.toString is not very fast, so you may want to search the internet for a faster routine.

10. Follow the KISS principle (i.e. Keep It Short and Simple or Keep It Simple Stupid!)

A method should do one thing and do that one thing well.

11. Use static variables for fields that only need to be assigned once.

Static variables will help to speed up your code.

12. Use ArrayList instead of Vector when possible since ArrayList is faster.

13. Use char[] rather than StringBuffer to convert to Strings when possible, since converting from char[] to a String is faster than StringBuffer to String.

14. Use local variables rather than class variables since it is easier to understand the scope of the (local) variables. Also, the JVM can access local variables faster than class variables.

15. Use logical (meaningful) names for your variables and methods in your Java programs. It will be easier for other people to follow (read) your programs. Also, in 6 months, it will be easier for you to recall what you were doing in the code.

Summary:

If you follow the above 15 Java programming tips, you will have fewer ‘strange bugs’ in your code and your Java code will be faster.

Regards,
Roger Lacroix
Capitalware Inc.

This entry was posted in Java, Programming.

Comments are closed.