01 JavaIntro
01 JavaIntro
Object-Oriented Programming
Outline
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.
Introduction to Java 3
Compiling and interpreting
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) {
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
Introduction to Java 6
Application with more than one class
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…
■ 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.
17
What else can we do?
■ do-while?
■ switch?
■ int, long, float, double, boolean…?
■ other syntatical stuff?
Introduction to Java 18