You are on page 1of 4

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.

Figure 1: Java environment (Farrell, 2014)


Two (2) types of Java program:
 Application – this is a stand-alone program. It can either be a:
o Console application – supports character output to a computer screen in a DOS window.
o Windowed application – creates a GUI with elements such as menus, buttons, dialog boxes, etc.
 Applet – it is a program embedded in a Web page. It runs in a
browser. Example of a simple Java program

public class FirstJavaProgram {


public static void main(String[] args) {
System.out.println(“First Java Application”);
}
}

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.
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.

Figure 2: Anatomy of a Java statement (Farrell, 2014)


A literal string is a series of characters that appear as exactly as entered. Any literal string in Java appears between double
quotation marks. Arguments are information passed to a method so it can perform its task.

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 main() Method


The main method is a special method in every Java application. When executing a Java program, execution always begins
with the method main. All Java applications must include a class containing one (1) main method.
Defining the main method in any class:
public static void main(String[] args) { }

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:

Figure 3: FirstJavaProgram class console output

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.
Executing and Debugging Logical Errors
If the Java compiler successfully compiled a Java program and no syntax errors are encountered, the program is ready to
execute. But if the program produces incorrect results, this type of error is called a logical error. For example, a program
that adds two (2) numbers, but the output was multiplied. Logical errors are errors that are difficult to find and resolve
because the interpreter (Java) does not produce an error message. This type of error must locate and correct by the
Comments
programmer.
Comments are used to explain the details in a program. These are notes that a programmer writes to a program to help
another person to understand the program. Comments are ignored by the compiler, meaning they are not executed when
the Java programs run.
Java has two (2) common types of comments:
 Single-line comments – these are comments that begin with // and can be placed anywhere in the line.
o Example: System.out.println(“Welcome to Java.”); //prints Welcome to Java.
 Multiple-line comments – these are enclosed between /* and */.
o Example:
/*
Program that converts Kilogram to Gram
*/
public class KiloToGram

The import Statement


Packages – these are collections of related classes that have been grouped together into a folder. The name of the folder is
the name of the package and begins with lowercase letters. The classes in the package are each placed in a separate file,
and the file name begins with the name of the class.
import – this is a reserved keyword in Java used to access the classes in a package. There are two (2) common ways on
how to use the import statement:
 Importing a package member: To import a specific class into the current file, follow the following syntax:

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, 7 th edition. California: Pearson Education,
Inc.

You might also like