Intro To Java: For ECS 160
Intro To Java: For ECS 160
Whats the first question youve got to ask about a language named Java?
debug
pretty portable
Server
Clients download applets via Web browser Browser runs applet in a Java Virtual Machine (JVM)
Applet
Client
Server
Interactive web, security, and client consistency Slow to download, inconsistent VMs (besides, flash won this war)
7
Client EJB
Server
JDBC
Compared to C++:
no header files, macros, pointers and references, unions, operator overloading, 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
Multi-threaded
9
Java Features
Well defined primitive data types: int, float, double, char, etc.
Control statements similar to C++: if-then-else, switch, while, for Interfaces Exceptions Concurrency Packages Name spaces Reflection Applet model
10
Java byte code: Intermediate representation for Java programs Java compiler: Transform Java programs into Java byte code Java interpreter: Read programs written in Java byte code and execute them Java virtual machine: Runtime system that provides various services to running programs Java programming environment: Set of libraries that provide services such as GUI, data structures,etc. Java enabled browsers: Browsers that include a JVM + ability to load programs from remote hosts
11
How are Java programs written? How are variables declared? How are expressions specified? How are control structures defined? How to define simple methods? What are classes and objects? What about exceptions?
12
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.java javac HelloWorld.java Output: HelloWorld.class Run 13 java HelloWorld
Predicates: ==, !=, >, <, >=, <= public class Demo { public static void main (String[] argv) { boolean b; b = (2 + 2 == 4); System.out.println(b); }
15
}
16
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
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:
Add new instance variables Add new methods (static and dynamic) Modify methods (only implementation) Cannot delete anything
20
21
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:
Packages
Object extends Attraction extends Movie Symphony Auxiliaries Demonstration
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
23
Packages contd.
Each package may then be divided into several packages, subpackages, and classes Each class can then be stored in a separate file
Packages contd.
package onto.java.entertainment; public abstract class Attraction { }
Where to store packages? How does Java find packages? Export and Import Access control
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/
Integrated development environment (IDE) for nothing in particular Java[tm] development tools (JDT) (comes with Eclips)
C/C++ extension in progress AspectJ support Windows, Linux, and Mac.... FREE!
28
Qualifiers
public any class* may access (no qualifier) package protected only the class* and classes* in the same package may access protected only the class* and decendent classes* may access private only the class* may access
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
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.
Java manages and schedules threads Java provides synchronize to help coordinate multiple threads
32
33
name()); * 1000)); 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(); } } } 35
36
Synchronized
public class Share { private int s; public synchronized int get() { ... } public synchronized void put(int s) { ... } }
Synchronized provides mutual exclusion on an object For any object, only one thread may execute inside any of that objects synchronized Share s1 = new Share(); t1 -> s1.get() // gets in methods Share s2 = new Share(); t2 -> s1.put(32) // blocks
37
38