Professional Documents
Culture Documents
01 Java Intro PDF
01 Java Intro PDF
Object-Oriented Programming
Outline
Readings:
HFJ: Ch.1, Ch.5.
GT: Ch. 2.
Introduction to Java 2
Java
very slow
Introduction to Java 3
Why Java?
Object-oriented
Portability
Safe
Data always initialized, references always type-
safe
Access to "private" or "package private" data and
methods is rigidly controlled.
Addresses the weaknesses of older programming
languages
Introduction to Java 4
Compiling and interpreting
Java
Java Java Virtual Machine
Compiler bytecode
program for Linux
program
Introduction to Java 5
Types of Java applications
Web browsers
Server applications – Java Enterprise Edition (J2EE)
Webservices, JSP and Servlet
Introduction to Java 6
Java Desktop Applications
Introduction to Java 7
HelloWorld application
same name with the class
HelloWorld.java:
this is a class class name start of the class
public class HelloWorld {
method name
public static void main (String[] args) {
Introduction to Java 8
Compile and run
public class HelloWorld {
public static void main (String[] args)
{
System.out.println("Hello, world");
}
}
Introduction to Java 10
Compile and run
Compile
javac TestGreeting.java
Greeting.java is automatically compiled
Run
java TestGreeting
%> javac TestGreeting.java
%> java TestGreeting
Hi there!
Introduction to Java 11
JDK – Java Development Kit
Introduction to Java 12
Code structure
source file Car.java
each file holds one class
class Car
public class Car { a class has one or more methods
void break() {
statement_1; statements
statement_2; statements are inside methods
}
two methods of class Car
void turn_right() { methods belong to a class
}
}
Introduction to Java 13
Writing a class with a main
main() method
the class MUST have a main() method!
Introduction to Java 14
What can we do in a method…
Conditional branching…
if (x == 2) {
System.out.println("x must be 2");
} else {
System.out.println("x is not 2");
}
Introduction to Java 15
What else can we do?
do-while?
switch?
int, long, float, double, boolean…?
other syntactical stuff?
Introduction to Java 16