+ All Categories
Home > Documents > Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade) ku00699.

Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade) ku00699.

Date post: 20-Dec-2015
Category:
View: 216 times
Download: 0 times
Share this document with a friend
25
Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade) http://www.kingston.ac.uk/~ku00699
Transcript
Page 1: Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade) ku00699.

Introducing Java

Stuart Fitz-Gerald

(with thanks to Chris Reade)

http://www.kingston.ac.uk/~ku00699

Page 2: Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade) ku00699.

Stuart Fitz-Gerald (Intro to Java) 2

Why Java?

• Overview of Java©• Hello Java Examples• A lightning tour of the language

Java is a relatively modern programming language which was developed at the same time as the web and was designed with the web in mind. It is has become an important Internet Technology because of Platform Independence, Design for Security, Huge Library of Classes (Platform) + J2EE (Java 2 Platform Enterprise Edition).

Page 3: Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade) ku00699.

Stuart Fitz-Gerald (Intro to Java) 3

Overview of Java

• History (1.0->1.3 and Java2, J2EE)• Features

– OO with classes and inheritance,– strong typing and safety,– small,– garbage collection– Huge platform– applets and applications

Page 4: Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade) ku00699.

Stuart Fitz-Gerald (Intro to Java) 4

Compiler/Interpreter

Platform Independence

Page 5: Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade) ku00699.

Stuart Fitz-Gerald (Intro to Java) 5

HelloJava1Example from "Learning Java" by P.Niemeyer and J. Knudsen (O'Reilly

2000)

//file: HelloJava1.javapublic class HelloJava1 extends javax.swing.JComponent {

public static void main(String[] args) { javax.swing.JFrame f = new javax.swing.JFrame("HelloJava1"); f.setSize(300, 300); f.getContentPane().add(new HelloJava1( )); f.setVisible(true); }

public void paintComponent(java.awt.Graphics g) { g.drawString("Hello, Java!", 125, 95); }}

Page 6: Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade) ku00699.

Stuart Fitz-Gerald (Intro to Java) 6

HelloJava1Example from "Learning Java" by P.Niemeyer and J. Knudsen (O'Reilly

2000)

% javac HelloJava1% java HelloJava1

Page 7: Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade) ku00699.

Stuart Fitz-Gerald (Intro to Java) 7

Some First Java Examples to try

• Java Applications– HelloJava1.javafrom "Learning Java" by P.Niemeyer and J. Knudsen (O'Reilly 2000)

– HelloWorld.javafrom ”Using the Java Language’ Handout)

– HelloJava2.javafrom "Learning Java" by P.Niemeyer and J. Knudsen (O'Reilly 2000)

• Java Applets– SimpleApplet.java (+ testSimple.html)

(see notes on compiling and running Java Programs)

Page 8: Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade) ku00699.

Stuart Fitz-Gerald (Intro to Java) 8

A lightning tour of the Java language

• Types• Classes, Objects, Constructors• Subclassing and Inheritance• Interfaces, Packages, Visibility• Exceptions and I/O• Runtime and Environment

– Virtual Machine and Byte Code– Security and Security Manager

Includes Examples from 'Java in a Nutshell'

Page 9: Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade) ku00699.

Stuart Fitz-Gerald (Intro to Java) 9

Types

String name = "Stuart";

boolean b =

!name.endsWith("er")

long filelength = f.length();

Date d = new Date();

String[ ] allfiles =

homedir.list();

Point[ ] [ ] pointTable;

All variable declarations must be typed (and parameters of methods).

Primitive types:

boolean, char, byte, short, int, long, float, double

Plus all Classes

Plus Arrays

Mostly static type checking (but some dynamics and casting).

Page 10: Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade) ku00699.

Stuart Fitz-Gerald (Intro to Java) 10

Classes

DogClass dog = new DogClass();dog.age = 4;dog.speak();

public class DogClass { String name,eyeColor; int age; boolean hasTail;public void speak() { Message msgSpeak = new Message(); msgSpeak.setMessage( "arf, arf" ); msgSpeak.setFrame( new Frame() ); msgSpeak.show(); }}

Let’s look at another example!

Page 11: Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade) ku00699.

Stuart Fitz-Gerald (Intro to Java) 11

Classespublic class Circle { // A class field public static final double PI= 3.14159; // A useful constant

// A class method: just compute a value based on the arguments public static double radiansToDegrees(double rads) { return rads * 180 / PI; }

// An instance field public double r; // The radius of the circle

// Two instance methods: // operate on the instance fields of an object public double area() { // Compute the area of the circle return PI * r * r; } public double circumference() { // Compute the circumference return 2 * PI * r; }}

Page 12: Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade) ku00699.

Stuart Fitz-Gerald (Intro to Java) 12

Objects and ConstructorsCircle c1 = new Circle(); // an INSTANCE of class Circlec1.r = 3.65; // initialiseddouble myarea = c1.area(); // used

public class Circle { public static final double PI = 3.14159; // A constant public double r; // An instance field that holds the radius of the circle

// The constructor method: initialize the radius field public Circle(double r) { this.r = r; }

// The instance methods:compute values based on the radius public double circumference() { return 2 * PI * r; } public double area() { return PI * r*r; }};Circle c2 = new Circle(2.76);

Better:

Multiple constructors are also possible!

Page 13: Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade) ku00699.

Stuart Fitz-Gerald (Intro to Java) 13

Class fields (and methods)

class Circle

Circle ... static PI= … ...

instance c2PIr = ..circumferencearea

instance anotherCPIr = ..circumferencearea

N.B. See main() method in earlier example

Page 14: Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade) ku00699.

Stuart Fitz-Gerald (Intro to Java) 14

Subclassing and Inheritancepublic class PlaneCircle extends Circle { // We automatically inherit the fields and methods of Circle, public double cx, cy;

// A new constructor method to initialize the new fields // It uses a special syntax to invoke the Circle() constructor public PlaneCircle(double r, double x, double y) { super(r); // Invoke the constructor of the superclass, Circle() this.cx = x; // Initialize the instance field cx this.cy = y; // Initialize the instance field cy }

// A new instance method that checks whether a point is inside the circle public boolean isInside(double x, double y) { double dx = x - cx, dy = y - cy; // Distance from center double distance = Math.sqrt(dx*dx + dy*dy); // Pythagorean theorem return (distance < r); // Returns true or false }}

Page 15: Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade) ku00699.

Stuart Fitz-Gerald (Intro to Java) 15

Subclass

class Circle

Circle ... static PI= … ...

instance anotherCPIr = ..circumferencearea

PlaneCircle ...

class PlaneCircle

instance pc1PIr = ..circumferenceareaisInside

Page 16: Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade) ku00699.

Stuart Fitz-Gerald (Intro to Java) 16

Applets

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"><html><head>

<title>Hello Web!</title><meta name="generator" content="BBEdit">

</head><body><applet code="HelloApplet.class" width=200 height=100></applet></body></html>

import java.applet.*;import java.awt.*;

public class HelloApplet extends Applet { public void paint( Graphics g ) { g.drawString( "Welcome to Java Programming!", 25, 25 ); }}

Let’s run this in a browser!

Page 17: Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade) ku00699.

Stuart Fitz-Gerald (Intro to Java) 17

HelloAppletFirst compile the Applet% javac HelloApplet.javaThen load HelloApplet.htm into your favourite browser. Hey presto!!!

Page 18: Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade) ku00699.

Stuart Fitz-Gerald (Intro to Java) 18

Visibilityprivate (entirely inside this class)

package (as above and other classes in same package)

protected (as above and subclasses)

public (all classes)

Packagespackage mycollection.textToolsclass TextComponent {…}

package mycollection.textToolsclass MultiText {…}

package mycollection.shapeclass Circle {…}

3 files but 1 package(with 2 inner packages)

Page 19: Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade) ku00699.

Stuart Fitz-Gerald (Intro to Java) 19

Interfaces

public interface Centered { public void setCenter(double x, double y); public double getCenterX(); public double getCenterY();}

public class CenteredRectangle extends Rectangle implements Centered { . . . // Inherits Rectangle fields + methods // MUST provide Centered methods as in interface . . .}

Example Interface Declaration:

Use of Interface in a Class Declaration:

Gets round 'No Multiple Inheritance' restriction

Page 20: Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade) ku00699.

Stuart Fitz-Gerald (Intro to Java) 20

Interfaces

public class CenteredRectangle extends Rectangle implements Centered { // New instance fields private double cx, cy;

public CenteredRectangle(double cx, double cy, double w, double h) { super(w, h); this.cx = cx; this.cy = cy; }

// We inherit all the methods of Rectangle, but must // provide implementations of all the Centered methods. public void setCenter(double x, double y) { cx = x; cy = y; } public double getCenterX() { return cx; } public double getCenterY() { return cy; }}

Details of the example Class Declaration:

Page 21: Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade) ku00699.

Stuart Fitz-Gerald (Intro to Java) 21

Exceptions (and I/O)

import java.io.*;InputStreamReader sr = new InputStreamReader(System.in);BufferedReader console = new BufferedReader(sr);System.out.print("What is your name: ");String name = null;try{ name = console.readLine();}catch (IOException e){ name = "<" + e + ">";}System.out.println("Hello " + name);

Page 22: Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade) ku00699.

Stuart Fitz-Gerald (Intro to Java) 22

A Complete Exampleimport java.io.*;import java.util.*;public class ComputeArea2{ static double radius; static double area; static final double PI = 3.14159; static private StringTokenizer stok; static private BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 1); public static void main(String args[]) { System.out.println("Enter radius"); radius = readDouble(); area = radius*radius*PI; System.out.println("The area for the circle of radius " + radius + " is " + area); } public static double readDouble() { double d = 0; try { String str = br.readLine(); stok = new StringTokenizer(str); d = new Double(stok.nextToken()).doubleValue(); } catch (IOException ex) { System.out.println(ex); } return d; }}

Page 23: Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade) ku00699.

Stuart Fitz-Gerald (Intro to Java) 23

Compiling and Running the Program

Page 24: Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade) ku00699.

Stuart Fitz-Gerald (Intro to Java) 24

JVM and Byte code

• Java Source• Compile to intermediate form

(Byte code - classes) Platform independent

• Interpret Byte code with Engine (JVM) implemented for each platform or browser plug-in

• Byte code delivered over web (as applets in HTML)

• Potential security problem!

Page 25: Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade) ku00699.

Stuart Fitz-Gerald (Intro to Java) 25

Java Security

• Strong Typing for Security• Byte Code Verification• Permissions and Policies• Security Manager• Class Loading


Recommended