Chapter 1 Week 2 BEEC4814
Chapter 1 Week 2 BEEC4814
Chapter 1 Week 2 BEEC4814
Introduction to Java
Java Virtual Machine (JVM)
javac FirstJavaProgram.java
If got error “javac’ is not recognized as an internal or
external command, operable program or batch file“.
2-29
Basic frame of Program
2-30
Basic frame of Program
2-31
//This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Create/Modify Source Code
Result
34
Execute statement
35
//This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
36
• Class name
• Main method
• Statements
• Statement terminator
• Reserved words
• Comments
• Blocks
37
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.
38
Line 2 defines the main method. In order to run a class, the
class must contain a method named main. The program is
executed from the main method.
39
A statement represents an action or a sequence of
actions. The statement System.out.println("Welcome to
Java!") to display the greeting "Welcome to Java!“.
40
Every statement in Java ends with a semicolon (;).
41
Reserved words or keywords are words that have a
specific meaning to the compiler and cannot be used for
other purposes in the program. For example, when the
compiler sees the word class, it understands that the
word after class is the name for the class.
//This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
42
Reserved words
abstract do implements public true
assert double import return try
boolean else instanceof short void
break enum int static volatile
byte extends interface strictfp while
case false long super default
catch final native switch if
char finally new synchronized protected
class float null this transient
const for package throw
continue goto private throws
A pair of braces in a program forms a block that groups
components of a program.
44
Character Name Description
" " Opening and closing Enclosing a string (i.e., sequence of characters).
quotation marks
; Semicolon Marks the end of a statement.
45
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
46
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
47
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
48
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
49
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
50
• Syntax Errors
• Detected by the compiler
• Runtime Errors
• Causes the program to abort
• Logic Errors
• Produces incorrect result
51
public class ShowSyntaxErrors {
public static main(String[] args) {
System.out.println("Welcome to Java);
}
}
52
public class ShowRuntimeErrors {
public static void main(String[] args) {
System.out.println(1 / 0);
}
}
53
public class ShowLogicErrors {
public static void main(String[] args) {
System.out.println("Celsius 35 is Fahrenheit degree ");
System.out.println((9 / 5) * 35 + 32);
}
}
54