240 likes | 382 Views
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
E N D
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 Inheritance What Java hasn‘t got CONTENT
Hi, I‘m duke, the Java Mascot 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
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) • 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
Java vs. JavaScript Object Oriented Programming languages appeared in 1995 created by Brendam Eich at Netscape created by James Gosling of Sun Microsystem 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 before it can run on the web directly interpreted 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 Class file (byte code) Compiler Java Application Java Application Windows JVM Linux JVM Linux Windows
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; /* 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); a string object can tell you its length and return individual characters System.out is like count
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, =, if are the same as in C++
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
y x Sx Sy five two five Example: Value & Reference int x = 5; int y = 2; x = y; String Sx = new String ("five"); String Sy = new String (“two"); Sx = Sy 5 2 2 Garbage: can’t be reached from the program – could be returned to the run-time system
the child is like the base with extra facilities Base class Inheritance Child Class • 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
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)
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