Beruflich Dokumente
Kultur Dokumente
Course information
Lectures: 6 x 2 hours TPs: 6 x 2 hours Lecturer: Alexandru Costan
alexandru.costan at inria.fr
Contents
Introduction to Java programming language Inheritance and applications Introduction to Swing Swing interactions Threads Perspectives
Readings
The Java Language Specication The Java Virtual Machine Specication The Java Tutorial Books Java Series Books (on the Sun website)
Further readings
Le langage Java: Concepts et pratique -le JDK 5.0, Irne Charon Introduction la programmation objet en Java: Cours et exercices, Jean Brondeau Algorithmique et programmation en Java: Cours et exercices corrigs,Vincent Granet Java 2.0: De l'esprit la mthode, Michel Bonjour, Gilles Falquet, Jacques Guyot, Andr Le Grand Algorithms in Java: Fundamentals, Data Structures, Sorting, Searching, and Graph Algorithms, Robert Sedgewick
Course website
http://www.irisa.fr/celtique/demange/ens/prog2/
Evaluation
Small project: 20% Main project: 50% Written exam: 30%
History
1991: developed by Sun Microsystems as a small programming language for embedded household devices
1997: Standard
SDK JRE
History
1998: Java 1.2, multiple types of platforms
2004: Java 5
Collections, enumerate concurrent programming
Why Java?
Easy to use
Addresses the weaknesses of older programming languages Object-oriented Supports good programming styles
Features
Built-in multi-threading
Compiler
Java Application: normal Java application running on desktops; console or GUI Java Applet: embedded application running within Web browsers JSP, Servlets
Mobile (embedded) application J2ME Java Card Java RMI - distributed programming
Java Application: normal Java application running on desktops; console or GUI Java Applet: embedded application running within Web browsers JSP, Servlets
Mobile (embedded) application J2ME Java Card Java RMI - distributed programming
Procedural programming
vs. OOP
Limitations of procedural
programming:
Data are separated from processes Passive data, active processes No guarantee of data consistancy and constrainsts Difculty in code maintenance
Hello World
Classes
Are the templates to create objects (by
instantiation)
Declaring a class
public class CLASSNAME {
Declaring a class
public class CLASSNAME {
elds methods
}
Classes
Denition
Instance
Constructors
Every class has a special method(s) called constructor to initialise objects data members
A constructor is invoked when an object is to be created/ allocated by using the new operator Baby myBaby1 = new Baby();
Distinguished at compile time by having different parameter lists Baby myBaby2 = new Baby("Madonna", false); Baby myBaby3 = new Baby("Bono", 2.5);
When no constructors are dened in source code, a default constructor that requires no arguments and does nothing will be automatically provided.
Constructors
public class Baby{ String name; boolean isMale; double weight; public Baby(){ name = "John Doe"; isMale = true; weight = 3.1; } public Baby(String babyName, boolean gender){ name = babyName; isMale = gender; weight = 3.1; } public Baby(String babyName, double kilos){ name = babyName; isMale = true; weight = kilos; } }
Access modiers
public: Accessible anywhere by anyone protected: Accessible only to the class itself and to its subclasses or other classes in the same package private: Only accessible within the current class default (no keyword): accessible within the current package
Data types
Java is a strongly typed language
Every variable must have a declared type There are two kinds of data types
Reference types
Arrays and objects Manipulated via references GradeBook myGradeBook = new GradeBook();
Assignment operator =
Baby tom = new Baby("Tom", 2); Baby alex = new Baby("Alex", 3); tom = alex alex.weight = 5; System.out.print(tom.weight);// 5
Assignment operator =
Baby tom = new Baby("Tom", 2); Baby alex = new Baby("Alex", 3); tom = alex alex.weight = 5; System.out.print(tom.weight);// 5
Reference types
public class Baby{ String name; boolean isMale; double weight; public Baby(){ name = "John Doe"; isMale = true; weight = 3.5; } public void eat(double food){ weight = weight + food; } Baby tom; tom = new Baby(); tom.eat(2.1);
Objects are manipulated via references Object references store object locations in computers memory NO explicit pointers in Java (no direct access to the references) NO pointer operators Directly handle attributes and methods Assignments (=) of references do NOT copy objects content
i.e. check if they point to the same object, NOT whether the content of the objects are the same
int n1 = 1; int n2 = 1; System.out.println(n1 == n2); //true Baby baby1 = new Baby("Tom"); Baby baby2 = new Baby("Tom"); System.out.println(baby1 == baby2); //false
i.e. check if they point to the same object, NOT whether the content of the objects are the same
int n1 = 1; int n2 = 1; System.out.println(n1 == n2); //true Baby baby1 = new Baby("Tom"); Baby baby2 = new Baby("Tom"); System.out.println(baby1 == baby2); //false
i.e. check if they point to the same object, NOT whether the content of the objects are the same
int n1 = 1; int n2 = 1; System.out.println(n1 == n2); //true Baby baby1 = new Baby("Tom"); Baby baby2 = new Baby("Tom"); System.out.println(baby1 == baby2); //false
Garbage collection
To reclaim the memory occupied by objects that are no longer in use Programmers dont have to deallocate objects Java Virtual Machine (JVM) performs automatic garbage collection
However, theres no guarantee when/whether an object is freed before the program terminates
Might not be needed as memory is still available Clean-up tasks must be done explicitly by other clean-up methods
explicit reference to objects attributes and methods parameter passing and return value calling constructor from inside constructor
explicit reference to objects attributes and methods parameter passing and return value calling constructor from inside constructor
Packages
Each class belongs to a package Classes in the same package serve a similar purpose Packages are just directories Classes in other packages need to be imported All classes "see" classes in the same package (no import needed) All classes "see" classes in java.lang
Packages
Usage import path.to.package.foo.Foo; import path.to.package.foo.*; Specic packages java.lang, java.util, java.io, java.awt, java.net, java.applet
Standard I/O
Three stream objects automatically created when a Java program begins executing System.out: standard output stream object
normally enables a program to output data to the screen (console) ex: System.out.println("some text");
Arrays
Indexed list of values: TYPE[] The index starts at 0 and ends at length-1.
int[] values; values = new int[5]; int size = values.length; int[][] M = new int[3][2]; int[] primes = {1,2,3,5,7};
Strings
String - constant strings (non-modiable)
String ch1 = new String("bonjour"); String ch2 = "au revoir"; String ch3 = ch1+ch2; int i = ch1.indexOf('j'); char c = ch2.charAt(3);
Constant data
final attribute
primitive data types: constant values reference types: constant references
final int number = 7; number = ...; //NO! number++; //NO! final Robot R2D2 = new Robot(); R2D2 = ...; //NO! R2D2.positionX = 15; //YES! R2D2.positionY = 20; //YES!
Constant data
final attribute
primitive data types: constant values reference types: constant references
final int number = 7; number = ...; //NO! number++; //NO! final Robot R2D2 = new Robot(); R2D2 = ...; //NO! R2D2.positionX = 15; //YES! R2D2.positionY = 20; //YES!
Constant data
final attribute
primitive data types: constant values reference types: constant references
final int number = 7; number = ...; //NO! number++; //NO! final Robot R2D2 = new Robot(); R2D2 = ...; //NO! R2D2.positionX = 15; //YES! R2D2.positionY = 20; //YES!
Constant data
final attribute
primitive data types: constant values reference types: constant references
final int number = 7; number = ...; //NO! number++; //NO! final Robot R2D2 = new Robot(); R2D2 = ...; //NO! R2D2.positionX = 15; //YES! R2D2.positionY = 20; //YES!
Constant data
final attribute
primitive data types: constant values reference types: constant references
final int number = 7; number = ...; //NO! number++; //NO! final Robot R2D2 = new Robot(); R2D2 = ...; //NO! R2D2.positionX = 15; //YES! R2D2.positionY = 20; //YES!
Constant data
final attribute
primitive data types: constant values reference types: constant references
final int number = 7; number = ...; //NO! number++; //NO! final Robot R2D2 = new Robot(); R2D2 = ...; //NO! R2D2.positionX = 15; //YES! R2D2.positionY = 20; //YES!
Exception handling
If a method p() uses a method q() susceptible to trigger an exception: catch and handle the exception
void p(){ ... try { ... q() ...} catch(xxxException e){//handling e} }
or, propagate the exception void p() throws xxxException{ ... q() ...}
xxxException: derived from Exception or RuntimeException ex: IOException, NullPointerException, NumberFormatException, IndexOutOfBoundsException finally{ ... } associated with a try{...} block, typically for cleanup purposes (closing les, freeing resources)
To take away
object oriented programming language data centric classes: models; objects: instances objects handled through references extensive API