Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 13

Java Programming Environment

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.
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) Figure 3.3: MyFirstJavaProgram
code written in Notepad
2. Click on Save
*Note: If the folder MYJAVAPROGRAMS
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.
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.

++ ++op Increments op by 1; Evaluates to the value of op after it


was incremented
‐‐ op‐‐ Decrements op by 1; Evaluates to the value of op before it
was decremented
‐‐ ‐‐op Decrements op by 1; Evaluates 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(“Variable values: ”);
System.out.println(“a = ” + a);
System.out.println(“b = ” + b);
System.out.println(“c = ” + c);
System.out.println(“Incremented a: ” + a++);
sum = ++b + c;
System.out.println(“Sum of ++b + c = ” + sum);
}
}

You might also like