![]() |
|
Nav-Bar |
Java Syntax and Style Guide |
|
|
General syntax
If you've ever programmed C you'll be at home with Java - as it has
got a vitually identical syntax.
command1;
int x = 1 + 2 + 4 * 6;
String name = "Mr Robert Franklin Hugostimyer Granlin Dorkinova-Percival Mikonokkus Rumpinald Smith"; So a newline doesn't mean a command is over - only a semi-colon. Whitespace Whitespace is generally ignored. Look at the following: int
width = 8;
Whitespace can be used to make your program more readable, and extra whitespace will be ignored by the Java compiler. Declaring Variables When you declare a variable like we did above, you can include them all on one line, separating each one with a comma. This of course assumes they are of the same type - ie. you can't mix strings and integers. int xPos=0, yPos=0, width=100, height=80; As always, the command is terminated with the semi-colon. In naming variables, the Java convention is to have the first letter lowercase, and each start of a new word inside the variable name should be uppercase. This is just a convention - it is not checked for by the Java compiler, but it'll make your program more readable if you stick to it. See the above variable names for examples of this (for example, numberOfChildren). Commenting your code Comments are just pieces of text you can insert into your program that explain what a particular command is attempting to do. A lot of programmers under-comments their code, a few get just the right amount (and I mean only a few - usually only when they think somebody else might be reading their code), and some very eager programmers over-comment their code. A comments is completely ignored by the Java compiler, but can act as
an aid to the programmer when coming back to the code at a later date.
Comments in Java can be inserted into source code in two ways:
Indenting code When you define a class, its entire definition is held within braces (curly brackets "{ }"). Also, when you define methods the entire definition is held in braces. You don't put a semi-colon after these. Braces are used in a few other places as well, and whenever you use them you don't need to put a semi-colon after them because the braces mark the beginning and end of the code block. However, it's generally clearer if you indent a few spaces inside the curly brackets draw out the function definitions from the surrounding code. Look at the following: public class Hello
public static void main(String
args[])
In terms of commenting the above code, it is sometimes advised to mark the end of the function or class definitions with a comment saying which method is finishing. Also, the first brace is sometimes placed on the same line as the function name, and the code isn't indented inside the class, only inside the methods. So the above code would thus read: public class Hello{ int variable1 = 0;
public static void main(String args[]){
}//End of Hello class It's up to you which one you find most readable. I'll tend to stick
to the first style above, but there's no neccessary "right" way to indent
code.
There's a very good style guide at
http://www.geosoft.no/javastyle.html
|