Java Reference
In-Depth Information
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.
conforms 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
display 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
Further, you can perform mathematical computations and display the result on the console.
Listing 1.3 gives an example of evaluating 10.5
+
2
*
3
.
45
-
3.5
L ISTING 1.3
ComputeExpression.java
1 public class ComputeExpression {
2 public static void main(String[] args) {
3 System.out.println(( 10.5 + 2 * 3 ) / ( 45 - 3.5 ));
4 }
5 }
class
main method
compute expression
0.39759036144578314
 
 
Search WWH ::




Custom Search