02 - Elements of Java - Handout 1
02 - Elements of Java - Handout 1
Java Environment
Java – a high-level programming language that was developed by Sun Microsystems for general-purpose business
applications and interactive, web-based Internet applications. Java can be used to write programs that run on any operating
system or device.
Java Environment Machine (JVM) – an environment that translates Java bytecode (compiled format for Java programs) into
machine language and executes it.
Source code – programming statements written in a high-level language. This is created using a text editor or a development
environment.
Development Environment – a set of tools that help write programs easily, such as NetBeans. The statements are saved in
a file. Then, the Java compiler converts the source code into a binary program of bytecode. The Java interpreter then checks
the bytecode and communicates with the operating system, executing the bytecode instructions line by line within the Java
Virtual Machine.
Figure 1 describes the Java environment.
The FirstJavaProgram is a class name. A class is the basic unit of a Java program. All Java codes are grouped into a class.
The definition of the class starts with an access modifier (such as public), which specifies the accessibility of classes,
followed by the keyword class. Every class definition is enclosed within pair of curly brackets or braces ( { and } ). These
braces mark the beginning and the end of the class.
The main method is a special method and is the entry point of the program execution. A Java class can only have one (1)
main method.
02 Handout 1 *Property of STI
Page 1 of 4
IT1708
In the example Java program, it contains only one (1) programming statement or command:
System.out.println(“First Java Application”);. A statement is an action that the program has to perform.
Identifiers
An identifier is a name of a program component, such as class, object, or variable.
These are the requirements in naming identifiers:
• It must begin with a letter of the English alphabet, an underscore, or a dollar sign. An identifier cannot begin with a
digit.
• Identifiers name can only have any combination of letters, digits, underscores, or dollar sign. White spaces are not
allowed.
• It cannot be a reserved keyword.
• Identifiers are case sensitive. For example, the identifier NUMBER is not the same as the identifier number or Number.
• It cannot be one of the following values: true, false, or null. These are not keywords, they are primitive values, but
they are reserved and cannot be used.
When naming class identifiers, it is a good practice to begin the name with a capital letter.
Reserved Words
Reserved words or keywords have special predefined meaning and cannot be used in naming variables, classes, methods,
or identifiers. The Java language designates certain keywords that the compiler recognizes as reserved words. The following
table lists all the reserved words in Java.
Table 1: Java reserved keywords (Farrell, 2014)
abstract continue for new switch
assert default goto package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
instance
case enum return transient
of
catch extends int short try
char final interface static void
class finally long strictfp volatile
const float native super while
The basic parts of the main method are the heading and the body:
• The public static void main(String[] args) is the heading.
• The statements enclosed between the braces form the body of the main method. It contains the declaration and
execution of the program.
Here are descriptions of the meaning and purpose of the terms used in the method heading:
• The public keyword is an access modifier. The method heading should be public for the Java interpreter to call it
and to run the class.
• The static keyword means that a method is accessible and usable.
• The void keyword indicates that the main method does not return any value when it is called.
• The name of the method is the main. When executing a Java application, the JVM always executes the main method
first.
• The contents between the parentheses of the main method, which is String[] args, represent the type of
argument that can be passed to the main method.
Saving, Compiling, and Debugging Syntax Errors
Java Development Kit (JDK) – includes a complete set of JRE tools for developing, debugging, and monitoring Java
applications. JDK is needed to install to the computer to write, compile, and execute Java programs.
Java Runtime Environment (JRE) – covers most end-users needs. It contains everything required to run Java applications on
a system.
The following are the steps on how to create, compile, and execute a Java application using a DOS window:
Step 1. Write a Java program in a text editor.
Step 2. Save the program as the same with the class name of the program with extension .java.
Step 3. Open the DOS window (cmd) then change the directory to the directory where the Java file was saved.
Step 4. Set the path of JDK using the path command. This setting tells the operating system where to find the Java
compiler and the classes. Setting the path of JDK is defined as set path=%path%;[JDK bin directory]
Step 5. Compile the Java program using javac command: javac [name of the Java file with extension
.java]
Step 6. Execute the compiled Java program using the java command (interpreter): Java [name of the Java
file]
The following figure shows how to set JDK path, compile and execute a Java program in the DOS window:
Syntax errors are invalid statements that the Java compiler (javac) might encounter. In the process of compiling, when the
compiler encounters a syntax error, it produces one (1) or more error messages. To debug this error, the programmer must
locate and correct the grammatical mistake in the program. For example, the compiler produces an error message of a
missing semicolon in a statement. To correct this, the programmer must add the semicolon to the statement.
import package_name.Class_name;
Example:
import java.util.Date;
• Importing an entire package: To import all the classes contained in a particular package, use the import statement
with the asterisk (*) wildcard character:
import package_name.*;
Example:
import java.util.*;
When importing packages, all the import statements must appear before the class declaration.
REFERENCES:
Baesens, B., Backiel, A., & Broucke, S. (2015). Beginning java programming: The object-oriented approach. Indiana: John
Wiley & Sons, Inc.
Farrell, J. (2014). Java programming, 7th edition. Boston: Course Technology, Cengage Learning.
Savitch, W. (2014). Java: An introduction to problem solving and programming, 7th edition. California: Pearson Education,
Inc.