Designing With Java: Common Java Mistakes And How To Avoid Them

How Java works: Here are a few common Java coding errors and programming traps. Also included are tips on how to craft better code to keep from having to debug errors.

Trying to debug a Java coding error can be about as much fun as hunting with a vegetarian. Here are a few Java programming traps programmers frequently fall into, along with tips on how to craft better code.

Spelling DOES Count!

It's just like your seventh grade teacher told you: spelling counts. If you don't spell your codes properly, they won't work. On top of that, attention to detail is critical here because, in addition to putting the letters in the right order, you also have to be cognizant of what case you use (UPPER vs. lower). Java, in addition to requiring words to be spelled correctly is also case sensitive.

One way to look at this is that computers are basically ignorant - they can't understand (compile) our instructions very well unless we tell them exactly what we want in exactly the way they can understand it. (This is true of any language - you wouldn't understand this sentence very well if I threw all of its letters into a box and drew them out in any old order.)

Below are case rules for entity names:

Package names are always lowercase. Examples: java.lang.ref, com.mycompanypackage

Classes and Interfaces always begin with a capital letter. Examples: CompanyApp, Checksum.

Method names consist of verbs that have the first letter lowercase and each subsequent word capitalized. Examples: compareTo, getNumber.

Variables consist of nouns that have the first letter lowercase and each subsequent word capitalized. Examples: keyBytes

Constants should be typed as all capital letters with words separated by underscores. Examples:

Punctuation Counts Too!

Semicolons, parentheses and brackets are used frequently in Java - think of them as the punctuation building blocks of coding. Semicolons are used to end Java statements (similar to how a period is used to end a sentence), while brackets group statements into comprehensible codes.

If you are a Java programmer, chances are good that you've misplaced (or forgotten altogether) a semicolon from time to time. Since a semicolon is necessary to end a Java statement, compilers are pretty good at pointing this type of error out to you. It doesn't hurt to comb through your code and check this out for yourself, however.

Likewise, you should also look for opening and closing parentheses ( ) and brackets { } - these always need to come in pairs. Missing a single parenthesis or bracket can cause a compiler to produce a sizeable list of errors. One easy way to avoid this type of error is to always create the closing parenthesis or bracket at the same time you create the opening one, and then type the code in-between.

All Things Being Equal

In Java, a single equal sign ( = ) is an entirely different operator than a double equal sign ( == ). In most cases, use the double equal sign when creating a loop or conditional statement and use the single equal sign everywhere else. For example:

To compare a and b for equality, use a==b; (note the double equal sign).

Where b has the same value as a, use a=b; (note the single equal sign).

It might help if you translate the single equal sign into the words "gets assigned." In the above example this would translate to "a gets assigned b."

These simple tips won't solve all your Java coding problems - but they will give you a great head start on beating your compiler into submission!

© High Speed Ventures 2011