itectural analays
Post on 02-Jun-2018
219 Views
Preview:
TRANSCRIPT
-
8/11/2019 itectural analays
1/44
Introduction to Sun Microsystems java
High level OOPL
Platform Independent
Case Sensitive
-
8/11/2019 itectural analays
2/44
2
Can you make coffee with it?
-
8/11/2019 itectural analays
3/44
3
It was meant to!!
A programming language for appliances!
-
8/11/2019 itectural analays
4/44
4
Java Virtual Machine
Must Run on Any Architecture
Program
in JavaJava
Compiler
Java
Bytecode
Java Virtual Machine
WRITE ONCE, RUN ANYWHERE!debug
pretty portable
-
8/11/2019 itectural analays
5/44
5
Doesnt Make Coffee Yet
-
8/11/2019 itectural analays
6/44
6
So Whats Java Good For?
Web applications!
Java Applet
Server
Java Applet
-
8/11/2019 itectural analays
7/44
7
Java on the Web: Java Applets
Clients download applets via Web browser
Browser runs applet in a Java Virtual Machine (JVM)
Interactive web, security, and client consistencySlow to download, inconsistent VMs (besides, flash
won this war)
Applet
ClientServer
-
8/11/2019 itectural analays
8/44
8
Java on the Web: J2EE
Thin clients (minimize download)
Java all server side
THIS IS WHAT YOULL BE DOING!!
ClientServer
JSPs
Servlets
EJB
JDBC
-
8/11/2019 itectural analays
9/44
9
The Java programming environment
Compared to C++:
no header files, macros, pointers and references, unions, operatoroverloading, templates, etc.
Object-orientation: Classes + Inheritance
Distributed: RMI, Servlet, Distributed object programming.
Robust: Strong typing + no pointer + garbage collection
Secure: Type-safety + access control
Architecture neutral: architecture neutral representation
Portable
Interpreted
High performance through Just in time compilation (compilerconverts byte code to native code) + runtime modification of code
Multi-threaded
-
8/11/2019 itectural analays
10/44
10
Java Features
Well defined primitive data types: int, float, double, char, etc.
int 4 bytes [2,147,648, 2,147,483,647] Control statements similar to C++: if-then-else, switch, while, for
Interfaces
Exceptions
Concurrency (Multi-Threading)
Packages
Reflection (makes it possible to inspect classes, interfaces, fields and
methods at runtime, without knowing the names of the classes,
methods etc. at compile time. It is also possible to instantiate new
objects, invoke methods and get/set field values using reflection. )
Applet model
-
8/11/2019 itectural analays
11/44
11
The Java programming environment
Java programming language specification
Syntax of Java programs Defines different constructs
Java byte code: Intermediate representation for Java programs
Java compiler: Program that Transform Java programs into Java
byte code
Java interpreter: Read programs written in Java byte code andexecute them (converts byte into native code. i.e Codes other
than java).
Java virtual machine: The whole technology is based on the
concept of JVM. Translator of byte code into platform specific
machine language. It is the Runtime system that providesvarious services to running programs (Logical representation of
JRE)
-
8/11/2019 itectural analays
12/44
12
JVM
-
8/11/2019 itectural analays
13/44
13
JIT (Just-in-Time) Compilation
-
8/11/2019 itectural analays
14/44
14
JVM
JVM stands for Java Virtual Machine. Itsan abstract computer
or virtual computer which runs the compiled java programs.Actually JVM is a software implementation which stands on the
top of the real hardware platform and operating system. It
provides abstraction between the compiled java program and
the hardware and operating system.
JIT: This Compiler converts byte code to native code.
Old Interpreter was replaced with JIT to increase the
performance of java applications.
When JVM compiles the class file it does not compile the full
class file in one shot. Compilation is done on function basis or
file basis. Depending on need basis the compilation is done.This type of compilation is termed as JIT or Just-in- Time
compilation.
-
8/11/2019 itectural analays
15/44
15
General ( java1.5Tiger & java1.6Mustang (wild horse))
can run your compiled code on any operating system without
recompiling your source code JVM and Java Application Programming Interface (API) that are
kinds of readymade software components
Java programming environment: Set of libraries that provide
services such as GUI, data structures,etc.
Java enabled browsers: Browsers that include a JVM + ability toload programs from remote hosts
Platform Dependent JVM, Java Compiler, Java Interpreter, C
program, JDK.
Platform IndependentJava program.
JDK (Java Development Kit) Collection of various tools,required to develop and run program
Tools ( JRE, Java Compiler, Java Interpreter)
-
8/11/2019 itectural analays
16/44
16
Path & classpath
To run the program , Set the properties, then only MS-DOS
knows about java commands. (Temporary) set path=%path%; (jdk - bin) (Binary files will be accessible in all
the folders & driver of OS)
set classpath=%classpath%.; (jdk - lib) (The class will be
available in all the folder of OS)
The other way of setting up the path My Computer Advanced Environment variables proceed.
(Permanent)
JAVA_HOMEjdk
Importance of path: Binary will be accessible in all the folders
and driver of O.S
Importance of classpath: .class files will be available in all the
folders of O.S
-
8/11/2019 itectural analays
17/44
17
OOPs Concepts- PIE
Abstraction: Providing essential properties and
operation of an object by hiding internal things. Encapsulation: Placing all the properties and
operations of an object in one place.
The place is called as class, Properties are called as
variables, Operations are called as methods. Polymorphism : Remote control application - one
button is used for switch on/ off.
-
8/11/2019 itectural analays
18/44
-
8/11/2019 itectural analays
19/44
19
How are Java programs written?
Define a class HelloWorld and store it into a file:
HelloWorld.java:public class HelloWorld {
public static void main (String[] args) {
System.out.println(Hello, World);
}
}
Compile HelloWorld.javajavac HelloWorld.java
Output: HelloWorld.class
Runjava HelloWorld
Output: Hello, World
-
8/11/2019 itectural analays
20/44
20
How are variables declared?
Fibonacci:
class Fibonacci {public static void main(String[] arg) {
int lo = 1;
int hi = 1;
System.out.println(lo);
while (hi < 50) {
System.out.println(hi);
hi = hi + lo;
lo = hi lo;
}}
}
-
8/11/2019 itectural analays
21/44
21
How to define expressions?
Arithmetic: +, -, *,/, %, =
8 + 3 * 2 /4
Use standard precedence and associativity rules
Predicates: ==, !=, >, =,
-
8/11/2019 itectural analays
22/44
22
How are simple methods defined?
Every method is defined inside a Java class definition
public class Movie {public static int movieRating(int s, int a, int d) {return s+a+d;
}
}
public class Demo {
public static void main (String argv[]) {
int script = 6, acting = 9, directing = 8;
displayRating(script, acting, directing);
}
public static void displayRating(int s, int a, int d){
System.out.print(The rating of this movie is);
System.out.println(Movie.movieRating(s, a, d));
}
}
-
8/11/2019 itectural analays
23/44
23
How are control structures specified?
Typical flow of control statements: if-then-else, while, switch, do-while, and blocksclass ImprovedFibo {
static final int MAX_INDEX = 10;
public static void main (String[] args) {
int lo = 1;
int hi = 1;
String mark = null;for (int i = 2; i < MAX_INDEX; i++) {
if ((i % 2) == 0)
mark = " *";
else mark = "";
System.out.println(i+ ": " + hi + mark);hi = lo + hi;
lo = hi - lo;
}}}
-
8/11/2019 itectural analays
24/44
24
What are classes and objects?
Classes: templates for constructing instances
Fields
Instance variables
Static variables
Methods
Instance
Static
class Point {
public double x, y;
}
Point lowerleft = new Point();
Point upperRight = new Point();
Point middlePoint = new Point();
lowerLeft.x = 0.0; lowerLeft.y = 0.0;
upperRight.x = 1280.0; upperRight.y = 1024.0
middlePoint.x = 640.0; middlePoint.y = 512.0
-
8/11/2019 itectural analays
25/44
25
How are instance methods defined?
Instance methods take an implicit parameter: instance
on which method is invokedpublic class Movie {public int script, acting, directing;
public int rating() {return script + acting + directing;
}
}
public class Demo {public static void main (String argv[]) {
Movie m = new Movie();
m.script = 6; m.acting = 9; m.directing = 8;
System.out.print(The rating of this movie is);System.out.println(m.rating());
}
}
-
8/11/2019 itectural analays
26/44
26
How to extend classes?
Inheritance: mechanism for extending behavior of
classes; leads to construction of hierarchy of classes[Note: no multiple inheritance]
What happens when class C extends class D:
Inherits instance variables
Inherits static variables Inherits instance methods
Inherits static methods
C can:
Add new instance variables
Add new methods (static and dynamic)
Modify methods (only implementation)
Cannot delete anything
-
8/11/2019 itectural analays
27/44
27
How to extend classes?
public class Attraction {public int minutes;public Attraction() {minutes = 75;}public int getMinutes() {return minutes;}public void setMinutes(int d) {minutes = d;}
}public class Movie extends Attraction {
public int script, acting, directing;public Movie() {script = 5; acting = 5; directing = 5;}public Movie(int s, int a, int d) {
script = s; acting = a; directing = d;}public int rating() {return script + acting + directing;}
}public class Symphony extends Attraction {
public int playing, music, conducting;public Symphony() {playing = music = conducting = 5;}public Symphony(int p, int m, int c) {
playing = p; music = m; conducting = c;}public int rating() {return playing + music + conducting;}
}
-
8/11/2019 itectural analays
28/44
28
What are abstract classes?
Abstract class: Merely a place holder for class
definitions; cannot be used to create instances.;public abstract class Attraction {
public int minutes;public Attraction() {minutes = 75;}public int getMinutes() {return minutes;}public void setMinutes(int d) {minutes = d;}public abstract void m();
}
Following is an error:Attraction x;x = new Attraction();
Following is not an error:
public class Movie extends Attraction { }public class Symphony extends Attraction { }Attraction x;x = new Movie ();x = new Symphony();
-
8/11/2019 itectural analays
29/44
29
Packages
Object
Attraction Auxiliaries Demonstration
Movie Symphony
extends
extends
How do we organize above classes into a single unit? Put them in file?
However, only one public class per file (whose name is same as files)
Solution: Place several files (compilation units) into a package
-
8/11/2019 itectural analays
30/44
30
Packagescontd.
units of organizing related Classes, Interfaces, Sub
packages Why?
Reduce name clashing
Limit visibility of names
Java programs typically organized in terms ofpackages and subpackages
Each package may then be divided into several packages,
subpackages, and classes
Each class can then be stored in a separate file
Each source file starts with something like:package mypackage;
Code in source file is now part ofmypackage
-
8/11/2019 itectural analays
31/44
31
Packagescontd.
package onto.java.entertainment;public class Movie extends class Attraction {}
package onto.java.entertainment;import java.io.*;import java.util.*;
public class Auxiliaries { }
package onto.java.entertainment;public abstract class Attraction { }
Where to store packages?
How does Java find packages?
Export and Import
Access control
-
8/11/2019 itectural analays
32/44
32
Exceptions
public class A {public void foo() throws MyException {
if(aBadThingHappened()) {throw new MyException();}
}public void bar() {
try {this.foo();
} catch (MyException e) {e.printStackTrace();
}}
}
public class MyException extends Exception {public MyException() {}
public MyException(String message) {super(String message);
}}
-
8/11/2019 itectural analays
33/44
33
Finally
public class A {public void foo() throws MyException {
throw new MyException();}
}public void bar() {
try {this.foo();
} catch (MyException e) {e.printStackTrace();
} catch (YourException e) {e.printStackTrace();
} finally {... // always executed before leaving the try/catch
}
}}
-
8/11/2019 itectural analays
34/44
34
Resources
http://java.sun.com/
Java[tm] 2 Platform, Standard Edition v1.4.1
java, javac, jar, jre, etc.
Any platform... FREE!
Online documentation and tutorials
http://www.eclipse.org/
Integrated development environment (IDE) for nothing in particular Java[tm] development tools (JDT) (comes with Eclips)
Project management
Editor
Incremental compiler
CVS support
C/C++ extension in progress
AspectJ support
Windows, Linux, and Mac.... FREE!
http://java.sun.com/http://www.eclipse.org/http://www.eclipse.org/http://java.sun.com/ -
8/11/2019 itectural analays
35/44
35
Qualifiers
publicany class* may access
(no qualifier) package protected only the class*and classes* in the same package may access
protectedonly the class* and decendent classes*
may access
privateonly the class* may access
The class or instances of the class (an object of the class)
-
8/11/2019 itectural analays
36/44
36
Package Protectedpackage edu.ucdavis;public class A {
int x;
}package edu.ucdavis;public class B {
void foo(A a) { a.x; } // OK, same package}
package edu.ucdavis.cs;public class B {
void foo(A a) { a.x; } // Not OK, different package}
package edu;public class B {
void foo(A a) { a.x; } // Not OK, different package}
package edu.ucdavis.cs;public class B {
void foo(A a) { a.x; } // Not OK, different package}
package org.omg;public class B {
void foo(A a) { a.x; } // Not OK, different package}
-
8/11/2019 itectural analays
37/44
-
8/11/2019 itectural analays
38/44
38
Threads
Multiple threads of execution within the same
program, share the same memory space ->lightweight.
Perform multiple tasks at the same time.
Work on the same task in parallel.
Heavily used in user interfaces. Web browsers: load web pages while the user can still scroll,
go back, open a new window, etc.
Web servers: serve multiple requests in parallel.
Can take advantage of multiple processors.
Threads in Java Java manages and schedules threads
Java provides synchronize to help coordinate multiplethreads
C
-
8/11/2019 itectural analays
39/44
39
Creating a Thread in Java
public class MyThread extends Thread {public MyThread(String threadName) {
super(threadName);}public void run() {
for(int i = 0; i < 10; i++) {System.out.println(i + + getName());try {
sleep((long)(Math.random() * 1000));
} catch(InterruptedException e) {}}
}}
C i Th d i J
-
8/11/2019 itectural analays
40/44
40
Creating a Thread in Java
public class ThreadTest {public static void main(String[] args) {
for(int i = 0; i < args.length; i++) {MyThread t = new MyThread(args[i]);t.start();
}}
}
> java ThreadTest Bob Frank0 Bob0 Frank1 Bob2 Bob1 Frank
3 Bob2 Frank3 Frank4 Frank...
C ti Th d i J i I t f
-
8/11/2019 itectural analays
41/44
41
Creating a Thread in Java via Interface
public class MyRunnable implements Runnable {String name;public MyRunnable(String name) {
this.name = name;}public void run() {
for(int i; i < 10; i++) {System.out.println(i + + name());try {sleep((long)(Math.random() * 1000));
} catch(InterruptedException e) {}}
}}
public class ThreadTest {public static void main(String[] args) {
for(int i = 0; i < args.length; i++) {Thread t = new Thread(new MyRunnable(args[i]), args[i]);t.start();
}}
}
P d C P bl
-
8/11/2019 itectural analays
42/44
42
Producer Consumer Problem
public class Producerextends Thread {private Share shared;
public Producer(Share s) {shared = s;
}
public void run() {for(int i = 0; i < 10; i++){
shared.put(i);}
}}
shared.put(0)
shared.get() // 0 gotten
shared.get() // 0 gotten again!!
shared.put(0)
shared.put(1)
shared.get() // 0 never gotten!!
public class Consumerextends Thread {private Share shared;
public Consumer(Share s) {shared = s;
}
public void run() {int value;
for(int i = 0; i < 10; i++) {value = shared.get();
}}
}
// what about simultaneous// access?!shared.put(0) shared.get()
RACE CONDITIONS!
S h i d
-
8/11/2019 itectural analays
43/44
43
public class Share {private int s;
public synchronized int get() { ... }public synchronized void put(int s) { ... }}
Synchronized
Synchronized provides mutual exclusion on an object For any object, only one thread may execute inside
any of that objects synchronized methods
Share s1 = new Share();Share s2 = new Share();
Thread t1 = ...;Thread t2 = ...;
t1 -> s1.get() // gets int2 -> s1.put(32) // blocks
t1 -> s1.get() // gets int2 -> s2.put(4) // gets in
P d C C di ti
-
8/11/2019 itectural analays
44/44
44
public class Share {private int s;private boolean empty = true;
public synchronized int get() {while (empty == true) {
try {wait(); // nothing to get, wait} catch (InterruptedException e) {}
}empty = true;
notifyAll(); // wakeup waiting Consumers/Producersreturn s;}public synchronized void put(int s) {while (empty == false) {
try {wait(); // no room} catch (InterruptedException e) {}
}this.s = s;empty = false;notifyAll(); // wakeup waiting Consumers/Producers
}}
Producer Consumer Coordination
top related