Download Java Introduction

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Date:
Subject:
Name:
11.11.2008
Distributed Data Processing
Maria Brückner
CONTENT

What is Java?






Characteristics
Flavors
Java vs. JavaScript
Java Applications
Java Basics
Java Program



Garbage Collection



Example: Hello Java!
Example: Parity Calculation
Example: Value & Reference
Inheritance
What Java hasn‘t got
What is Java ?




Hi, I‘m duke,
the Java
Mascot
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
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!!!
Different Flavors of Java
Sun is now offering 3 “editions”:

Java Standard Edition (Java SE)


Java Enterprise Edition (Java EE)


for general purpose
Java SE plus various APIs
Java Micro Edition (Java ME)

optimized run-time environment for consumer products
Java
vs.
JavaScript
Object Oriented Programming languages
appeared in 1995
created by James Gosling
of Sun Microsystem
created by Brendam Eich
at Netscape
can stand on its own
must be placed inside an HTML
document to function
large, complicated language
small, simple set of commands
compiled into machine language directly interpreted
before it can run on the web
by Web Browsers
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 compiled
to produce
byte code
run by Java
Virtual Machine
(JVM) to produce
results
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
Java Virtual Machine
Java
Source
Compiler
Class file (byte code)
Java Application
Java Application
Windows JVM
Linux JVM
Windows
Linux
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
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
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
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 ;
Hello Java!


a simple Java program
the virtual machine will start the main method of this
class if called via java HelloWorld
class HelloWorld {
public static void main (String[] args) {
System.out.println(„Hello Java!“);
}
}


the filename must be equal to the class name
the extension must be .java
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
Parity Calculation: overview
set up using System.in
// initialisation
String inputData = formattedInput.readLine();
int pos = 0;
int parityBit = 0;
a string object can tell you its
length and return individual
characters
/* 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);
System.out is like count
Parity Calculation: main body
while (pos < inputData.length()){
char current = inputData.charAt(pos);
pos = pos+1;
switch (current){
case '0':
break;
// current position for user (start at 1)
while, switch, =, if
are the same as in C++
case '1':
parityBit = 1 - parityBit; // invert parityBit
break;
default: System.out.println("Invalid input: "+current+" at "+(pos));
}
}
Garbage Collection

memory management - major cause of bugs




C++


forget to release memory - lose resources (a leak)
use memory after release - unpredictable contents
release twice – confuse the memory allocator
explicitly release allocated memory with delete
Java


run-time system scans memory
release blocks not referenced by program
Example: Value & Reference
int x = 5;
int y = 2;
x = y;
y
5
2
2
String Sx = new String ("five");
String Sy = new String (“two");
Sx = Sy
Garbage: can’t be reached from
the program – could be returned
to the run-time system
x
five
Sx
Sy
five
two
the child is like the base
with extra facilities
Inheritance


Child Class
a class automatically has the methods and
properties of its ancestor (base class)
define new class starting from the ancestor




Base class
can add data members
can add methods
can change implementation of methods
a class always inherits from 1 ancestor
What Java Hasn't Got

constants


structures



use 'final' variables - can't be changed
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)
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
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
Thank you for attention !