Java Reference
In-Depth Information
Java is case sensitive —uppercase and lowercase letters are distinct—so value and Value
are different (but both valid) identifiers.
Class Body
A left brace (as in line 5), { , begins the body of every class declaration. A corresponding
right brace (at line 11), } , must end each class declaration. Lines 6-10 are indented.
Good Programming Practice 2.3
Indent the entire body of each class declaration one “level” between the left brace and the
right brace that delimit the body of the class. This format emphasizes the class declaration's
structure and makes it easier to read. We use three spaces to form a level of indent—many
programmers prefer two or four spaces. Whatever you choose, use it consistently.
Error-Prevention Tip 2.2
When you type an opening left brace, { , immediately type the closing right brace, } , then
reposition the cursor between the braces and indent to begin typing the body. This practice
helps prevent errors due to missing braces. Many IDEs insert the closing right brace for
you when you type the opening left brace.
Common Programming Error 2.3
It's a syntax error if braces do not occur in matching pairs.
Good Programming Practice 2.4
IDEs typically indent code for you. The Tab key may also be used to indent code. You can
configure each IDE to specify the number of spaces inserted when you press Tab .
Declaring a Method
Line 6
// main method begins execution of Java application
is an end-of-line comment indicating the purpose of lines 7-10 of the program. Line 7
public static void main(String[] args)
is the starting point of every Java application. The parentheses after the identifier main in-
dicate that it's a program building block called a method . Java class declarations normally
contain one or more methods. For a Java application, one of the methods must be called
main and must be defined as shown in line 7; otherwise, the Java Virtual Machine (JVM)
will not execute the application. Methods perform tasks and can return information when
they complete their tasks. We'll explain the purpose of keyword static in Section 3.2.5.
Keyword void indicates that this method will not return any information. Later, we'll see
how a method can return information. For now, simply mimic main 's first line in your
Java applications. In line 7, the String[] args in parentheses is a required part of the
method main 's declaration—we discuss this in Chapter 7.
The left brace in line 8 begins the body of the method declaration . A corresponding
right brace must end it (line 10). Line 9 in the method body is indented between the
braces.
 
Search WWH ::




Custom Search