Java Reference
In-Depth Information
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
further 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 );
}
}
1.8 Creating, Compiling, and Executing a Java Program
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.6. If your program has compile errors, you have to modify
the program to fix them, and 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.
Sections 1.10 and 1.11 will introduce developing Java programs using NetBeans and Eclipse.
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.7.
command window
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 .
file name Welcome.java,
A Java compiler translates a Java source file into a Java bytecode file. The following com-
mand compiles Welcome.java :
compile
javac Welcome.java
Note
You must first install and configure the JDK before you can compile and run programs.
See Supplement I.B, Installing and Configuring JDK 8, for how to install the JDK and set
up the environment to compile and run Java programs. If you have trouble compiling
and running programs, see Supplement I.C, Compiling and Running Java from the
Command Window. This supplement also explains how to use basic DOS commands
and how to use Windows Notepad to create and edit files. All the supplements are
accessible from the Companion Website at www.cs.armstrong.edu/liang/intro10e/
supplement.html .
Supplement I.B
Supplement I.C
If there aren't any syntax errors, the compiler generates a bytecode file with a .class
extension. Thus, the preceding command generates a file named Welcome.class , as shown
.class bytecode file
 
 
 
Search WWH ::




Custom Search