Intro To Java: For ECS 160
Intro To Java: For ECS 160
Stoney Jackson
[email protected]
wwwcsif.cs.ucdavis.edu/~jacksoni
Java
Compiler
pretty portable
Java
Bytecode
Java Applet
Java Applet
Server
Client
Server
Client
Server
EJB
JDBC
Compared to C++:
Multi-threaded
Java Features
Interfaces
Exceptions
Concurrency
Packages
Name spaces
Reflection
Applet model
10
11
12
Compile HelloWorld.java
javac HelloWorld.java
Output: HelloWorld.class
Run
java HelloWorld
13
14
Arithmetic: +, -, *,/, %, =
8 + 3 * 2 /4
Use standard precedence and associativity rules
15
}
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));
16
17
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
18
}
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());
19
Inherits
Inherits
Inherits
Inherits
C can:
instance variables
static variables
instance methods
static methods
20
21
int minutes;
Attraction() {minutes = 75;}
int getMinutes() {return minutes;}
void setMinutes(int d) {minutes = d;}
abstract void m();
Following is an error:
Attraction x;
x = new Attraction();
22
Packages
Object
extends
Attraction
Auxiliaries
Demonstration
extends
Movie
Symphony
23
Packages contd.
24
Packages contd.
package onto.java.entertainment;
public abstract class Attraction { }
package onto.java.entertainment;
public class Movie extends class Attraction {}
package onto.java.entertainment;
import java.io.*;
import java.util.*;
public class Auxiliaries { }
25
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);
}
}
26
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
}
}
}
27
Resources
http://java.sun.com/
http://www.eclipse.org/
Project management
Editor
Incremental compiler
CVS support
28
Qualifiers
29
Package Protected
package edu.ucdavis;
public class A {
int x;
}
package edu.ucdavis;
public class B {
void foo(A a) { a.x; } // OK, same package
}
package org.omg;
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 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
}
30
Protected
public class A {
protected int x;
}
public class B extends A {
void foo(A a) { this.x; a.x; } // OK, B is a decendent of A
}
public class C extends B {
void foo(A a) { this.x; a.x; } // OK, C is a decendent of A through B
}
package edu; // Uh oh!
public class D extends C {
void foo(A a) { this.x; a.x; } // OK, D is a decendent of A
}
public class E {
void foo(A a) { this.x; a.x; } // NOT OK, E is NOT a decendent of A
}
31
Threads
32
33
34
Runnable {
name());
* 1000));
e) {}
35
public Producer(Share s) {
shared = s;
}
public Consumer(Share s) {
shared = s;
}
}
shared.put(0)
shared.get() // 0 gotten
shared.get() // 0 gotten again!!
shared.put(0)
shared.put(1)
shared.get() // 0 never gotten!!
}
// what about simultaneous
// access?!
shared.put(0) shared.get()
RACE CONDITIONS!
36
Synchronized
public class Share {
private int s;
public synchronized int get() { ... }
public synchronized void put(int s) { ... }
}
Thread t1 = ...;
Thread t2 = ...;
37
38