Java Reference
In-Depth Information
public class Welcome {
public static void main(String[] args) {
System.out.println( "Welcome to Java!" );
}
}
Class block
Method block
Caution
Java source programs are case sensitive. It would be wrong, for example, to replace
main in the program with Main .
You have seen several special characters (e.g., { } , // , ; ) in the program. They are
used in almost every program. Table 1.2 summarizes their uses.
case sensitive
special characters
T ABLE 1.2
Special Characters
Character
Name
Description
{}
Opening and closing braces
Denote a block to enclose statements.
()
Opening and closing parentheses
Used with methods.
[]
Opening and closing brackets
Denote an array.
//
Double slashes
Precede a comment line.
Opening and closing quotation marks
Enclose a string (i.e., sequence of characters).
""
;
Semicolon
Mark the end of a statement.
common errors
The most common errors you will make as you learn to program will be syntax errors. Like
any programming language, Java has its own syntax, and you need to write code that con-
forms to the syntax rules. If your program violates a rule—for example, if the semicolon is
missing, a brace is missing, a quotation mark is missing, or a word is misspelled—the Java
compiler will report syntax errors. Try to compile the program with these errors and see what
the compiler reports.
syntax rules
Note
You are probably wondering why the main method is defined this way and why
System.out.println(...) is used to display a message on the console. For the
time being, simply accept that this is how things are done. Your questions will be fully
answered in subsequent chapters.
The program in Listing 1.1 displays one message. Once you understand the program, it is
easy to extend it to display more messages. For example, you can rewrite the program to dis-
play three messages, as shown in Listing 1.2.
L ISTING 1.2 WelcomeWithThreeMessages.java
1 public class WelcomeWithThreeMessages {
2 public static void main(String[] args) {
3 System.out.println( "Programming is fun!" );
4 System.out.println( "Fundamentals First" );
5 System.out.println( "Problem Driven" );
6 }
7 }
class
main method
display message
Programming is fun!
Fundamentals First
Problem Driven
 
Search WWH ::




Custom Search