Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
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
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
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++
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
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.
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.
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 ;
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 ;)
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 ;)
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 ;)
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 ;)
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 ;)
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
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");}
}
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();
}}
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
}
// ...
}
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;}
}
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 ;
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!");
}}
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;
}}
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 ;
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));
}}
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.
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.
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);
}}
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);
}}
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 ;)
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
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 ;
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
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
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
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 ;
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 ;
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 ;)
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;
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 . . . ;)
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++
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 ;
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 ;
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++