Multimedia Programing
Introduction
A Simple Java Program
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Trace a Program Execution
Enter main method
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Trace a Program Execution
Execute statement
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Trace a Program Execution
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
print a message to the
console
Anatomy of a Java Program
• Class name
• Main method
• Statements
• Statement terminator
• Reserved words
• Comments
• Blocks
Class Name
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!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Main Method
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.
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Statement
A statement represents an action or a sequence of
actions. The statement System.out.println("Welcome
to Java!") in the program is a statement to display
the greeting "Welcome to Java!“.
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Statement Terminator
Every statement in Java ends with a semicolon (;).
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Reserved words
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!");
}
}
Blocks
A pair of braces in a program forms a block that groups
components of a program.
public class Test {
Class block
public static void main(String[] args) {
System.out.println("Welcome to Java!"); Method block
}
}
Special Symbols
Character Name Description
{} Opening and closing Denotes a block to enclose statements.
braces
() Opening and closing Used with methods.
parentheses
[] Opening and closing Denotes an array.
brackets
// Double slashes Precedes a comment line.
"" Opening and closing Enclosing a string (i.e., sequence of characters).
quotation marks
; Semicolon Marks the end of a statement.
{ …}
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
( … )
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
;
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
// …
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
"…"
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}