Introduction to Java
Object-Oriented Programming
Outline
■ Brief introduction of the language and platform
■ Sample programs
■ Code structure
■ How to compile and run a program
Readings:
■ IPIJ: Ch.1.
Introduction to Java 2
Why Java?
■ Widely used.
■ Widely available.
■ Continuously under development since early 1990s.
■ Embraces full set of modern abstractions.
■ Variety of automatic checks for mistakes in
programs.
It’s not about the language!
Introduction to Java 3
Compiling and interpreting
■ Program source code compiled into bytecode
■ Bytecode is platform-independent
■ Bytecode is executed in an interpreter environment
(virtual machine)
Introduction to Java 4
HelloWorld application
same name with the class
HelloWorld.java:
class name
this is a class start of the class
public class HelloWorld {
main() function
public static void main (String[] args) {
System.out.println("Hello, world"); a statement
it says print to
standard output
}
}
end of the class
Introduction to Java 5
Compile and run public class HelloWorld {
public static void main (String[]
args) {
System.out.println("Hello, world");
}
}
■ Compile HelloWorld.java
javac HelloWorld.java compiler
■ Run
java HelloWorld HelloWorld.class
%> javac HelloWorld.java
%> java HelloWorld
Hello, world
Lưu ý %> là dấu nhắc, không phải lệnh
Introduction to Java 6
Application with more than one class
Two classes in separated files
public class TestGreeting {
TestGreeting.java: public static void main(String[] args) {
Greeting gr = new Greeting();
gr.greet();
}
}
public class Greeting {
Greeting.java:
public void greet() {
System.out.print("Hi there!");
}
}
Introduction to Java 7
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 8
Applications that use a library
from a file
named Java standard
stdlib.jar library
9
Java Standard library
No need for a .jar file or a special configuration
10
Compile and run
■ Put the .jar file in the same
folder with the code
■ Linux/Mac
%> javac -classpath .:stdlib.jar Heart.java
%> java -classpath .:stdlib.jar Heart
■ Windows
%> javac -classpath .;stdlib.jar Heart.java
%> java -classpath .;stdlib.jar Heart
Introduction to Java 11
Integrated Development Environment
12
Input & Output
command-line
arguments
13
Code structure
Introduction to Java 14
What can we do in a method…
■ Looping… while (x > 12) {
x = x – 1;
}
for (int x = 0; x < 10; x++) {
System.out.print(x);
}
■ Conditional branching…
if (x == 2) {
System.out.println("x must be 2");
} else {
System.out.println("x is not 2");
}
Introduction to Java 15
Example: use of for loop
16
Type conversions
Automatic
■ Convert number to string for "+".
■ Make numeric types match if no loss of
precision.
Explicitly defined for function call.
Cast for values that belong to multiple
types.
■ Ex: small integers can be short, int or
long.
■ Ex: double values can be truncated to
int values.
17
What else can we do?
■ do-while?
■ switch?
■ int, long, float, double, boolean…?
■ other syntatical stuff?
Read your text books!
Introduction to Java 18