ba22001 introduction to java instructor: jingwu he

33
BA2200 1 Introduction to Java Instructor: Jingwu He

Upload: johnathan-ward

Post on 25-Dec-2015

227 views

Category:

Documents


0 download

TRANSCRIPT

BA2200 1

Introduction to Java

Instructor: Jingwu He

BA2200 2

History of Java• In 1991, Sun started development of a programming

language that should be used for programming of consumer electronics, e.g. Mobile telephones, remote console, ...

• The language was originally named Oak by James Gosling, the creator of the language.

• The market was not ready for this language and the project goal was changed: the language should be for the web.

• The browser HotJava was launched in 1995. The browser was written in Java and could execute applets (code running inside web-pages)

• Also in 1995 Netscape decided to make their webbrowser Java enabled.

BA2200 3

More History

The first versions of the language (e.g. Java 1.02 released 1996) was not ready for industry use, they were to immature (e.g. when it comes to making user interface).

In December 1996 a much more mature version Java 1.1 was release. (some browser has build in support for JDK 1.1.6 and not later versions).

BA2200 4

Full application programming languageAdditional capability as a Web programming language OO programming languageAdopts its looks from C++Compiled to processor-neutral instruction set then interpreted on each supporting platformExtremely fast adoption rate!

What is Java

BA2200 5

Terminology

PackageA grouping of logically related code (classes). For instance, all user-interface code is in a single package; likewise, all input/output code and all network code.

ClassA collection of data and methods that operate on that data.

MethodA group of statements in a class that handle a task.

BytecodesA set of instructions that look like machine code, but are not specific to any processor.

Virtual MachineThe environment in which Java runs. The JVM is responsible for executing the bytecodes and has responsibility for the fundamental capabilities of Java.

BA2200 6

Any Operating System

JAVA Application (Bytecodes)

JAVA Virtual Machine

Any Hardware Platform

Java Architecture

BA2200 7

Platform Independence

Java source Java compile Java bytecode

-HelloWorld.class-Platform Neutral

Java source compiled into intermediary bytecode Application runs anywhere Java VM is ported Applet runs in any Java enabled browser

test suite

class HelloWorld {public static void Main (String args[]){System.out.println("Hello World");}}

javac HelloWorld.java

BA2200 8

Java Program Patterns

• Applications– Java programs which run stand alone

• Web-based Java Applications– Applets

• Dynamic and interactive programs that can run inside a web page displayed by a browser enabled for Java

– Servlets• Programs that run inside request/response oriented servers

– JavaServer Pages• An extension to the servlet architecture. Allow for the separation of

the display of web content and the generation of that content

BA2200 9

Java 2 Platform Editions

Java TechnologyEnabled Services

Java TechnologyEnabled Desktop

Workgroup Server

High-End Server

Micro Edition

Standard Edition

Enterprise Edition

J2ME J2EEJ2SE

BA2200 10

Java 2 Platform Editions

– Java 2 Platform, Micro Edition (J2ME)

• Highly optimized runtime environment

• Targets consumer products

– Pagers, cell phones

– Java 2 Platform, Standard Edition (J2SE)

• Java 2 tools, runtimes, and APIs for developers writing, deploying, and running applets and applications

• Referred to as Java 2 SDK

– Java 2 Platform, Enterprise Edition (J2EE)

• Targets enterprise-class server-side applications.

BA2200 11

Object Oriented Programming

• Conceptualizing a computer program as a set of separate objects that interact with each other.

• Objects can model real life situations– BankAccount object– Bicycle

• Objects contain information and the means of accessing and changing that information.

• Benefits– Reusability– Reliability

BA2200 12

Class

A template for creating objects A class is a blueprint, or prototype, that defines the variables and the

methods common to all objects of a certain kind

new

ClassInstances/objects

BA2200 13

Working with Objects

method

method

data

message

anObject

data encapsulation

BA2200 14

Bank Account Class

name

address

balance

interestRateytdInterestdeposit

withdraw

changeAddress

attributes(variables)methods

BA2200 15

Instance of a class (object)

nameaddress

balance

interestRate

ytdInterestdeposit

withdraw

changeAddress

class

nameaddress

balance

interestRate

ytdInterestdeposit

withdraw

changeAddress

Instance of a class(object)

John Smith1234 Main St

1000

.03

15.25

new

BA2200 16

Many instances of a class (objects)

nameaddress

balance

interestRate

ytdInterestdeposit

withdraw

changeAddress

Instance of a class(object)

John Smith1234 Main St

1000

.03

15.25

nameaddress

balance

interestRate

ytdInterestdeposit

withdraw

changeAddress

Mary Smith2115 Pine St

7500

.04

215.25

Instance of a class(object)

BA2200 17

Messages trigger methods

nameaddress

balance

interestRate

ytdInterestdeposit

withdraw

changeAddress

Mary Smith2115 Pine St

7500

.04

215.25

Instance of a class

BA2200 18

Bicycles

BA2200 19

Bicycle Class

color

currentGear

currentSpeedcurrentCadence

numberOfGearsbrake

changeGear

changeCadence

attributes(variables)methods

BA2200 20

An instance of a class (object)

BA2200 21

Many instances (objects)

BA2200 22

Messages

BA2200 23

How does in work in Java?

• Class contains:– Variables

• To hold values

– Methods• To execute logic

BA2200 24

Java Program naming conventions

• Classes– Start with upper case alphabetic

– Must be stored in a file with the same name and a .java extension

– Use meaningful names

– Examples• BankAccount – good

• Bike - good

• 113BankAccount – invalid

• bankAccount – works, but may get a warning

• Stuff - works, but not meaningful

BA2200 25

Java Program naming conventions

• methods– Start with lower case alphabetic– Use meaningful names– Capitalize words– Examples

• calculateInterest – good• changeSpeed - good• 123Calculate – invalid• CalculateInterest – works, but may get a warning• stuff - works, but not meaningful

BA2200 26

Java Program naming conventions

• variables– Start with lower case alphabetic, _ or $

– Use meaningful names

– Capitalize words

– Examples• int areaCode – good

• int currentSpeed - good

• boolean 123 – invalid

• float CalculatedInterest – works, but doesn’t follow convention

• int stuff - works, but not meaningful

BA2200 27

class MyClass { class variables myMethod1() { method variables statement 1; statement 2; } myMethod2() { method variables statement 1; statement 2; }}

Java Program structure

scope

BA2200 28

class BankAccount { String name; String address; float balance; float interestRate; changeAddress() { method variables statement 1; statement 2; } withdraw() { method variables statement 1; statement 2; } deposit() { method variables statement 1; statement 2; }}

Java Program structure

BankAccount myAccount = new BankAccount();

BA2200 29

class Bike { int currentSpeed; int currentCadence; int numberOfGears; int currentGear; changeGears() { method variables statement 1; statement 2; } changeSpeed() { method variables statement 1; statement 2; } brake() { method variables statement 1; statement 2; }}

Java Program structure

BA2200 30

Creating Objects

• new operator– Creates an instance of class (object)

• Bike myBike = new Bike()

– Allocates memory

• Deallocating objects– Done by Java garbage collection

BA2200 31

Working with Objects

changeSpeed statusspeed

myBike

BA2200 32

Java Programming

SDKJava Software Development Toolkit

Available from Sun Microsystems, Inc.SDK 1.4 is the current version

Also version 5.0 RC (Released Code)Known as JDK in early versions

Development environments (IDE)IBM - WebSphere Studio Application DeveloperSun - ForteMicrosoft - Visual J++Borland - JBuilderOracle - JDeveloperAnd others

BA2200 33

Java Translation

Enter/edit the Java program

Save

Source

Compiler

Java Bytecode

Success

FailCorrect errors

.java

file

Java

Java Bytecode

Interpreter

Machine LanguageInstructions

CPU Execution of Machine Language Instructions

Data for the Program

Program Output

.class file

JVM

Syntax errors

Logic errors