Applets are used when we want the images to move
and jump around the screen. But otherwise, any program that operates
on applets can be run on applications and vice versa. Some of the
features of Java programs:
1. There are no line
numbers. In the text, line numbers are used solely to explain. Do
not put line numbers in the program.
2. Comments:
There are two ways of show comments in a Java Program.
a. //.
any coding on a line following two slashes is considered a comment.
Note that the next line is not considered a comment without the two slashes.
b. If you
have a lot of comments, you can enclose them with a /* in the beginning
and a */ in the end. These comments can extend over many lines.
3. Each block of a program
must be enclosed in curly braces { in the beginning and } in the end.
4. Blank lines and spaces can
be used to make the program easier to read. They are ignored by the
compiler.
5. Java is case sensitive.
You have to use the same case (upper or lower) consistently.
6. The program starts with
a class definition. Every program has at least one programmer-defined
or user-defined class. Classes can be public, protected, package
access, or private. Use public since it can be accessed from any
part of the program. The name of the program can be any name except
a keyword. When the program is saved it must have the same name followed
by .java For example in the first exercise, the definition is public
class Welcome1 { and this must be saved as Welcome1.java
7. Semicolons are required
at the end of each statement.
In application programs only:
8. All application programs begin executing at main.
So
the next line will be public static void main( String args[] ). The
parentheses after main indicate that main is a program building
block known as a method. Only one method can be declared
as main and should be written as shown. Void means that the
method does not return any information. Methods are also enclosed
in curly braces { }.
9. The simplest way to print on the screen is System.out.println(
" --------- " ). Whatever is between the quotes will be the screen
output. This is its argument.
10. Another way is using JOptionPane.
Putting \n in the output stream causes the output
after it to be on the next line. \n is not printed, and should be
in the argument enclosed by quotes. This is called an escape sequence.
Table below shows all the escape sequences.
| Escape Sequence | Description |
| \b | backspace |
| \n | New line. Position the screen cursor to the beginning of the next line. |
| \t | Horizontal tab. Move the screen cursor to the next tab stop. |
| \r | Carriage return. Position the screen cursor to the beginning of the current line; do not advance to the next line. Any characters output after the carriage return overwrite the previous character output on that line. |
| \\ | Backslash. Used to print a backslash character. |
| \' | single quote |
| \" | Double quote. Used to print a double quote character. For
example
System.out.println( "\"in quotes\"" ) displays "in quotes" |
In applets program only:
8.