Creating, Compiling, and Executing a Java
Program
Lecture 4
Department of Computer Science and Engineering, ITER
Siksha ’O’ Anusandhan (Deemed to be University), Bhubaneswar, Odisha, India.
1 / 13
Contents
1 Creating, Compiling, and Executing a Java Program
2 Structure of a Java program
3 Programming Style and Documentation
4 Programming Errors
2 / 13
Creating, Compiling, and Executing a Java Program
You can use any text editor or IDE to create and edit a Java
source-code file.
A Java compiler translates a Java source file into a Java
bytecode file. The following command compiles Welcome.java:
javac Welcome.java
Once the bytecode file is generated, then this bytecode file can be
executed using the following command:
java Welcome
The bytecode is similar to machine instructions but is architecture
neutral and can run on any platform that has a JVM.
3 / 13
Java Program Development Process
Figure 1: Java Program Development Process
4 / 13
The Java Program Execution Process
Figure 2: The Java Program Execution Process
5 / 13
Structure of a Java program
Figure 3: Basic Structure of a Java program
6 / 13
Programming Style and Documentation
Programming style deals with what programs look like.
Documentation is the body of explanatory remarks and comments
pertaining to a program.
Good programming style and appropriate documentation reduce
the chance of errors and make programs easy to read.
7 / 13
Appropriate Comments and Comment Styles
It is good practice to include a summary at the beginning of the
program.
You should also include comments that introduce each major
step.
// Line comment
/* */ Block Comment
/** */ Javadoc Comment
8 / 13
Proper Indentation and Spacing
A consistent indentation style makes programs clear and easy to
read, debug, and maintain.
Indentation is used to illustrate the structural relationships
between a program’s components or statements.
Indent each subcomponent or statement at least two spaces
more than the construct within which it is nested.
A single space should be added on both sides of a binary
operator, as shown in the following statement:
System . o u t . p r i n t l n ( 3 + 4 * 4 ) ; Bad s t y l e
System . o u t . p r i n t l n ( 3 + 4 * 4 ) ; Good s t y l e
9 / 13
Block Styles
p u b l i c c l a s s Test
{
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] args )
{
System . o u t . p r i n t l n ( ” Next − l i n e s t y l e ” ) ;
}
}
p u b l i c c l a s s Test {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] args ) {
System . o u t . p r i n t l n ( ” End− of − l i n e s t y l e ” ) ; }
}
10 / 13
Programming Errors
Programming errors can be categorized into three types: syntax
errors, runtime errors, and logic errors.
Syntax Errors : Errors that are detected by the compiler are
called syntax errors or compile errors. Syntax errors result from
errors in code construction.
Runtime Errors : Runtime errors are errors that cause a program
to terminate abnormally. They occur while a program is running; if
the environment detects an operation that is impossible to carry out.
Logic Errors : Logic errors occur when a program does not
perform the way it was intended to.
11 / 13
Example
p u b l i c c l a s s Test {
p u b l i c s t a t i c main ( S t r i n g [ ] args ) {
System . o u t . p r i n t l n ( ” Block S t y l e s ) ;
System . o u t . p r i n t l n ( 5 / 0 ) ;
System . o u t . p r i n t l n ( ( 9 / 5 ) * 35 + 3 2 ) ;
}
}
void is missing in line 2 and closing quotation mark is missing in
line 3(Syntax error).
division by zero is not possible in line 4(Runtime error).
the output of line 5 will be 67 instead of 95(Logical error).
12 / 13
References
Y Daniel Liang ‘Introduction to JAVA Programming, Comprehensive Version’,
Pearson, 2014.
13 / 13