Beruflich Dokumente
Kultur Dokumente
(c) venkatesh
Java - An Introduction
(c) venkatesh
http://www.vidyarthiplus.com
Buzzword compliant!
http://www.vidyarthiplus.com
Simple
Pure
Portable
Surprisingly effective
http://www.vidyarthiplus.com
Object Oriented
Portable
High Performance
Geared for Distributed Environments
Secure
http://www.vidyarthiplus.com
http://www.vidyarthiplus.com
Java as Portable
(c) venkatesh
JAVA COMPILER
(translator)
JAVA INTERPRETER
(one for each different system)
Windows 95
http://www.vidyarthiplus.com
Macintosh
Solaris
Windows NT8
(c) venkatesh
Java
Write Once, Run Anywhere
http://www.vidyarthiplus.com
10
http://www.vidyarthiplus.com
11
Class Loader
Lightweight Binary Class Files
Multithreading
Dynamic
Good communication constructs
Secure
http://www.vidyarthiplus.com
12
Java as Secure
(c) venkatesh
http://www.vidyarthiplus.com
13
Feature
Encapsulation
Inheritance
Multiple Inherit.
Polymorphism
Binding (Early/Late)
Concurrency
Garbage Collection
Genericity
Class Libraries
http://www.vidyarthiplus.com
C++
Yes
Yes
Yes
Yes
Both
Poor
No
Yes
Yes
Objective
C
Ada
Java
Yes
Yes
Yes
Yes
Both
Poor
Yes
No
Yes
Yes
No
No
Yes
Early
Difficult
No
Yes
Limited
Yes
Yes
No
Yes
Late
Yes
Yes
No
Yes
14
http://www.vidyarthiplus.com
15
Types
boolean either true of false
char
16 bit Unicode 1.1
byte
8-bit integer (signed)
short
16-bit integer (signed)
int
32-bit integer (signed)
long
64-bit integer (singed)
float
32-bit floating point (IEEE 754-1985)
double 64-bit floating point (IEEE 754-1985)
String
(class for manipulating strings)
Java uses Unicode to represent characters
internally
16
Java Environment/
Life Cycle of Java Code
Runtime
Environment
(c) venkatesh
Compile-time
Environment
Bytecode
Verifier
Java
Source
(.java)
Java
Compiler
Class
Loader
Java
Bytecodes
move locally
or through
network
Java
Interpreter
Just in
Time
Compiler
Java
Class
Libraries
Java
Virtual
machine
Runtime System
Java
Bytecode
(.class )
http://www.vidyarthiplus.com
Operating System
Hardware
18
http://www.vidyarthiplus.com
19
Hello Internet
(c) venkatesh
21
Bytecode Verifier
(c) venkatesh
http://www.vidyarthiplus.com
22
Class Loader
(c) venkatesh
http://www.vidyarthiplus.com
23
Security Manager
(c) venkatesh
http://www.vidyarthiplus.com
24
http://www.vidyarthiplus.com
25
Operator overloading
Pointers and Array/pointers
Multiple-inheritance of implementation
Enum, typedef, #define
Copy constructors, destructors
Templates
And other stuff....
http://www.vidyarthiplus.com
26
http://www.vidyarthiplus.com
27
Core Classes
language
Utilities
Input/Output
Low-Level Networking
Abstract Graphical User Interface
Internet Classes
TCP/IP Networking
WWW and HTML
Distributed Programs
28
Main Packages
(c) venkatesh
java.lang
java.util
java.io
java.awt
java.awt.image
java.applet
java.net
29
(c) venkatesh
Java Fundamentals
Constructs
Graphics
Multithreading
Streams and Networking
Networking
http://www.vidyarthiplus.com
30
What is Java ?
(c) venkatesh
A programming language:
31
Types
(c) venkatesh
String s;
http://www.vidyarthiplus.com
32
declaring a class
class MyClass {
member variables;
member functions () ;
http://www.vidyarthiplus.com
33
Java programs
(c) venkatesh
Two kinds
Applications
have main()
run from the OS prompt
Applets
have init(), start(), stop(), paint(), update(), repaint(),
destroy()
run from within a web page
34
class MyApp {
public static void main(String s [ ] ) {
System.out.println(Hello World);
}
} // end class MyApp
http://www.vidyarthiplus.com
35
declare a reference
String s;
create/define an object
36
declare
int a [ ] ;
int [ ] b ;
int [ ] c [ ];
int c [ ][];
// 1-dim
// 1-dim
// 2-dim
// 2-dim
allocate space
37
// 1-dim
38
this is because
(c) venkatesh
http://www.vidyarthiplus.com
39
Constructors
(c) venkatesh
class Weight {
int lb; int oz;
public Weight (int a, int b ) {
lb = a; oz = b;
}
}
http://www.vidyarthiplus.com
40
this keyword
(c) venkatesh
http://www.vidyarthiplus.com
41
class Weight {
int lb; int oz;
public Weight (int lb, int oz ) {
this.lb = lb; this.oz = oz;
}
}
42
class Weight {
public int m1 (int a) {
int x = this.m2(a); return x;
}
public int m2(int b) { return b*7 ; }
}
http://www.vidyarthiplus.com
43
class
(c) venkatesh
parameter
(c) venkatesh
class MyApp {
int a;
public static void main(String [] s ) { (new MyApp()).myMethod(); }
public void myMethod() { yourMethod(this); }
public void yourMethod(MyApp inMyApp) { inMyApp.a = 77; }
}
45
static keyword
(c) venkatesh
46
class PurchaseOrder {
private static int POCount; // var. a is shared by all objects of this class
public static void main(String [] s ) {
PurchaseOrder po1 = new PurchaseOrder();
po1.updatePOCount();
}
public void updatePOCount() { POCount++; }
}
http://www.vidyarthiplus.com
47
class Math {
public static double sqrt(double x) {
// calculate
return result;
}
}
class MyApp {
public static void main(String [] s ) {
double dd;
dd = Math.sqrt(7.11);
}
}
48
Inheritance (subclassing)
(c) venkatesh
class Employee {
protected String name;
protected double salary;
public void raise(double dd) {
salary += salary * dd/100;
}
public Employee ( ) { }
}
49
public Manager ( ) { }
}
50
Overriding (methods)
(c) venkatesh
}
public Manager ( ) { }
}
http://www.vidyarthiplus.com
51
(c) venkatesh
class First {
public First() { System.out.println( First class ); }
}
public class Second extends First {
public Second() { System.out.println(Second class); }
}
public class Third extends Second {
public Third() {System.out.println(Third class);}
}
First class
Second class
Third class
52
access modifiers
(c) venkatesh
private
public
everywhere
protected
(default)
53
super keyword
(c) venkatesh
http://www.vidyarthiplus.com
54
//overrides raise() of
salary += bonus;
}
public Manager ( ) { }
}
http://www.vidyarthiplus.com
55
super(name, salary);
this.bonus = bonus;
}
}
http://www.vidyarthiplus.com
56
final keyword
(c) venkatesh
means constant
applies to
http://www.vidyarthiplus.com
57
class Math {
public final double pi = 3.1412;
public static double method(double x) {
double x = pi * pi;
}
}
note: variable pi is made read-only
http://www.vidyarthiplus.com
58
class Employee {
protected String name;
protected double salary;
public final void raise(double dd) {
salary += salary * dd/100;
}
public Employee ( ) { }
}
then: cannot ovveride method raise() inside
the Manager class
http://www.vidyarthiplus.com
59
http://www.vidyarthiplus.com
60
abstract classes
interfaces
(concrete classes)
http://www.vidyarthiplus.com
61
http://www.vidyarthiplus.com
62
sample interface
(c) venkatesh
interface ResponceToMouseClick {
public void mouseDown();
public void mouseUp();
public void mouseDoubleClick();
}
63
...
int a = 7, b = 0, result;
if ( b != 0) {
result = a/b;
}
else {
System.out.println(b is zero);
}
...
...
int a = 7, b = 0, result;
try {
result = a/b;
}
catch (ArithmeticException e ) {
System.out.println(b is zero);
}
...
64
Exceptions (contd)
(c) venkatesh
...
int a = 7, b = 0, result;
try {
result = a/b;
/// more code .. reading from a file
}
catch (ArithmeticException e ) {
System.out.println(b is zero);
}
catch (IOException e ) {
System.out.println(Cant read);
}
finally {
Sysytem.out.println(Closing file);
/// code to close file
}
...
http://www.vidyarthiplus.com
65
if (y == 0 ) {
throw new ArithmeticException();
}
else {
return a/b ;
}
} // end divide()
http://www.vidyarthiplus.com
66
67
(c) venkatesh
http://www.vidyarthiplus.com
68
http://www.vidyarthiplus.com
69
http://www.vidyarthiplus.com
70
setLayout(new BorderLayout());
// Add text field to top
add("North",new TextField());
// Create the panel with buttons at the bottom...
Panel p = new Panel();
// FlowLayout
p.add(new Button("OK"));
p.add(new Button("Cancel"));
add("South",p);
http://www.vidyarthiplus.com
71
http://www.vidyarthiplus.com
72
import java.awt.*;
Assemble the GUI
http://www.vidyarthiplus.com
73
Import java.awt.*;
class MyGui {
public static void main(String [] s ) {
Frame f = new Frame (My Frame);
Button b = new Button(OK);
TextField tf = new TextField(George, 20);
f.setLayout(new FlowLayout());
f.add(b);
f.add(tf);
f.setSize(300, 300);
f.setVisible(true);
}
}
74
http://www.vidyarthiplus.com
output
(c) venkatesh
http://www.vidyarthiplus.com
75
Events
(c) venkatesh
b.addActionListener(
Button
);
Frame
f.addWindowListener(
http://www.vidyarthiplus.com
);
76
Events
(c) venkatesh
http://www.vidyarthiplus.com
77
Listener Interfaces
(c) venkatesh
78
Listener Interfaces
(c) venkatesh
79
80
81
(c) venkatesh
}
}
82
Implementing 2 interfaces
(c) venkatesh
83
or use Adapters
(c) venkatesh
}
Need only implement the method(s) that are required,
instead of all seven methods of the WindowListener
interface
84
(c) venkatesh
}
http://www.vidyarthiplus.com
85
class MyApp {
Button b = new Button(OK);
Frame f = new Frame(My Frame);
public static void main(String [] s ) {
((new MyApp()).go(); }
public void go() {
86
(c) venkatesh
http://www.vidyarthiplus.com
87
http://www.vidyarthiplus.com
88
(c) venkatesh
http://www.vidyarthiplus.com
89
(c) venkatesh
Multithreading in Java
(A built-in feature in Java)
http://www.vidyarthiplus.com
90
Single-threaded Process
Multiplethreaded Process
Threads of
Execution
91
Threads
(c) venkatesh
92
Thread states
(c) venkatesh
new
start()
wait()
sleep()
suspend()
blocked
runnable
stop()
dead
http://www.vidyarthiplus.com
non-runnable
notify()
slept
resume()
unblocked
93
Threading Mechanisms...
(c) venkatesh
http://www.vidyarthiplus.com
94
http://www.vidyarthiplus.com
extends Thread
run()
body of execution
new MyThread();
95
An example
(c) venkatesh
96
.....
public void run()
{
// thread body of execution
}
}
Creating Object:
MyThread myObject = new MyThread();
Creating Thread Object:
Thread thr1 = new Thread( myObject );
Start Execution:
thr1.start();
http://www.vidyarthiplus.com
97
An example
(c) venkatesh
98
(c) venkatesh
99
Method 1
Method 2
Key
Block 1
Threads
Monitor (synchronised) solves race-condition problem
100
(c) venkatesh
so
101
102
the driver
(c) venkatesh
class MyMainClass {
public static void main(String [] args ) {
Shared sharedObject = new Shared ();
Thread t1 = new Thread(new MyThread(sharedObject));
Thread t2 = new Thread(new YourThread(sharedObject));
Thread t3 = new Thread(new HerThread(sharedObject));
t1.start();
t2.start();
t3.start();
} // end main()
}
http://www.vidyarthiplus.com
103
Threads in Action...
Cooperative threads - File Copy
(c) venkatesh
reader()
{
- - - - - - - - lock(buff[i]);
read(src,buff[i]);
unlock(buff[i]);
- - - - - - - - }
buff[0]
buff[1]
writer()
{
- - - - - - - - - lock(buff[i]);
write(src,buff[i]);
unlock(buff[i]);
- - - - - - - - - }
104
(c) venkatesh
http://www.vidyarthiplus.com
105
Example:
http://www.vidyarthiplus.com
106
import java.io.*;
public class FileToOut1 {
public static void main(String args[]) {
try {
FileInputStream infile = new FileInputStream("testfile.txt");
byte buffer[] = new byte[50];
int nBytesRead;
do {
nBytesRead = infile.read(buffer);
System.out.write(buffer, 0, nBytesRead);
} while (nBytesRead == buffer.length);
}
catch (FileNotFoundException e) {
System.err.println("File not found");
}
catch (IOException e) { System.err.println("Read failed"); }
}
}
107
Filters
(c) venkatesh
For objects:
ObjectInputStream, ObjectOutputStream
108
(c) venkatesh
import java.io.*;
public class GenerateData {
public static void main(String args[]) {
try {
FileOutputStream fos = new FileOutputStream("stuff.dat");
DataOutputStream dos = new DataOutputStream(fos);
dos.writeInt(2);
dos.writeDouble(2.7182818284590451);
dos.writeDouble(3.1415926535);
dos.close(); fos.close();
}
catch (FileNotFoundException e) {
System.err.println("File not found");
}
catch (IOException e) {
System.err.println("Read or write failed");
}
}
}
109
import java.io.*;
public class ReadData {
public static void main(String args[]) {
try {
FileInputStream fis = new FileInputStream("stuff.dat");
DataInputStream dis = new DataInputStream(fis);
int n = dis.readInt();
System.out.println(n);
for( int i = 0; i < n; i++ ) { System.out.println(dis.readDouble());
}
dis.close(); fis.close();
}
catch (FileNotFoundException e) {
System.err.println("File not found");
}
catch (IOException e) { System.err.println("Read or write failed");
}
}
}
110
Object serialization
(c) venkatesh
http://www.vidyarthiplus.com
111
import java.io.*;
import java.util.*;
public class WriteDate {
public WriteDate () {
Date d = new Date();
try {
FileOutputStream f = new FileOutputStream("date.ser");
ObjectOutputStream s = new ObjectOutputStream (f);
s.writeObject (d);
s.close ();
}
catch (IOException e) { e.printStackTrace(); }
public static void main (String args[]) {
new WriteDate ();
}
}
http://www.vidyarthiplus.com
112
113
Threads in Action...
Multithreaded Server
(c) venkatesh
Server Process
Client Process
Server
Threads
Client Process
User Mode
Kernel Mode
Message Passing
Facility
http://www.vidyarthiplus.com
114