Ten Things to Remember When Programming in Java

All programs start with:
void main( ) {
And programs end with:

All lines end with a semi-colon (;). Well, MOST lines do anyways.

Variables cannot contain spaces, or any punctuation, like periods or commas. They can actually only contain letters and numbers. tomAge is a well formed variable, but tom Age and tom-Age are not.

The capitalization of variables (like tomAge), functions (like printLine), keywords (like void), and datatypes (like int and String) in Java matter. This is called "case-sensitive". This means TomsAge is not the same as tomsAge and Void is not the same as void.

You can name variables however you like. Say you are declaring a variable that is supposed to hold your age. You could name it dsf87JKSD. age would be a much better variable name though. age is much more descriptive than dsf87JKSD.

A variable declaration is where we declare what data type a variable is. Declaring variables is usually the first thing you do in a program. A couple example declarations are:

int tomAge;
double candyBarPrice;

A variable assignment is where we assign a value to a variable. These usually come right after declarations and throughout the rest of the program. A couple example declarations are:

tomAge = 21;
candyBarPrice = 0.65;

Don't expect to get your program right the first time. Everyone makes mistakes, and you usually will have several in your program when you start to write it. When you run your program and you get error messages, just read the message, look at the line number (you can use the "Goto Line" feature under the Edit menu to jump to that line), go to that line, and see if you can tell what the error was. Sometimes the line numbers are misleading. The error may be a line or two before or after the line it says the error is on.
Just keep trying to fix errors and Run the program again until you get the message "Compilation successful." and your program pops up.

Some common, and useful, math operators are:

+	addition
-	subtraction
*	multiplication
/	division

halfAge = tomAge / 2.0;
product = 3 * 4;

The most common mistakes made when programming are: Forgetting a semi-colon at the end of a line. Spelling variables, functions, and keywords wrong or with wrong capitalization. Putting quotes " and parentheses ( ) in the wrong places.

Back to Sample Programs     Forward to What Next?