Date:11.11.2008 Subject:Distributed Data Processing Name:Maria Br ü ckner

24
Date: 11.11.2008 Subject: Distributed Data Processing Name: Maria Brückner

description

Date:11.11.2008 Subject:Distributed Data Processing Name:Maria Br ü ckner. What is Java? Characteristics Flavors Java vs. JavaScript Java Applications Java Basics. Java Program Example: Hello Java! Example: Parity Calculation Garbage Collection Example: Value & Reference - PowerPoint PPT Presentation

Transcript of Date:11.11.2008 Subject:Distributed Data Processing Name:Maria Br ü ckner

Page 1: Date:11.11.2008 Subject:Distributed Data Processing Name:Maria Br ü ckner

Date: 11.11.2008Subject: Distributed Data ProcessingName: Maria Brückner

Page 2: Date:11.11.2008 Subject:Distributed Data Processing Name:Maria Br ü ckner

CONTENT

What is Java? Characteristics Flavors

Java vs. JavaScript Java Applications Java Basics

Java Program Example: Hello Java! Example: Parity

Calculation Garbage Collection

Example: Value & Reference

Inheritance What Java hasn‘t got

Page 3: Date:11.11.2008 Subject:Distributed Data Processing Name:Maria Br ü ckner

What is Java ?

programming language developed by Sun Microsystems

first public available version of Java (Java 1.0) was released 1995

target: a program can be written once and then runs on multiple operating systems

consists out of a Java compiler, the Java virtual machines, and the Java class libraries

Hi, I‘m duke, the Java Mascot

Page 4: Date:11.11.2008 Subject:Distributed Data Processing Name:Maria Br ü ckner

Characteristics of Java „Write once, run everywhere“

characteristics: platform independent OOP language strongly-typed programming language interpreted and compiled language automatic memory management single inheritance

actively developed via the Java Community Process (JCP)

watchout: Java is case-sensitive!!!

Page 5: Date:11.11.2008 Subject:Distributed Data Processing Name:Maria Br ü ckner

Different Flavors of Java

Sun is now offering 3 “editions”:

Java Standard Edition (Java SE) for general purpose

Java Enterprise Edition (Java EE) Java SE plus various APIs

Java Micro Edition (Java ME) optimized run-time environment for consumer

products

Page 6: Date:11.11.2008 Subject:Distributed Data Processing Name:Maria Br ü ckner

Java vs. JavaScript

Object Oriented Programming languages

appeared in 1995

created by James Goslingof Sun Microsystem

created by Brendam Eichat Netscape

must be placed inside an HTMLdocument to function

can stand on its own

large, complicated language small, simple set of commands

compiled into machine languagebefore it can run on the web

directly interpretedby Web Browsers

Page 7: Date:11.11.2008 Subject:Distributed Data Processing Name:Maria Br ü ckner

Architecture of Java Applications

Java applications are written as text files

java compiler creates platform independent code (bytecode)

bytecode can be executed by the java runtime environment

Java Virtual Machine is a program which knows how to run the bytecode on the operating system

JRE translates the bytecode into native code

Java code

is compiledto produce

byte code

run by JavaVirtual Machine(JVM) to produceresults

Page 8: Date:11.11.2008 Subject:Distributed Data Processing Name:Maria Br ü ckner

Java Applications in a Nutshell

Java programs written in a text files with extension “.java”

applications are .java files with a main() method compile a Java application

javac MyProgram.java this will result in a file of Java byte code, MyProgram.class

run a Java application java MyProgram the Java virtual machine will execute the program in

MyProgram.class

Page 9: Date:11.11.2008 Subject:Distributed Data Processing Name:Maria Br ü ckner

Java Virtual Machine

Windows Linux

Windows JVM Linux JVM

Java ApplicationJava Application

CompilerJava

SourceClass file (byte code)

Page 10: Date:11.11.2008 Subject:Distributed Data Processing Name:Maria Br ü ckner

Portability

uniform run-time system Java Virtual Machine same interface on different processors

interpreted “assembly language” Compiler generates instructions for JVM

no implementation dependencies e.g. define size of types

C++ int could be 32 or 64 bits in Java size of int is 32 bits on every

machine

Page 11: Date:11.11.2008 Subject:Distributed Data Processing Name:Maria Br ü ckner

Robust

simple language

no “pointers” - no direct memory access

strong typing - checked at compile time

run-time bounds & cast checking

exceptions automatically jump to handler code on

error ensure programmer handles faults

Page 12: Date:11.11.2008 Subject:Distributed Data Processing Name:Maria Br ü ckner

Java Basics

syntax & control structures if, for, while, do {} while () – like C++

primitive data types int, char, short, long, float, double – like C++ also byte, boolean

compound data types class: e.g. to represent a person: age, name, … strings: a normal class holding characters arrays: a normal class holding a collection of

items

Page 13: Date:11.11.2008 Subject:Distributed Data Processing Name:Maria Br ü ckner

Java Program

consists of statements

statements are processed in a certain order and / or in parallel

control structures are used to influence the processing of statements

a statement is the smallest unit which can be processed and is ended with ;

Page 14: Date:11.11.2008 Subject:Distributed Data Processing Name:Maria Br ü ckner

Hello Java! a simple Java program the virtual machine will start the main method

of this class if called via java HelloWorld

the filename must be equal to the class name the extension must be .java

class HelloWorld { public static void main (String[] args) { System.out.println(„Hello Java!“); }}

Page 15: Date:11.11.2008 Subject:Distributed Data Processing Name:Maria Br ü ckner

Example: Parity Calculation

to detect errors

add extra “parity” bit to 7 data bits ensure that total number of ones is even

an error will make the total odd

on receipt, count the number of bits if odd, there has been at least one error if even, assume no error cannot detect even number of errors

Page 16: Date:11.11.2008 Subject:Distributed Data Processing Name:Maria Br ü ckner

Parity Calculation: overview

// initialisationString inputData = formattedInput.readLine();

int pos = 0;int parityBit = 0;

/* Calculate the parity bit */…

if (inputData.length() != 7) System.out.println("There should be 7 bits of input");else System.out.println("Result: "+inputData+parityBit);

set up using System.in

a string object can tell you its length and return individual characters

System.out is like count

Page 17: Date:11.11.2008 Subject:Distributed Data Processing Name:Maria Br ü ckner

Parity Calculation: main body

while (pos < inputData.length()){ char current = inputData.charAt(pos);

pos = pos+1; // current position for user (start at 1) switch (current){ case '0': break;

case '1': parityBit = 1 - parityBit; // invert parityBit break;

default: System.out.println("Invalid input: "+current+" at "+(pos));

}}

while, switch, =, ifare the same as in C++

Page 18: Date:11.11.2008 Subject:Distributed Data Processing Name:Maria Br ü ckner

Garbage Collection

memory management - major cause of bugs forget to release memory - lose resources (a leak) use memory after release - unpredictable contents release twice – confuse the memory allocator

C++ explicitly release allocated memory with delete

Java run-time system scans memory release blocks not referenced by program

Page 19: Date:11.11.2008 Subject:Distributed Data Processing Name:Maria Br ü ckner

Example: Value & Reference

int x = 5;int y = 2;x = y;

String Sx = new String ("five");String Sy = new String (“two");Sx = Sy

yx

5

five

2

two

2

Sx Sy

Garbage: can’t be reached from the program – could be returned to the run-time system

five

Page 20: Date:11.11.2008 Subject:Distributed Data Processing Name:Maria Br ü ckner

Inheritance

a class automatically has the methods and properties of its ancestor (base class)

define new class starting from the ancestor can add data members can add methods can change implementation of methods

a class always inherits from 1 ancestor

Base class

Child Class

the child is like the basewith extra facilities

Page 21: Date:11.11.2008 Subject:Distributed Data Processing Name:Maria Br ü ckner

What Java Hasn't Got constants

use 'final' variables - can't be changed structures

combine related values (e.g. name, age, address)

use classes instead pointers

however, objects use the reference model: a field in a object can refer to another object

single byte characters all characters are Unicode (2 byte)

Page 22: Date:11.11.2008 Subject:Distributed Data Processing Name:Maria Br ü ckner

Summary of Java

great similarities with C++

uses reference variables not pointers

classes group data & functions together

inheritance can define new classes by extension

portability through Virtual Machine

concerned with safety: garbage collection

Page 23: Date:11.11.2008 Subject:Distributed Data Processing Name:Maria Br ü ckner

Sources http://www.java.com http://en.wikipedia.org/wiki/Java_(disambiguation) http://java.sun.com/docs/books/tutorial http://www.vogella.de/articles/JavaIntroduction „Encyclopedia of Computer Science“ fourth edition

ISBN 0-333-77879-0

Page 24: Date:11.11.2008 Subject:Distributed Data Processing Name:Maria Br ü ckner

Thank you for attention !