Java Reference
In-Depth Information
Line 1 defines a class. Every Java program must have at least one class. Each class has a
name. By convention, class names start with an uppercase letter. In this example, the class
name is Welcome .
Line 2 defines the main method. The program is executed from the main method. A class
may contain several methods. The main method is the entry point where the program begins
execution.
A method is a construct that contains statements. The main method in this program con-
tains the System.out.println statement. This statement displays the string Welcome to
Java! on the console (line 4). String is a programming term meaning a sequence of charac-
ters. A string must be enclosed in double quotation marks. Every statement in Java ends with
a semicolon ( ; ), known as the statement terminator.
Reserved words , or keywords , have a specific meaning to the compiler and cannot be used
for other purposes in the program. For example, when the compiler sees the word class , it
understands that the word after class is the name for the class. Other reserved words in this
program are public , static , and void .
Line 3 is a comment that documents what the program is and how it is constructed. Comments
help programmers to communicate and understand the program. They are not programming
statements and thus are ignored by the compiler. In Java, comments are preceded by two
slashes ( // ) on a line, called a line comment, or enclosed between /* and */ on one or several
lines, called a block comment or paragraph comment . When the compiler sees // , it ignores
all text after // on the same line. When it sees /* , it scans for the next */ and ignores any text
between /* and */ . Here are examples of comments:
class name
main method
string
statement terminator
reserved word
keyword
comment
line comment
block comment
// This application program displays Welcome to Java!
/* This application program displays Welcome to Java! */
/* This application program
displays Welcome to Java! */
A pair of curly braces in a program forms a block that groups the program's components.
In Java, each block begins with an opening brace ( { ) and ends with a closing brace ( } ). Every
class has a class block that groups the data and methods of the class. Similarly, every method
has a method block that groups the statements in the method. Blocks can be nested , meaning
that one block can be placed within another, as shown in the following code.
block
public class Welcome {
public static void main(String[] args) {
System.out.println( "Welcome to Java!" );
}
Class block
Method block
}
Tip
An opening brace must be matched by a closing brace. Whenever you type an opening
brace, immediately type a closing brace to prevent the missing-brace error. Most Java
IDEs automatically insert the closing brace for each opening brace.
match braces
Caution
Java source programs are case sensitive. It would be wrong, for example, to replace
main in the program with Main .
case sensitive
You have seen several special characters (e.g., { } , // , ; ) in the program. They are used
in almost every program. TableĀ 1.2 summarizes their uses.
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
special characters
common errors
 
 
Search WWH ::




Custom Search