Java Reference
In-Depth Information
Further, you can perform mathematical computations and display the result on the console.
10.5
+
2
*
3
Listing 1.3 gives an example of evaluating
.
45
-
3.5
L
ISTING
1.3
ComputeExpression.java
1
public class
ComputeExpression {
2
class
main
method
compute expression
public static void
main(String[] args) {
3
System.out.println((
10.5
+
2
*
3
) / (
45
-
3.5
));
4 }
5 }
0.39759036144578314
The multiplication operator in Java is
*
. As you can see, it is a straightforward process to
translate an arithmetic expression to a Java expression. We will discuss Java expressions fur-
ther in Chapter 2.
1.28
✓
✓
What is a keyword? List some Java keywords.
Check
1.29
Is Java case sensitive? What is the case for Java keywords?
Point
1.30
What is a comment? Is the comment ignored by the compiler? How do you denote a
comment line and a comment paragraph?
1.31
What is the statement to display a string on the console?
1.32
Show the output of the following code:
public class
Test {
public static void
main(String[] args) {
System.out.println(
"3.5 * 4 / 2 - 2.5 is "
);
System.out.println(
3.5
*
4
/
2
-
2.5
);
}
}
You save a Java program in a .java file and compile it into a .class file. The .class file
is executed by the Java Virtual Machine.
Key
Point
You have to create your program and compile it before it can be executed. This process is
repetitive, as shown in Figure 1.14. If your program has compile errors, you have to modify
the program to fix them, then recompile it. If your program has runtime errors or does not
produce the correct result, you have to modify the program, recompile it, and execute it
again.
You can use any text editor or IDE to create and edit a Java source-code file. This section
demonstrates how to create, compile, and run Java programs from a command window. If you
wish to use an
IDE
such as Eclipse, NetBeans, or TextPad, refer to Supplement II for tutori-
als. From the command window, you can use a text editor such as Notepad to create the Java
source-code file, as shown in Figure 1.15.
VideoNote
Eclipse brief tutorial
command window
IDE Supplements
Note
The source file must end with the extension
.java
and must have the same exact
name as the public class name. For example, the file for the source code in Listing 1.1
should be named
Welcome.java
, since the public class name is
Welcome
.
VideoNote
NetBeans brief tutorial
file name