Laboratory Activity #2
Running a Java program
Objectives
• To create a simple Java stand-alone program and run it using command line interface
Running a Java program
Java programs
● Java applets: programs designed to run on a web browser
● Java application: standalone programs
● Pre-requisites in writing a program:
o Syntax rules: rules which tell you which statements are legal or accepted by the
programming language and which are not
o Semantic rules: rules which determine the meaning of the instruction
Steps in creating and running a Java program
● Create a source file
o A source file contains code, written in the Java programming language, that you and
other programmers can understand. You can use any text editor to create and edit
source files.
● Compile the source file into a .class file
o The Java programming language compiler (javac) takes your source file and
translates its text into instructions that the Java virtual machine can understand. The
instructions contained within this file are known as bytecodes.
● Run the program
o The Java application launcher tool (java) uses the Java virtual machine to run your
application.
Figure 2.1 steps in creating and running a Java program
The table below shows the different steps in Java programming and its corresponding tools to
use and the output.
Table 2.2 phases of running a Java program
Example:
You are going to create a Java program following the PDLC discussed in the lecture.
Problem requirements
• Create a program that will display hello world on the screen
Program analysis
• Input: none
• Output: hello world
Program design
• Display “hello world”
Program coding (creating)
public class HELLOWORLD {
/**
* My first Java program
*/
public static void main( String[] args )
{
// print the string "hello world" on screen
System.out.println("Hello world!");
}
}
Saving the Java source code
● Save the source code with .java extension file. In this case save the above source
code to HELLOWORLD.Java. Take note that the filename should be the same as the
class name.
Compiling
• javac HELLOWORLD.java
• Result:
– HELLOWORLD.class
IMPORTANT: javac command is in lower case
Interpreting and loading
• Type: java HELLOWORLD
• Result:
Program debugging and testing
• Test your program several times
• If errors occurs you do debugging
• Types of errors
• Syntax errors
• Compile-time errors
• Runtime errors
• Exceptions
• Logic errors
• Semantic errors
Example of an error:
Syntax Error: }
Solution: Change } to {
Program maintenance
• Update your program by removing the errors and if there are changes in the
requirements.
To summarize the above example, figure 2.3 shows how the hello.Java is created, compiled and
run.
HELLOWORLD.Java HELLOWORLD.class
Figure 2.3 Executing a Java program
Dissecting the HELLOWORLD program
/**
*The HELLOWORLD class implements an
*application that simply prints “Hello world!” To standard output.
*Author: Frevy Orencia
*Date: August 05, 2021 */
public class HELLOWORLD
{
public static void main(String[] args)
{
System.out.println("Hello world!"); //display the string
}
}
________________
Program comment
● These are notes written to a code for documentation purposes.
● Those texts are not part of the program and does not affect the flow of the program
● Something used to document a part of a code, thus, ignored by the compiler but are useful to other
programmers
● It is not part of the program itself, but used for documentation purposes.
● It is good programming practice to add comments to your code.
Types of a Java program comment:
● /*your text here*/ C style comment
o Used for single line and multi-line comments
● /**program documentation*/ special javadoc comments
o Used for single line and multi-line comments
● //your text here C++ style comment
o Used for single line comments
/**
*The HELLOWORLD class implements an application that simply prints
* “Hello world!” To standard output.
*Author: Frevy Orencia
*Date: August 05,2021 */
public class HELLOWORLD
{
public static void main(String[] args)
{
System.out.println("Hello world!"); // display the string.
}
Class definition
● Indicates the name of the class which is HELLOWORLD
● In Java, all code should be placed inside a class declaration
● The class uses an access specifier public, which indicates that our class in accessible to other
classes from other packages (packages are a collection of classes)
● The next line which contains a curly braces { (indicates the start of a block-the class block) and
} (indicates end of the block)
● Note: every application must start with class definition
/**
*The HELLOWORLD class implements an application that simply prints
* “Hello world!” To standard output.
*Author: Frevy Orencia
*Date: August 05,2021 */
public class HELLOWORLD
{
public static void main(String[] args)
{
System.out.println("Hello world!"); // display the string.
}
}
________________
Main method
● The entry point for your Java application and will subsequently invoke all the other methods required
by your program
● Every application must contain a main method whose signature is: public static void
main(String[] args)
/**
*The HELLOWORLD class implements an application that simply prints
* “Hello world!” To standard output.
*Author: Frevy Orencia
*Date: August 05,2021 */
public class HELLOWORLD{
public static void main(String[] args)
{
System.out.println("Hello world!"); // display the string.
}
}
● The command System.out.println(), prints the text enclosed by double quotation on the screen.
● All statement in Java must be terminated with ;
Coding guidelines
1. Your Java source programs should always end with the .java extension.
2. Filenames should match the name of your public class. So for example, if the name of your public
class is HELLOWORLD, you should save it in a file called HELLOWORLD.java.
3. You should write comments in your code explaining what a certain class does, or what a certain
method does.