Date: 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
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
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 James Gosling of Sun Microsystem created by Brendam Eich at Netscape must be placed inside an HTML document to function can stand on its own large, complicated languagesmall, 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 Windows Linux Windows JVMLinux JVM Java Application Compiler Java Source Class file (byte code)
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 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! “ ); }
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 // 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); set up using System.in 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
Example: Value & Reference int x = 5; int y = 2; x = y; String Sx = new String ("five"); String Sy = new String ( “ two"); Sx = Sy y x 5 five 2 two 2 Sx Sy Garbage: can ’ t be reached from the program – could be returned to the run-time system five
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 base with extra facilities
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 „Encyclopedia of Computer Science“ fourth edition ISBN
Thank you for attention !