EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur...

41
EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and Virtual Machine Common Usage Java OOP Everything is Object Everything in Object Entry Points Classes Inheritance and Subtyping Late Binding Fields and Methods Visibility Compilation Basics Usage CLASSPATH and VM Issues Packages Ant Issues Performance Safe or Not ? Java v.s. C++ EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle [email protected] http://wiki-prog.kh405.net

Transcript of EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur...

Page 1: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

EPITA Première Année Cycle IngénieurAtelier Java - J1

Marwan Burelle

[email protected]://wiki-prog.kh405.net

Page 2: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Plan

1 IntroductionA Brief PresentationBytecode and Virtual MachineCommon Usage

2 Java OOPEverything is ObjectEverything in ObjectEntry PointsClassesInheritance and SubtypingLate BindingFields and Methods Visibility

Page 3: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Plan

3 CompilationBasics UsageCLASSPATH and VM IssuesPackagesAnt

4 IssuesPerformanceSafe or Not ?Java v.s. C++

Page 4: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Plan

1 IntroductionA Brief PresentationBytecode and Virtual MachineCommon Usage

2 Java OOP

3 Compilation

4 Issues

Page 5: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

A Bit of History

JAVA was created in 1991 by James Gosling of SUN.The first public implementation (v1.0) in 1995.Java’s philosophy : Write Once, Run Anywhere(WORA)Goals : a Virtual Machine and an Object OrientedLanguage with a C/C++ like syntax.From November 2006 to may 2007 SUN moves Javato Open Source.

Page 6: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Primary Goals

There were five primary goals in the creation of the Javalanguage :

1 It should use the object-oriented programmingmethodology.

2 It should allow the same program to be executed onmultiple operating systems.

3 It should contain built-in support for using computernetworks.

4 It should be designed to execute code from remotesources securely.

5 It should be easy to use by selecting what wereconsidered the good parts of other object-orientedlanguages.

Page 7: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Bytecode Compiling

The major innovation of Java (in the first place) was thebytecode compilation model and its Virtual Machine.

Keep the whole framework portable across differentplatforms ;Allows a quick widespread ;Optimizations focus upon one virtual architecture ;Introduce the notion of code mobility ;

Page 8: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Write Once, Run Anywhere

The JVM was intend to be platform independant ;Normally, a program compiled for the JVM will runon any valid JVM on any OS on any CPU ;Mobile Code : applications can send bytecode (classesor complete programs) to any working JVM ;Mobile Code classic example : browser applet (classesexecuted inside a browser ;)A lot of hardware configuration utilities (such asLine6 POD utilities) benefits from WORA (utilitiescan run on any computer provide a JVM isavailable ;)

Page 9: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

The JVM

java command launches the JVM ;It takes a class (bytecode) as input and execute themain()method of this class ;Can take compressed classes (.jar files ;)Classes are found in the class path (throughCLASS_PATH environment variable or options ;)

Page 10: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Common Usage

Applets : portable mobile code loaded into browser ;Small applications with quick dev cycle and a highportability requirement ;Smooth and fast GUI oriented app (often free gadgetsdistributed over the net with an applet version ;)Web services : the web server acts as a serviceprovider, each request triggers code executions onthe server (servelet model ;)

Page 11: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Applets

An applet is a small class with a limitedenvironment ;The bytecode only requires a JVM (and someclasses ;)The local security is enforced via a sandbox model ;The applet have access to a limited set of localresources (sandbox) and can callback only its originalprovider ;Applets offer more interactivity and expressivitythan basic web pages (html and javascript ;)

Page 12: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Web Services

When a request arrives on the server some code istriggered ;The servlet model assures data persistence within anunconnected model (creating a connection atapplication level, while the connection is notpersistent at network level ;)The whole framework is not system dependant(thanks to the bytecode ;)The servelet model offers a generic API : each serveletdoes not depends on the specific implementation ;Java Extensions (JDBC, XML manipulation, XSLT. . . ) and client-side mechanism (Javascript/AJAX,Flash, applets . . . ) offer a solid framework formodern web applications (web 2.0 ;)

Page 13: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Plan

1 Introduction

2 Java OOPEverything is ObjectEverything in ObjectEntry PointsClassesInheritance and SubtypingLate BindingFields and Methods Visibility

3 Compilation

4 Issues

Page 14: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Basic Programming

Java’s syntax is very similar to C/C++ syntax ;Java offers some basic types (integer, float, boolean. . . ) which can be used directly (unboxed) or throughobject oriented paradigms (boxed ;)Arrays are the only structured types outside objects(but they’re also objects . . . ;)Java offers an exception mechanism (throw andcatch ;)

Exemple:

public class Hello {public static void main(String[] args) {

System.out.println("Hello, World");}

}

Page 15: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

To Be Object Or Not, To Be, That Is The Question

Exemple:

public class PlayWithInt {private int x;private Integer y;public PlayWithInt(int z) {y = new Integer (z);x = z;

}public void print(){System.out.println(y.toString());System.out.println(Integer.toString(x));

}public static void main(String[] args){PlayWithInt o = new PlayWithInt(1);o.print();

}}

Page 16: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

To Be Object Or Not, To Be, That Is The Question

Exemple:

public class PlayWithInt {

// ...

public void times(int n){y = y * n; // object or not ?x *= n; // C like shortcut

}

// ...

}

Page 17: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Everything in Object

No Code outside classes ;Methods (static or not) are completely defined inclasses ;A Java file (.java) defines one public class, otherclasses in the file are private classes only visible fromthe public one ;

Exemple:

public class PlayWithInt {// ...

}private class IntBox {private int x;public IntBox(int z){x = z;}

}

Page 18: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Entering Into The Program

Most programming languages offer a unique entrypoint (like the main() function in C or the BEGIN ...END. block in Pascal ;)Some other languages executed any code inside theprogram (like OCaml ;)In Java, the entry point is defined by the class givenas parameter to the JVM ;Any class can have a class method main, and thuscan serve as entry point of the program ;The mainmethod exists without creating anyinstance of the class, if needed, such an instanceshould be explicitly created (using new constructor)inside the method ;

Page 19: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Entry Point

Exemple:

public class HelloWorld {// Our Entry Point !public static void main(String[] args){// Do some clever things ...System.out.println("Hello World!");

}}

Page 20: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Class Definitions

Exemple:

// A public classpublic class MyClasses {// Some propertiesprivate int some_int;private String a_string;// Constructors// Whithout parameterspublic MyClasses() {some_int = 0;a_string = "";

}// Overloaded Constructorspublic MyClasses(int x, String s){some_int = x;a_string = s;

}}

Page 21: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Class Fields and Methods

Methods and properties can exist outside of object ;These methods and properties are marked as static ;staticmethods can be accessed directly withoutinstantiating an object ;static properties are shared among the class and itsinstance ;The mainmethod is an example of class method ;

Page 22: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Class Fields and Methods

Exemple:

public class MyClass2 {public static int x = 0;public int y;public MyClass2(int z) { y = z; }public static void setX(int z){ x = z; }public static void main(String[] args){MyClass2 o = new MyClass2(1);System.out.println("x = "+ Integer.toString(MyClass2.x)+ " y = " + Integer.toString(o.y));

MyClass2.setX(2);System.out.println("x = "+ Integer.toString(o.x)+ " y = " + Integer.toString(o.y));

}}

Page 23: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Class Fields and Methods

Exemple:x = 0 y = 1x = 2 y = 1MyClass2.x refers property x in the class, thus MyClass2.xand o.x are the same entity.

Page 24: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Inheritance

The inherited fields can be used directly, just like any other fields.

You can declare a field in the subclass with the same name as theone in the super-class, thus hiding it (not recommended).

You can declare new fields in the subclass that are not in thesuper-class.

The inherited methods can be used directly as they are.

You can write a new instance method in the subclass that has thesame signature as the one in the super-class, thus overriding it.

You can write a new static method in the subclass that has thesame signature as the one in the super-class, thus hiding it.

You can declare new methods in the subclass that are not in thesuper-class.

You can write a subclass constructor that invokes the constructorof the super-class, either implicitly or by using the keywordsuper.

Page 25: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Inheritance

Exemple:

public class Animal {public Integer members;public String scream;public Animal(int m, String s) {members = new Integer(m);scream = new String(s);

}public String toString() {return ("Your animal has "+ members.toString()+ " members and "+ scream);

}}

Page 26: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Inheritance

Exemple:

public class Dog extends Animal {// A new fieldsprivate String breed;// The new constructorpublic Dog(String r){// We’re using the parent constructorsuper(4,"barks");breed = new String(r);

}// We override the toString methodpublic String toString(){return ("Your dog is a "+breed);

}}

Page 27: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Subtyping (so-called polymorphism)

Inheritance provide a form of subtyping : if MyClass2is a subclass of MyClass thus instances of MyClass2can be used where an instance of MyClass is awaited ;Subtyping can be seen as way a of describing objectby their behaviour : this object can be use herebecause it provides these methods ;Interfaces extends the notion of subtyping (when aclass implements an interface, it can be seen as oftype of this interface ;)In OOP, subtyping is often referred to aspolymorphism while it does not correspond to MLnor System F like polymorphism ;Real polymorphism (precisely F-boundedpolymorphism) can be found using Generics (seelater ;)

Page 28: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Java Object Hierarchy

Page 29: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Method Resolution

Method are defined in classes but are called fromobject (instance ;)Late Binding : the method is selected dynamically :

If the method is not overridden or if the class of theobject is not ambiguous, everything happen as usual ;When the method is overridden and the known classof the object (at compile time) was the parent, thanksto late binding, the executed method is the method ofthe instance rather than the method of the knownclass ;

Page 30: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Late Binding

Exemple:

public static void printAnimal(Animal a){System.out.println(a.toString());

}public static void main(String[] args){Animal a = new Animal(4,"meows");Dog d = new Dog("Chihuahua");printAnimal(a);printAnimal(d);

}

Your animal has 4 members and meowsYour dog is a Chihuahua

Page 31: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Visibility

Methods and fields can have an access modifiers :

Access modifiers

Modifier Class Package Subclass Worldpublic yes yes yes yesprotected yes yes yes nonone (default) yes yes no noprivate yes no no no

Page 32: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Plan

1 Introduction

2 Java OOP

3 CompilationBasics UsageCLASSPATH and VM IssuesPackagesAnt

4 Issues

Page 33: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Compiling Java Code

javac is the java compiler ;The simplest way of compiling a file is :> javac HelloWorld.java

This compilation produces a file MyClass.class ;If MyClass contains a static mainmethod, it can bepassed to the JVM :> java HelloWorldHello World!

Each class should have has its own file (in order to bepublic) and the file should be named after the classname ;

Page 34: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

CLASSPATH

java (the JVM) look for the given class in theCLASSPATH ;CLASSPATH is an environment variable (similar toPATH) defining path to class files (or jar file ;)When loading a class, every needed classes must beaccessible using the CLASSPATH ;This path could also be specified using the -cpoption of java ;When compiling a class every needed classes shouldbe available in the class path also ;Fortunately the dot (current directory) is in the classpath by default ;

Page 35: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Packages

Classes can be organized in packages ;If your class belongs to a package, the file must beginwith the statement :

package mypackage;

The compiler and the JVM expect to find classes ofpackage in directory named after the package name(under the class path ;)The package names often show some kind ofhierarchy (i.e. java.awt.color) :

It does not reflect a real inclusion hierarchy ;Each name separated by a dot denotes a directoryname (thus classes of the package java.awt.colorshould be find in the directory java/awt/color/under the class path ;)

Page 36: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Using Packages

A component of a package (namely a class) can beaccessed in several ways :

Using the full name : mypackage.MyClass ;Importing the component at the beginning of thefile : import mypackage.MyClass;Importing the whole package at the beginning of thefile : import mypackage.*;

Static methods or fields can be imported in order toprevent full name usage : myStaticMethod() insteadof MyClass.myStaticMethod() ;Static import is done using :import static mypackage.MyClass.myStaticMethod;

Page 37: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Ant

Ant is a Java build system from Apache ;The rules file (equivalent of make’s Makefile) is inXML ;In short Ant offers the same mechanism as make butJava oriented and more portable (most actions aredescribe without referring to external tool usedunlike make ;)Ant offers simple and efficient way of integrating aproject inside a complete Java oriented web servicesframework (putting things at the right place and soon . . . ;)

Page 38: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Plan

1 Introduction

2 Java OOP

3 Compilation

4 IssuesPerformanceSafe or Not ?Java v.s. C++

Page 39: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Is Java Slow ?

Bytecode programs are often considered slower thannative binary programs ;The previous assumption is partially true : part of theexecution time is lost in the VM ;But, real programs spend quiet some times in I/O orwaiting for system calls to complete (which takes asmuch time in both bytecode and native binary ;)Since Java is Fully Object Oriented (without trollinside), it sometimes loose time and spaceconfronted to a more low level language ;Some restrictions of the language (mainly to remainalmost pure OO) introduce unwanted overhead ;Java also offers some other compilation methods (JustIn Time or Native Compilation) for specific cases ;

Page 40: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Typing Issues

While Java is considered strongly typed there are stillsome possibility of Run-Time errors due to typeunsafe expressions accepted by the compiler ;Java permits dynamic explicit unsafe cast ;Explicit allocation and usage of pointer (as opposedto ML like references) can lead to NULL pointerderefencement error ;Some typing rules does not preserves type safety inorder to satisfy design choices rather than subjectreduction ;Java types system does not distinguish inheritanceand subtyping, which is prove to be unsafe ;

Page 41: EPITA Première Année Cycle Ingénieur Atelier Java - …...EPITA Première Année Cycle Ingénieur Atelier Java - J1 Marwan Burelle Introduction A Brief Presentation Bytecode and

EPITA PremièreAnnée Cycle

IngénieurAtelier Java - J1

Marwan Burelle

IntroductionA Brief Presentation

Bytecode and VirtualMachine

Common Usage

Java OOPEverything is Object

Everything in Object

Entry Points

Classes

Inheritance and Subtyping

Late Binding

Fields and MethodsVisibility

CompilationBasics Usage

CLASSPATH and VM Issues

Packages

Ant

IssuesPerformance

Safe or Not ?

Java v.s. C++

Java v.s. C++