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

Slides:



Advertisements
Similar presentations
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Advertisements

Portability and Safety Mahdi Milani Fard Dec, 2006 Java.
Lab Information Security Using Java (Review) Lab#0 Omaima Al-Matrafi.
JAVA METHODS and CONSTRUCTORS. 2 JAVA Classes The class is the fundamental concept in JAVA (and other OOPLs) A class describes some data object(s), and.
Lab#1 (14/3/1431h) Introduction To java programming cs425
Client Side Programming Using Java Applet Outcomes: You will be expected to know: – Java Applets and HTML file; –bytecode and platform independent programs;
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
CIS 368/568 BU209 Brian O’Haire MCSE MCDBA BU322 Offices Hours 7-8 before class Other times by appointment Web.
Introduction to Object- Oriented Programming with Java Spring Semester 2003 Paul Krause.
Principles of Object-Oriented Software Development The language Java.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
1 Java (vs. C++). 2 Object-oriented Programming Java and C++ are the most popular object-oriented programming languages Java Sun Microsystems in 1990.
Introduction to Java CS 331. Introduction Present the syntax of Java Introduce the Java API Demonstrate how to build –stand-alone Java programs –Java.
Java Data Types  Everything is an Object  Except Primitive Data Types  For efficiency  Platform independent  Portable  “slow”  Objects are often.
March 2005Java Programming1. March 2005Java Programming2 Why Java? Platform independence Object Oriented design Run-time checks (fewer bugs) Exception.
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
UMBC CMSC 331 Java JAVA BASICS. UMBC CMSC 331 Java 2 Comments are almost like C++ The javadoc program generates HTML API documentation from the “javadoc”
Developing User Interfaces (DUI) Chris North cs3724: HCI.
Programming in Java; Instructor:Moorthy Introduction, Objects, Classes, Libraries1 Programming in Java Introduction.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Programming Languages and Paradigms Object-Oriented Programming.
Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Java and C++, The Difference An introduction Unit - 00.
Java Programming Robert Chatley William Lee
Lecture 3 Casting Abstract Classes and Methods Interfaces.
Internet Software Development Classes and Inheritance Paul J Krause.
CS 363 Spring 2004 Elizabeth White CS 363 Comparative Programming Languages Java.
G51PRG-Sem2 A Reality Check a sort of quiz Dave Elliman.
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
CIS 644 Aug. 25, 1999 tour of Java. First … about the media lectures… we are experimenting with the media format please give feedback.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.
Introduction to java Prepared By:-Pragnesh Patel Lect. In Computer Dept. NSIT,Jetalpur 1.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Object Oriented Programming Examples: C++, Java Advantages: 1. reusibility of code 2. ability to adapt (extend) previously written code.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Objects and Classes Mostafa Abdallah
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
Overview of Java CSCI 392 Day One. Running C code vs Java code C Source Code C Compiler Object File (machine code) Library Files Linker Executable File.
JAVA METHODS and CONSTRUCTORS. 2 JAVA Classes The class is the fundamental concept in JAVA (and other OOPLs) A class describes some data object(s), and.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
Java Computer Industry Lab. 1 Programming Java Java Basics Incheon Paik.
OOP Basics Classes & Methods (c) IDMS/SQL News
RealTimeSystems Lab Jong-Koo, Lim
Features of JAVA PLATFORM INDEPENDENT LANGUAGE JAVA RUNTIME ENVIRONMENT (JRE) JAVA VIRTUAL MACHINE (JVM) JAVA APP BYTE CODE JAVA RUNTIME ENVIRONMENT.
Spring 2006 Special Topics in Computer Engineering: Java Intro 1 Crash Course in Java Based on notes from D. Hollinger Based in part on notes from J.J.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Topic: Classes and Objects
JAVA METHODS and CONSTRUCTORS
JAVA MULTIPLE CHOICE QUESTION.
Object-Oriented Programming with Java
Internet and Java Foundations, Programming and Practice
Lecture Note Set 1 Thursday 12-May-05
Programming Language Concepts (CIS 635)
Java Programming Language
The Building Blocks Classes: Java class library, over 1,800 classes:
Object-Oriented Software Engineering
JAVA BASICS.
Chap 1. Getting Started Objectives
JAVA BASICS.
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

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

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

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

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

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); }

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

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)

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'

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

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!

Stuart Fitz-Gerald (Intro to Java)11 Classes public class Circle { // A class field public static final double PI= ; // 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; }

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 = ; // 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!

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

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 }

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

Stuart Fitz-Gerald (Intro to Java)16 Applets <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " 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!

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

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)

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

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:

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);

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 = ; 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; }

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

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!

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