Program Format:
//printing a message on the screen
//notice the format of the code
public class HelloClass {
public static void main (String[ ] args) {
System.out.println
("Hello, Java world!");
System.out.println ("I
plan to be a Java expert!");
}
}
|
Notice the format
style that we will be using. The indentations keep the
code clearly visible and easy to read.
It is possible, in
Java, to write all of your code on one line -- this is called free
form style. Free form style is extremely difficult to debug at
a later date and is nearly impossible for a programming team to
decipher. We will NOT
be using free form style.
|
Case sensitivity:
if (netpay > grosspay)
If (NetPay > GrossPay)
IF (NETPAY > GROSSPAY)
|
Java is very picky
about your caps lock key. The three lines of code at the left,
at first glance, may appear to all say the same thing. The
Java compiler, however, will only execute the first line of code.
Most Java code is written in smaller case and ALL reserved words
(such as "if") MUST be written in smaller case.
|
Comments:
//printing another message on the screen
//notice the commented lines
public class HelloAgainClass {
public static void main (String[ ] args) {
System.out.println
("Hello!"); //first
print
System.out.println ("I
just love this Java!");
}
}
/*sometimes comments are longer statements
that
wrap around to the next line on the screen*/
|
In Java, comments may be expressed in different forms.
The comments
beginning with // are single line comments. They can appear on a line by themselves, or they
may follow other lines of code.
The comments enclosed
within /* and */ are used for
longer comments that wrap around a line.
|
Blank Space:
//notice the spacing in this code
public class HelloStillClass {
public static void main (String[ ] args) {
System.out.println
("Java rocks!");
System.out.println ("A
real space cadet!");
}
}
|
The compiler ignores
extra blanks between words and symbols.
Blank lines between
lines of code are also ignored. Notice the
blank line between the two print statements.
You cannot, however, embed blanks in variable names.
The use of blanks can improve readability when appropriately placed. |