lec2
lec2
Computer
Programming l2
Dr: Marwa AL Enany
A Simple Java Program
2
Listing 1.1
Run
A Simple Java Program
7
𝟏𝟎.𝟓+𝟐𝟑
Listing 1.3: Example of evaluating
𝟒𝟓−𝟑.𝟓
Run
Creating, Compiling, and Running Java
Programs
8
Compiling Java Source Code
9
You can port a source program to any machine with appropriate
compilers.
The source program must be recompiled, however, because the object
program can only run on a specific machine.
Java was designed to run object programs on any platform.
With Java, you write the program once, and compile the source
program into a special type of object code, known as bytecode.
The bytecode can then run on any computer with a Java Virtual
Machine.
Java Virtual Machine is a software that interprets Java bytecode.
Anatomy of a Java Program
10
Class name
Main method
Statements
Statement terminator
Reserved words
Comments
Blocks
Class Name 11
Every Java program must have at least one class. Each
class has a name.
By convention, class names start with an uppercase
letter. In this example, the class name is Welcome.
) …(
;
// This program prints Welcome to Java! 19
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
// …
"…" 20
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Programming Style and Documentation
21
Appropriate Comments
Naming Conventions
Proper Indentation and Spacing Lines
Block Styles
Appropriate Comments 22
Indentation
– Indent two spaces.
Spacing
– Use blank line to separate segments of the code.
Block Styles
25
Guidelines:
➢ A single error will often display many lines of compile errors
➢ Fix errors from the top line and work downward.
➢ Fixing errors that occur earlier in the program may also fix
additional errors that occur later.
Runtime Errors
28
Runtime errors are errors that cause a program to terminate
abnormally.