Java Program Structure and
Programming Errors
Objectives
• Creating a java application
• Compiling java application
• Running java application
• Programming errors
• Syntax
• Logic
• Runtime
• Common errors
• Programming Style and Documentation
Announcements
• Google Classroom
u43sdci
• Email : [email protected]
Creating a java application
• Use text editor to write your programs
• Save the files with the extension .java
• The file name should be the same as the class
name
Note: Java is Case sensitive
• Use command line to run your apps
Sample Java Program
1. public class Welcome {
2 public static void main(String[] args) {
3 // Display message Welcome to Java! on the console
4 System.out.println("Welcome to Java!");
5 }
6}
Special Characters
Compiling java application
C:\>javac Welcome.java
● The javac compiler creates a file called Welcome.class
● Contains the bytecode version of the program - instructions the Java
Virtual Machine will execute -
Running java application
● Use the Java application launcher called java
C:\>java Welcome
● When the program is run, the following output is displayed:
“Welcome to Java!”
● When Java source code is compiled, each individual class is put
into its own output file named after the class and using the .class
extension
Phases of a Java Program
Programming errors
• Programming errors can be categorized into three types:
• syntax errors
• Runtime errors
• logic errors.
Syntax Errors
• Errors that are detected by the compiler
• Syntax errors result from errors in code construction, such as
mistyping a keyword, omitting some necessary punctuation, or
using an opening brace without a corresponding closing brace
• These errors are usually easy to detect because the compiler tells
you where they are and what caused them
• Fix errors from the top line and work downward
Logic Errors
• Occur when a program does not perform the way it was intended
to.
Runtime Errors
• 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.
• Input mistakes typically cause runtime errors
• Division by zero may cause Runtime errors
Common errors
• Missing Braces
• Missing Semicolons
• Missing Semicolons
• Misspelling Names
Programming Style and Documentation
• Appropriate Comments and Comment Styles
• Proper Indentation and Spacing
• Block Styles
• next-line
• end-of-line
Resources
•Liang – Chapter 1