Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade)"— Presentation transcript:

1 Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade) http://www.kingston.ac.uk/~ku00699

2 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).

3 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

4 Stuart Fitz-Gerald (Intro to Java)4 Compiler/Interpreter Platform Independence

5 Stuart Fitz-Gerald (Intro to Java)5 HelloJava1 Example from "Learning Java" by P.Niemeyer and J. Knudsen (O'Reilly 2000) //file: HelloJava1.java public 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); }

6 Stuart Fitz-Gerald (Intro to Java)6 HelloJava1 Example from "Learning Java" by P.Niemeyer and J. Knudsen (O'Reilly 2000) % javac HelloJava1 % java HelloJava1

7 Stuart Fitz-Gerald (Intro to Java)7 Some First Java Examples to try Java Applications –HelloJava1.java from "Learning Java" by P.Niemeyer and J. Knudsen (O'Reilly 2000) –HelloWorld.java from ”Using the Java Language’ Handout) –HelloJava2.java from "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)

8 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'

9 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).

10 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!

11 Stuart Fitz-Gerald (Intro to Java)11 Classes public 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; }

12 Stuart Fitz-Gerald (Intro to Java)12 Objects and Constructors Circle c1 = new Circle(); // an INSTANCE of class Circle c1.r = 3.65; // initialised double 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!

13 Stuart Fitz-Gerald (Intro to Java)13 Class fields (and methods) class Circle Circle... static PI= …... instance c2 PI r =.. circumference area instance anotherC PI r =.. circumference area N.B. See main() method in earlier example

14 Stuart Fitz-Gerald (Intro to Java)14 Subclassing and Inheritance public 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 }

15 Stuart Fitz-Gerald (Intro to Java)15 Subclass class Circle Circle... static PI= …... instance anotherC PI r =.. circumference area PlaneCircle... class PlaneCircle instance pc1 PI r =.. circumference area isInside

16 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"> Hello Web! 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!

17 Stuart Fitz-Gerald (Intro to Java)17 HelloApplet First compile the Applet % javac HelloApplet.java Then load HelloApplet.htm into your favourite browser. Hey presto!!!

18 Stuart Fitz-Gerald (Intro to Java)18 Visibility private (entirely inside this class) package (as above and other classes in same package) protected (as above and subclasses) public (all classes) Packages package mycollection.textTools class TextComponent {…} package mycollection.textTools class MultiText {…} package mycollection.shape class Circle {…} 3 files but 1 package (with 2 inner packages)

19 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

20 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:

21 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 = " ";} System.out.println("Hello " + name);

22 Stuart Fitz-Gerald (Intro to Java)22 A Complete Example import 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; }

23 Stuart Fitz-Gerald (Intro to Java)23 Compiling and Running the Program

24 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!

25 Stuart Fitz-Gerald (Intro to Java)25 Java Security Strong Typing for Security Byte Code Verification Permissions and Policies Security Manager Class Loading


Download ppt "Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade)"

Similar presentations


Ads by Google