3 Programming Environment
3 Programming Environment
3 Programming Environment
III. Java Programming Environment
Objectives
In this chapter, we will be discussing on how to write, compile and run Java programs. There are
two ways to do this: using an Integrated Development Environment (IDE) or using a console and
a text editor.
At the end of this lesson, the student should be able to:
Create a Java program using a text editor and console in a Windows environment.
Differentiate between syntax errors and runtime errors.
Introduction
This instruction material uses Windows XP as the operating system. Make sure that
before you proceed, you have installed Java in your system.
My First Java Program
public class MyFirstJavaProgram
{
public static void main(String[] args)
{
System.out.println(“Welcome to ICT!”);
}
}
Using a Text Editor and Console
We will now use Notepad as our text editor in windows to save the code in the First Java
Program. We will also need to open the command prompt to compile and run the program.
Step 1: Start the Text Editor
To start Notepad in Windows:
1. Click on Start
2. Accessories
3. Notepad
Figure 3.1: Notepad window
Step 2: Open Terminal
To open Command Prompt in Windows:
1. Click on Start
2. Accessories
3. Command Prompt
Figure 3.2: Command Prompt window
Step 3: Write the source code
Type the code found in My First Java Program in the Notepad.
Training-workshop on Object-oriented Programming using Java
15 | P a g e
Step 4: Save your Java Program
Filename: MyFirstJavaProgram.java
Folder name: MYJAVAPROGRAMS
To open the Save dialog box:
1. Click File (found on the menu bar)
2. Click on Save
*Note: If the folder MYJAVAPROGRAMS does not exist yet, you will have to create the
folder.
Figure 3.3: MyFirstJavaProgram code written in Notepad
Step 5: Compiling your program
Go to the Command Prompt window
Go to the folder MYJAVAPROGRAMS where you saved the program
Specify the Path for the Java Development Kit
Type PATH C:\Program Files\Java\jdk1.6.0_12\bin
Note ‐ The path may vary for the specified folder in the installation of Java in your
machine.
To compile a Java program, we type in the command: javac [filename]
So in this case, type in: javac MyFirstJavaProgram.java During compilation, javac
adds a file to the disk called [filename].class, or in this case, MyFirstJavaProgram.class,
which is the actual bytecode.
Figure 3.4: Compile MyFirstJavaProgram in Command Prompt
Step 6: Running the Program
To run your Java program:
Type java [filename without the extension]
In the case of our example, type in: java MyFirstJavaProgram
You can see on the screen after running the program: "Welcome to ICT!"
Figure 3.5: Run MyFirstJavaProgram in Command Prompt
Java Errors
In our MyFirstJavaProgram example, we didn’t encounter any problems in compiling
and running. However, in programming this is not always the case. In Java, there are two types
of errors: Syntax Errors and Run‐time Errors.
Training-workshop on Object-oriented Programming using Java
17 | P a g e
Syntax Errors
Syntax Errors occur after compilation of a Java program. They are usually typing errors
of the syntax of the Java source code. Java attempts to isolate the error by displaying the line of
code and pointing to the first incorrect character in that line. However, the problem may not be
at the exact point.
Common syntax errors in Java are misspelled Java commands, or forgotten semicolon at
the end of a statement. Other common mistakes are capitalization, spelling, use of incorrect
special characters, and omission of correct punctuation.
Example: In our MyFirstJavaProgram.java code, we intentionally omit one semicolon at the end
of one statement and type a command incorrectly.
Figure 3.6: MyFirstJavaProgram code typed incorrectly
The error messages are then generated after compiling the program.
Figure 3.7: Run MyFirstJavaProgram with errors
The first and second error message suggests that there is an error on the declaration of
an identifier of the main method. The third error message suggests that there is a missing
semicolon by the end of a Java statement.
As a rule of thumb, if you encounter a lot of error messages, try to correct the first
mistake in a long list, and try to compile the program again. Doing so may reduce the total
number of errors dramatically.
In our example, correcting the spelling of static deletes the second and third errors.
Run‐time Errors
Run‐time errors are errors that will not display until you run or execute your program.
Even programs that compile successfully may display wrong answers if the programmer has not
thought through the logical processes and structures of the program.
Increments op by 1; Evaluates to
++ ++op the value of op after it was
incremented
Decrements op by 1; Evaluates
‐‐ op‐‐ to the value of op before it was
decremented
Decrements op by 1; Evaluates
‐‐ ‐‐op to the value of op after it was
decremented
Table 4.3: Increment and Decrement operators
The increment and decrement operators can be placed before or after an operand.
Example:
public class ArithmeticOperatorsSample
{
public static void main(String[] args)
{
int a = 38;
int b = 47;
int c = 50;
int sum;
System.out.println(“Incremented a: ” + a++);
sum = ++b + c;
System.out.println(“Sum of ++b + c = ” + sum);
}
}