Lectre 1 - Java Intro
Lectre 1 - Java Intro
Lectre 1 - Java Intro
Java Basics
Contents
Background Java Virtual Machine Application & Applet Class & Object 3 Principles of Object Oriented Programming Start up Java Variables & Assign String & Character Arithmetic Operator & Expression Comment Array Keywords
Background / History
1990, Green Project 1991, Develop the Language for Household Products => To General Developing Language
Java James Gosling, Arthur Van Hoff, Andy Bechtolsheim Coffee Materials in Indonesia Write once, Run everywhere Spread Rapidly through WWW and Internet
Pentium
Binary File
Source Code
Compiler(PowerPC)
PowerPC
Binary File
Compiler(SPARC)
Binary Code
4
SPARC
Java Interpreter
Java Compiler
Pentium
Java Interpreter
(Independent on Platform)
(PowerPC)
%javac Hello.java
Hello.class created
PowerPC
Java Interpreter
% java Hello
Run JVM Byte Code
Java Compiler
SPARC
(SPARC)
5
Java Program
Applet Program running in Web Browser Environment Can be viewed by appletviewer or Web browser with JVM
Object Memory Space to Define State and Operation Instance of Class Class Template of Creating Object
Encapsulation Control Access the data and method in program code Inheritance Inherit the data and methods of parent class Polymorphism
Shape
getArea() Shape Obj = new Ellipse(); Obj.getArea();
Ellipse
Rectangle
Triangle
Which Area?
getArea()
getArea()
getArea()
java.applet : Applet related java.awt : Abstract Window Toolkit java.awt.event : Event process from awt component java.awt.image : Image processing java.beans : JavaBeans Component java.io : File or Network I/O Support java.lang : Java Language Support java.net : Network related functions java.util : Utility function
Java 2 SDK
Java 2 Software Development Kit (Java 2 SDK) Java Application or Applet Development and Running Environment Java 2 Runtime Environment (JRE) Java Running Environment Java 2 Standard Edition Java 2 Micro Edition Java 2 Enterprise Edition
10
In the Java programming language: A program is made up of one or more classes A class contains one or more methods A method contains program statements These terms will be explored in detail throughout the course A Java application always contains a method called main
11
class body
12
{ method body
}
method header
13
C
hello.c void main() { printf(Hello World\n); }
Main Function
Hello.java Main method class Hello { public static void main(String args[]) { System.out.println(Hello World); }
% javac Hello.java
Compile
% cc hello.c o hello
% java Hello
Run
% hello
Result
Hello World
14
Variable Type char boolean byte short int long float double
16bits Unicode character data Boolean Variable 8 bits signed integer 16 bits signed integer 32 bits signed integer 64 bits signed integer 32 bits signed floating point number 64 bits signed floating point number
15
16
String : sequence of character String s = Enter an integer value: ; Char c = A; Unicode Concatenation Operator +
Result :
Lincoln said: Four score and seven years ago
17
Arithmetic Operator
Operators + - * / % += -= *= /= %= ++ -5/2 2 Why isnt it 2.5 ? 5%2 1 4/2 2 4%2 0 count * num + 88 / val 19 % count j += 6; j = j + 6;
18
Comment
Single Line Comment int i = 10 ; // i is counter Multiple Line Comment /* Some comments */ Documentation Comment /** Documentation Comment */
19
20
int k[];
K = j;
Example 1.15
21
Arrays ( Multi-Dimensional)
Definition of Two Dimensional Array
type VarName[][];
float fa[][];
22
Example 1.16
23
Java Keywords
47 Java Keywords abstract boolean break byte case catch char class const* continue default do
double else extends final finally float for goto* if implements import instanceof
int interface long native new package private protected public return short static
super switch synchronized this throw throws transient* try void volatile while
24