Java Programming
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
Sun Microsystems Inc. James Gosling
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
Byte Code and Java Virtual Machine
Existing Development Environment
Binary File Compiler(Pentium)
Pentium
Binary File
Source Code
Compiler(PowerPC)
PowerPC
Binary File
%cc Hello.c o Hello % Hello
Run
Compiler(SPARC)
Binary Code
4
SPARC
Byte Code and Java Virtual Machine
Java Development Environment
Java Interpreter
Java Compiler
(Pentium) Java Code
Java Compiler Java ByteCode
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
Java Application Program Application Program written in general programming language
Applet Program running in Web Browser Environment Can be viewed by appletviewer or Web browser with JVM
Class and Object
Object Memory Space to Define State and Operation Instance of Class Class Template of Creating Object
3 Principles of Object Oriented Programming
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 Class Library
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
Java Program Structure
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
Java Program Structure
// comments about the class
public class MyProgram { class header
class body
Comments can be placed almost anywhere
12
Java Program Structure
// comments about the class
public class MyProgram { // comments about the method
public static void main (String[] args)
{ method body
}
method header
13
Start Java Application
Java
Class Name
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 and Assign
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
Variable and Assign
Variable Declaration type varName; Value assign varName = value; int num = 100; long m = 21234234L double type : .5 0.8 9e-2 -9.3e-5 float type : 1.5e-3f; float x, y, x;
16
String and Character
String : sequence of character String s = Enter an integer value: ; Char c = A; Unicode Concatenation Operator +
String s = Lincoln said: + \ Four score and seven years ago\ ;
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 */
Using javadoc Tool, make document
19
Arrays ( One Dimensional)
Definition of One Dimensional Array
type VarName[]
int ia[];
Assign Range to One Dimensional Array
varName = new type[size]
ia = new int[10];
Declaration of One Dimensional Array with Range
type varName[] = new type[size]
int ia[] = new int[10];
20
Arrays ( One Dimensional)
Number of elements in Array
varName.length
Initialization of One Dimensional Array
Type varName[] = {e0, , en};
int j[] = {0, 1, 2, 3, 4, 5};
int k[];
K = j;
Example 1.15
21
Arrays ( Multi-Dimensional)
Definition of Two Dimensional Array
type VarName[][];
float fa[][];
Assign Range to Two Dimensional Array
varName = new type[size1][size2];
fa = new float[2][3];
Declaration of Two Dimensional Array with Range
type varName[][] = new type[size1][size2];
float fa[] = new float[2][3];
22
Arrays ( Two Dimensional)
Number of elements in Multi-Dimensional Array
varName.length
Number of Raw
Number of elements in Each elements of Multi-Dimensional Array
varName[index].length
Initialization of Multi-Dimensional Array
Type varName[][] = {{e00, , e0n}, {e10,,e1y}, {e20, , e2z}};
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