Java


Creative Commons License
This Java tutorial is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Preamble
This tutorial is about basic and advanced features of Java. Please note that some features are implemented from Java ver. 8 only.
Headlines
Java is recognized as “The language of Internet” despite strong competitors like JavaScript, Python, C#… Billions of server computers, desktop computers, devices (meters, TVs, smarphones, blu-ray players…) and smart cards use Java as core programming language. Java evolved with key versions: Java, Java 2, Java 5 (enumerated types, generics), Java 6, 7 and 8 (Java 9: draft).
First rose Pascal, CoBOL, ForTran... and later on...
Java types are primitive (boolean with false and true as possible values), char, byte, short (16-bit long), int, long (64-bit long), float and double. Otherwise, they are constructed as data structures by means of the class and interface keywords.
Rule(s)
Example
float x = 1.F;
Float y = new Float(1.F); // Heavy style, to be avoided…
float z = x + y; // Autoboxing: 'float' and 'Float' are seamlessly compatible
Rule(s)
Examples
int i;
assert(i instanceof int); // Type of a variable like 'i' having a primitive type like 'int' cannot be tested (compilation error) 
assert(i instanceof Integer); // Compilation error as well
String surname = "Barbier";
assert(surname instanceof String); // This works (no assertion error at execution time)! Don't forget that 'java.lang.String' is not a primitive but a constructed type
public class Type {
    boolean attribute0;
    byte attribute1;
    short attribute2;
    int attribute3;
    long attribute4;
    float attribute5;
    double attribute6;
    char attribute7;

    public static void main(String[] args) {
        Type object = new Type();

        object.attribute0 = true;
        System.out.println(object.attribute0); // 'true'
        object.attribute1 = '\r';
        System.out.println(object.attribute1); // '13'
        object.attribute2 = 0x7777;
        System.out.println(object.attribute2); // '30583'
        object.attribute3 = 0xFFFFFFFF;
        System.out.println(object.attribute3); // '-1'
        object.attribute4 = Long.MAX_VALUE;
        System.out.println(object.attribute4); // '9223372036854775807'
        object.attribute5 = 6.5E1F;
        System.out.println(object.attribute5); // '65.0'
        object.attribute6 = 7.2e-2D;
        System.out.println(object.attribute6); // '0.072'
        object.attribute7 = 66;
        System.out.println(object.attribute7); // 'B'
    }
}
Resource(s)
N_INSEE.Java.zip 
Primitive_types.Java.zip 
See also
C++ types
Arrays in Java come from a primitive type.
Rule(s)
Example
assert(! (given_name instanceof Object)); // <=> 'assert(given_name instanceof Object == false);'
Rule(s)
Example
Object tab[] = new Object[1000];
String another_tab[] = new String[1000];
assert(tab instanceof Object[]);
assert(another_tab instanceof Object[]);
// Initialization:
Object my_tab[] = new Object[]{new Object()}; // 'my_tab' is created with one object inside at position '0'
assert(my_tab.length == 1);
Resource(s)
Primitive_types.Java.zip 
See also
C++ arrays
Enumerated types did not exist before Java 5; They were managed in a weird way.
Example
public static final byte Celsius = 0;
public static final byte Fahrenheit = 1;
public static final byte Kelvin = 2;
public Temperature(float value,byte unit)
…
Temperature t = new Temperature(18.F,3); // Error comes from the fact that '3' does not embody a meaningful value for temperature units!
Rule(s)
Example
public enum Temperature_unit {Celsius, Fahrenheit, Kelvin}
…
public Temperature(float value,Temperature_unit unit) // 'Temperature_unit' is simply used as a type
…
Temperature t = new Temperature(18.F,Temperature_unit.Celsius); // Later on, values of 'Temperature_unit' may be passed as arguments 
…
for(Temperature_unit unit : Temperature_unit.values()) System.out.print(“\t” + unit); // Java enumerated types have nice facilities like iterating
Resource(s)
Exception_management.Java.zip 
See also
C++ enumerated types
Constructed types are built from the class keyword. OO programming relies on the class/instance duality: a class is a data structure plus functions (operations or “methods” in Java), which are inseparable of data (attributes or fields) while an instance is a class incarnation (a.k.a. “object”) at execution time. In short, a class is a “mold” for to-be-created instances. OO programming mainly leads us to reuse existing classes in core and external libraries.
Example
java.util.Date my_date = new java.util.Date(0L); // January 1, 1970, 00:00:00 GMT
Rule(s)
Example
final class Program {
    Temperature _target_temperature;
    java.util.Calendar _time;
}
             
public class Programmable_thermostat … {
    private Program[] _program = new Program[8]; // An attribute named '_program' as an array of 8 values, each having 'Program' as type
    …
Resource(s)
Programmable thermostat app.
See also
C++ constructed types
In OO programming, attributes aim at being hidden (Parnas' information hiding principle) to avoid any direct (unsafe) access from any code outside the class. Instead, methods are generally exposed. Only internal (reusable) computations are hidden, leading to inherent (“private”) methods. In this spirit, the Smalltalk programming language philosophy is interesting: attributes are by default not accessible while functions are. To create some access (read and/or write), getters and setters are required.
Example (Smalltalk)
get_x
    "return of _x"
    ^_x.
set_x: x
    "assign _x with x"
    _x := x.
Rule(s)
Example
public class My_class {
    public float a; // To be avoided, absolutely!
    public final float b = 1963.F;
    private double _c;
    …
}
…
My_class mc = new My_class();
mc.a = 1.F; // OK because 'public'
mc.b = 2.F; // Compilation error because 'final'
System.out.println(mc.b); // OK because 'public'
mc._c = 3.; // Compilation error because 'private'
System.out.println(mc._c); // Compilation error because 'private'
Rule(s)
Example
public class My_class {
    …
    private void _f() {…};
    public void g() {…}; // '_f' is probably called inside 'g' and/or in other methods…
}
…
My_class mc = new My_class();
mc._f(); // Compilation error because 'private'
mc.g(); // OK because 'public'

Class attributes and methods

Rule(s)
Example
public class Temperature {
    public static final float Min = -273.15F; // In Celsius
    …
    if(_value < Min) … // Or 'Temperature.Min' when one accesses this (public) attribute from outside the 'Temperature' class
Rule(s)
Example
final public class Leap_year_utility { // See -'final' and inheritance- section to understand why 'final' is here used
    final public static boolean Leap_year(final java.util.Calendar calendar) { // See -More on 'static'- section to understand why 'final' is here used
        return ((calendar.get(java.util.Calendar.YEAR) % 4 == 0) && (calendar.get(java.util.Calendar.YEAR) % 100 != 0)) || (calendar.get(java.util.Calendar.YEAR) % 400 == 0);
    }
}
Resource(s)
N/A
See also
C++ class attributes and functions
Visibility and encapsulation are illustrated by the iceberg metaphor that faithfully represents the idea of encapsulation. The hidden part is called “the implementation” while the visible part is called “the interface”.
Example
private java.util.Calendar _birth_date; // Implementation is by definition hidden, i.e., 'private' here
private static java.util.Calendar _Now() {
    return new java.util.GregorianCalendar();
}
…
public int age() { // Interface is by definition visible, i.e., 'public' here
    return _Now().get(java.util.Calendar.YEAR) - _birth_date.get(java.util.Calendar.YEAR);
}

Variations on visibility

Rule(s)
Example
public class Human_being {
    java.util.Calendar _birth_date; // "package" visibility
    …

public class Family { // The 'Human_being.java' and 'Family.java' files must be in the same directory
    java.util.Set<Human_being> _members = new java.util.HashSet<Human_being>();
    …
    public int last_child_birth_year() {
        int result = 0;
        for (Human_being member : _members) {
            if (result < member._birth_date.get(java.util.Calendar.YEAR)) { // Access to '_birth_date' succeeds because 'Human_being' and 'Family' are in the same package
                result = member._birth_date.get(java.util.Calendar.YEAR); // Access to '_birth_date' succeeds because 'Human_being' and 'Family' are in the same package
            }
        }
        return result;
    }
}
Rule(s)
Example
public class Prisoner {
    private String _file_number;
    public boolean equals(Object p) {
        if(p instanceof Prisoner) {
            return (this._file_number.equals(((Prisoner)p)._file_number)); // 'this' accesses to '_file_number' of 'p'; this is not really private!
        }
        return false;
    }
    …
Rule(s)
Example
//  This entire code is included in the 'My_class.java' file
class Class_with_package_visibility { // "package" visibility ('My_class' is probably a user class, but other classes in the same package may use it!)
…
}
public class My_class {
    private class Class_with_private_visibility { // 'private' visibility (such a declaration is allowed inside the user class only!)
        …
    }
    …
}
Resource(s)
Covariance.Java.zip 
See also
Non-encapsulation in the C programming language
Modifiers are additional information associated with attributes and methods. Their composition is sometimes impossible and/or useless.

Attribute modifiers

public, private, protected, static, final, transient and volatile

Method modifiers

public, private, protected, static, final, abstract, synchronized, native and strictfp
Resource(s)
N/A
See also
C++ modifiers
The naming space in Java relies on hierarchical directories of the Operating System (OS). Reusing this code requires localisation through the import clause.
Example
package my_code;
…
public class X { …

package my_code.my_extra_code; // Absolute path from 'classpath' OS variable
…
import my_code.X; // 'import my_code.*;' -> all is imported
…
public class Y extends X { … // 'X' has been previously imported

public class Y extends my_code.X { … // Alternative: no import, absolute path to 'X'
Rule(s)
Example
export classpath = $classpath:/usr/FranckBarbier

Package import using static

Example (without static import)
double cosinus = Math.cos(Math.PI); // Cosinus of 180 deg.
Rule(s)
Example
import static java.lang.Math.*; // 'cos', 'PI' and others… All static members…
…
double cosinus = cos(PI); // Shorter, instead of 'double cosinus = Math.cos(Math.PI);'
Resource(s)
N/A
See also
C++ naming space
The Java compiler (as C++) generates a default constructor for a class; The constructor's name is that of the class. This constructor is a “classical” function without arguments. One often requires constructors with arguments for initializations typically. Any introduced constructor (with or without arguments) cancels that of the compiler. For this reason, if one intends to keep a no-argument constructor then one needs its re-introduction!

Contrary to C++, Java does not support the notion of destructor. Instead, no-longer-used objects are automatically collected to free memory based on a garbage collector. Garbage collecting in Java is associated with the finalize method of java.lang.Object.
Example
public class Student {
    public Student() { … // The no-argument constructor is re-introduced
    public Student(String student_id) { … // This constructor cancels the compiler's no-argument constructor
    …
Rule(s)
Resource(s)
N/A
See also
C++ constructor and destructor notions
Inheritance is a foundation of OO programming. The principle behind inheritance is the fact that data structures are extended from existing ones. This amounts to adding attributes (structural inheritance) and/or methods (behavioral inheritance).
The reserved keyword in Java for inheritance is extends.
Java supports single inheritance between classes and multiple inheritance between classes and interfaces.
Example of inheritance tree (in French)

Structural inheritance

Example
abstract public class Compte_bancaire {
    private int _id;
    protected float _solde;
    …
 
public class Compte_cheque extends Compte_bancaire {
    protected float _taux_interet;
    protected float _seuil;
    …

Behavioral inheritance

Example
abstract public class Compte_bancaire {
    …
    public int id() { // This method is usable by 'Compte_cheque' objects!
        return _id;
    }
    …

Property extension

Example
public class Compte_epargne_logement extends Compte_epargne {
    public final static float Taux_interet = Livret_A.Taux_interet * 2.F / 3.F; // Fixed rate in the French law
    public float taux_interet() {
        return Taux_interet;
    }
    …

Inheritance and constructors

Example
abstract public class Compte_epargne extends Compte_bancaire {
    protected Compte_epargne(int id,float solde) {
        super(id,solde);
    }
}

Overriding (redefinition)

Example
abstract public class Compte_bancaire {
    …
    abstract public float taux_interet(); // Abstract method
    public void appliquer_taux_interet() {
        _cumul_interets = _solde * (1.F + (taux_interet() / 100.F));
    }
}

public class Compte_cheque extends Compte_bancaire {
    …
    public float taux_interet() { // Abstract nature is removed when overriding
        return _taux_interet;
    }
    public void appliquer_taux_interet() { // Overriding as well
        if(_solde > _seuil) super.appliquer_taux_interet();
    }
}
Rule(s)
Example
// Assumption: 'Daughter' inherits from 'Mother', which inherits from 'Grandmother'. One wants in 'Daughter' to access to 'jewel' (not 'private') in 'Mother':
super.jewel;
// One now wants in 'Daughter' to access to 'other_jewel' (not 'private') in 'Grandmother':
super.super.other_jewel; // Compilation error! Contrary to C++, Java prevents such an access
Rule(s)
Example
public class Note {
    private static int _Increment = 10;
    private final java.io.StringReader _source;
    public final static String Default_header = "Note: ";
    protected StringBuffer _text;

    private void initialization() { // 'private' prevents overriding -> to allow overriding, possibly use 'protected' instead
        _text = new StringBuffer(Default_header);
    }

    public Note(java.io.StringReader source) {
        _source = source;
        initialization(); // Polymorphism does not apply since it is indicated 'private' above
    }
…
public class Confidential_note extends Note {
    public final static String Default_header = "Confidential note: ";
    private void initialization() { // Overriding does not work because 'private' in 'Note'
        _text = new StringBuffer(Default_header);
    }

    public Confidential_note(java.io.StringReader source) {
        super(source);
    }
…
Note cn = new Confidential_note(new java.io.StringReader("Franck Barbier"));

final and inheritance

Rule(s)
Resource(s)
Polymorphism.Java.zip 
See also
C++ inheritance
Polymorphism is intimately associated with inheritance. Polymorphism relies on the testing of objects' type at run-time to choose the “right” code.
Example
Compte_cheque cc = new Compte_cheque(1963,10500.F,2.F,10000.F);
Compte_bancaire cb = cc; // 'cb' points to 'cc' since their mutual types are compatible through inheritance (see inheritance tree above)
cb.appliquer_taux_interet(); // 'appliquer_taux_interet' in 'Compte_cheque' is run since the runtime type of 'cb' is 'Compte_cheque'
cb = cel; // Assumption: 'cel' is a direct instance of 'Compte_epargne_logement'
cb.appliquer_taux_interet(); // Polymorphism again
Rule(s)
Example
Animal a1 = new Cat(); // Assumption: 'Cat' directly or indirectly inherits from 'Animal'
Resource(s)
Polymorphism.Java.zip 
See also
C++ polymorphism
The notion of interface is peculiar to Java. Interfaces are close to abstract classes having no concrete methods at all (declared or inherited). As an illustration, java.util.Comparator<T> is a famous predefined interface for comparing (e.g., sorting) “compliant” objects.
Example
public class Task implements java.util.Comparator<Task> {
    private int _priority;

    public Task(int priority) {
        _priority = priority;
    }

    @Override
    public int compare(Task t1, Task t2) {
        if (t1._priority < t2._priority) {
            return -1; // '-1' (or any negative) is a convention/contract imposed by 'java.util.Comparator<T>'
        }
        if (t1._priority == t2._priority) {
            return 0; // '0' is a convention/contract imposed by 'java.util.Comparator<T>'
        }
        return 1; // '1' (or any positive) is a convention/contract imposed by 'java.util.Comparator<T>'
    }
}

Interfaces: key issues

Rule(s)
Example
Comparable<Integer> i = new Comparable<Integer>(); // Compilation error: attempt to (directly) instantiate an interface
Comparable<Integer> i = new Integer(0); // Instead, 'i' has for (design) type 'Comparable<Integer>' while it has 'Integer' as (runtime) compatible type
Rule(s)
Example
public class Sort_illustration<T extends Comparable<T>> {
    java.util.Vector<T> _implementation;
    void sort(int left, int right) { // Naive (slow) sort
        assert (left < right);
        for (int i = left; i < right; i++) {
            for (int j = i + 1; j <= right; j++) {
                _cost++;
                if (_implementation.elementAt(i).compareTo(_implementation.elementAt(j)) > 0) {
                    T temp = _implementation.elementAt(i);
                    _implementation.setElementAt(_implementation.elementAt(j), i);
                    _implementation.setElementAt(temp, j);
                }
            }
        }
    }
…
public class Elephant implements Comparable<Elephant> {
    private float _weight;
    public int compareTo(Elephant e) {
        if(this._weight < e. _weight) return -1;
        …
    }
…
Sort_illustration<Elephant> elephant_zoo = new Sort_illustration(…); // 'elephant_zoo' is sortable

Multiple interface inheritance

Example
public interface I {
    String S = "S in I";
    void f();
}

public interface J {
    String S = "S in J";
    void f();
}

public interface K extends I, J { …

public class C implements K { …

Conflict about multiple interface inheritance

Example
public interface I {
    String S = "S in I";
    void f();
    void g(I i);
    I h();
}

public interface J {
    String S = "S in J";
    void f();
    void g(J j);
    J h();
}

public interface K extends I, J { … // Compilation error about 'h'

public class C implements K {
    public void f() { // Compilation error about 'g': 'g' from 'I' or 'g' from 'J'?
        g(this);
    }
    public void g(I i) {
    }
    public void g(J j) {
    }
    public static void main(String[] args) {
        System.out.println(I.S);
    }
}
Resource(s)
Heap.Java.zip 
Multiple_inheritance.Java.zip 
Sort_illustration.Java.zip 
See also
C++ multiple inheritance
Genericity is the ability to express the behavior of a class by using anonymous types. Roughly speaking, a stack has a behavior (pop, push...), which does not depend upon the type of its stored elements (e.g., CHARACTER, INTEGER). Thus, genericity allows the possibility of designing a class by referring to T as “any type”.
Example
public class Bound_stack<T> extends java.util.ArrayDeque<T> { // Note that 'java.util.Stack<T>' is deprecated in Java
    private final int _capacity;
    Bound_stack(final int capacity) {
        _capacity = capacity;
    }
    public boolean full() {
        return _capacity == size(); // From 'java.util.ArrayDeque<T>' without redefinition
    }
    public void push(final T t) {
        if (full()) {
            throw new RuntimeException("push");
        }
        super.push(t); // Omitting 'super' creates recursion
    }
}
…
public static void main(String[] args) {
    Bound_stack<Character> bs = new Bound_stack<Character>(10);
    bs.push('A');
    bs.push('B');
    // bs.removeFirst(); // 'A' would be removed while it's illegal for a stack!
    bs.pop(); // 'A'
}

extendskeyword and generic classes

Example
public class Bank<T extends Compte_bancaire> { // See inheritance tree above, look at 'Compte_bancaire'
    private java.util.Deque<T> _bank_accounts = new java.util.ArrayDeque<T>();
    …
    public T greater_bank_account() { // Say, the account in the bank with the greater balance
        T result = null;
        for(java.util.Iterator<T> i = _bank_accounts.iterator();i.hasNext();) {
            T temp = i.next();
            if(result == null) result = temp;
            if(temp.compareTo(result) > 0) result = temp;
        }
        return result;
    }
…
Bank<Compte_bancaire> a_bank = new Bank<Compte_bancaire>(); // See inheritance tree above, look at 'Compte_bancaire'
Bank<Compte_cheque> another_bank = new Bank<Compte_cheque>(); // See inheritance tree above, 'Compte_cheque' inherits from 'Compte_bancaire'

?character and generic classes

Example
public boolean has_bigger_customer_than(Bank<T> bank) {
    return this.greater_bank_account()._solde > bank.greater_bank_account()._solde ? true : false;
}
…
Bank<Compte_bancaire> b1 = new Bank<Compte_bancaire>();
Bank<Compte_cheque> b2 = new Bank<Compte_cheque>();
boolean result = b1.has_bigger_customer_than(b2); // Compilation error
Rule(s)
Example (appropriate solution)
public boolean has_bigger_customer_than(Bank<?> bank) { … // 'has_bigger_customer_than' is now signed in a different way to remove the compilation error problem
…
Bank<?> a_bank = another_bank; // This is the generalized approach to avoid problems!

superkeyword and generic classes

Example
public class Bank<T extends Compte_bancaire> {
    …
    public void absorption(Bank<T> target_bank) {
        while(! _bank_accounts.isEmpty()) target_bank.add(_bank_accounts.removeFirst());
    }
…
Bank<Compte_bancaire> b1 = new Bank<Compte_bancaire>();
Bank<Compte_cheque> b2 = new Bank<Compte_cheque>();
…
b1.absorption(b2); // Sound compilation error because one may add 'T' objects to 'b2' such that 'T' <= 'Compte_bancaire', but 'T' > 'Compte_cheque'
…
b2.absorption(b1); // In contrast, this compilation error is rather inconsistent, why? 
…
public void absorption(Bank<? extends Compte_bancaire> target_bank) { … // 'absorption' is signed in a different way, but problems still persist with 'b1.absorption(b2);'
…
public void absorption(Bank<? super T> target_bank) { … // This is the single solution, i.e., using the 'super' keyword

Erasure versus reification

Rule(s)
Example
assert(a_bank.getClass() == another_bank.getClass());
…
assert(a_bank instanceof Bank);
assert(another_bank instanceof Bank);
…
assert(a_bank instanceof Bank<?>);
assert(another_bank instanceof Bank<?>

Advanced array management

Generic arrays may be used instead of primitive arrays

Example
java.util.Vector<Character> tab = new java.util.Vector(2); // '<Character>' may be omitted with 'new'
// tab.set(0,'a'); // Execution error: out of bounds, why?
tab.add(0,'a'); // This works
tab.add(1,'b'); // Again, this works
tab.add(2,'c'); // Very lucky!

Conversion of primitive arrays into generic arrays

Example
Short tab[] = {13, 7, 6, 4, 12, 11, 8, 3}; // Caution: 'short tab[]' does not work!
java.util.Collections.sort(java.util.Arrays.asList(tab)); // 'tab' is converted into 'java.util.List<Short>' and next sorted
for (Short s : tab) { // Initial 'tab' array is physically sorted by side effect
    System.out.print("-" + s); // '-3-4-6-7-8-11-12-13' is displayed
}

Conversion of generic arrays into primitive arrays

Example
java.util.Deque<Short> deque = new java.util.ArrayDeque()<Short>
deque.addLast((short) 13);
…
Short other_tab[] = deque.toArray(new Short[0]);

Parameterized (generic) classes with T do not support arrays of type T

Example
public class My_class<T> {
    T tab_containing_T_elements[]; // Declaration is OK
    My_class() {
        tab_containing_T_elements = new T[1000]; // However, instantiation failed at compilation time: 'error: generic array creation'
    }
    …

Generic methods

Rule(s)
Example (first form)
java.util.Deque<Short> deque = new java.util.ArrayDeque<Short>();
deque.addLast((short)13);
deque.addLast((short)7);
 …
Short result[] = (Short[])deque.toArray(); // Mandatory cast from 'Object[]' to 'Short[]'
Example (second form)
Short result[] = deque.toArray(new Short[0]); // Great, no cast!
Rule(s)
Example (second form)
Byte parameter[] = new Byte[0];
Byte result[] = deque.toArray(parameter);
assert (parameter == result && parameter.length == result.length);

Utility classes

Example
java.util.Vector<Integer> vector = new java.util.Vector<Integer>();
vector.addElement(11);
vector.addElement(28);
vector.addElement(6);
vector.addElement(15);
vector.addElement(22);
java.util.Collections.sort(vector);
assert (java.util.Collections.binarySearch(vector, 22) >= 0);
assert (java.util.Collections.binarySearch(vector, 23) < 0);
Resource(s)
Binary_tree.Java.zip 
Bound_stack.Java.zip 
Generics.Java.zip 
See also
C++ genericity
Exception management is a programming style that favors the implementation of a defense strategy when facing up abnormal behaviors, failures or, worst, stronger errors that may come from the operating system itself (e.g., no more memory). Promoted by Ada, exception management relies on a self-contained thread of control compared to the “normal” program execution. Exceptions are raised and caught depending upon the chosen defense strategy. In OO programming, exceptions are plain objects and, in Java, they are instantiated from a predefined (extensible) hierarchy of classes.

Raising and catching exceptions

Example
public Temperature(float value,Temperature_unit unit) throws Exception,Invalid_temperature_exception { // A constructor may have declared exceptions as any Java method
    switch(unit) {
        case Celsius: _value = value;
            break;
        case Fahrenheit: _value = (value - 32.F) * 5.F / 9.F;
            break;
        case Kelvin: _value = value + Min;
            break;
        default: throw new Exception("Illegal temperature unit"); // Raising
    }
    if(_value < Min) throw new Invalid_temperature_exception(_value); // Raising
    …
}

Temperature t;
try {
    t = new Temperature(18.F,Temperature_unit.Celsius);
}
catch(Exception e) { // Catching: normal program execution is diverted from here
    System.exit(1); // Horrible style, to be avoided!
} // Normal execution goes on from here when the 'e' exception does not occur

User-defined exceptions

Example
public class Invalid_temperature_exception extends Exception {
    private float _value; // Exceptions may have attributes as ordinary objects
    public Invalid_temperature_exception(float value) {
        super("Invalid temperature"); // One calls the one-argument constructor of 'java.lang.Exception'
        _value = value;
    }
    public float value() { // Exceptions may have methods as ordinary objects
        return _value;
    }
}

Precise catching

Example
public void decrement() throws Invalid_temperature_exception {
    _value -= _step;
    if(_value < Min) throw new Invalid_temperature_exception(_value);
}

Temperature t = new Temperature();
try {
    t.decrement();
}
catch(Invalid_temperature_exception ite) {
    ite.printStackTrace(); // Old Java style, for debugging purposes only!
}

finally clause

Example
Temperature t = new Temperature();
try {
    t.decrement();
}
catch(Invalid_temperature_exception ite) {
    // 'ite' analysis, partial correction, etc.
    throw ite; // Routing for final processing
} finally {
    // Resource release, etc.
}

Good and bad practices

Example
try {
    // Any Java statement
}
catch(Throwable t) { // Generic catching is useless
    if(t instanceof NullPointerException) … // Very bad idea because it seems that we intend to test 't' against all of its possible types?
}

My_class c1 = new My_class(),c2 = null;
try {
    c2 = (My_class)c1.clone(); // 'clone' in 'java.lang.Object' by default raises an exception
}
catch(CloneNotSupportedException cnse) { // Good style: closer probable exceptions are caught first!
    // 'cnse' as instance of 'CloneNotSupportedException' (including subtypes) is caught and processed…
}
catch(Throwable t) {
    // Any other exception whose type does not comply with 'CloneNotSupportedException'
    // However, one may note that poor information is available since one only knows that 't instanceof CloneNotSupportedException == false'
}

Multi-catch

Example
try {
    // Java statement possibly raising occurrences of 'NoSuchAlgorithmException' or 'UnsupportedEncodingException'
}

catch(java.security.NoSuchAlgorithmException | java.io.UnsupportedEncodingException e) { // The processing of the 'e' exception is shared by two exception types
    // 'e' is instance of 'NoSuchAlgorithmException' or 'UnsupportedEncodingException' (including subtypes of both)…
}

Assertions

Example
public void decrement() { // No declared exception since, potentially, an instance of 'AssertionError' may be raised…
    _value -= _step;
    assert(_value >= Min); // In general, by default, the compiler generates appropriate code to raise an instance of 'AssertionError' (this compiler behavior may be inhibited through the '-da' argument)
}

Temperature t = new Temperature();
try {
    t.decrement();
} catch(AssertionError ae) {
    // 'ae' as instance of 'AssertionError' (including subtypes) is caught and processed…
}
Rule(s)
  • In fact, assertions in Java are the support for contract programming (contracts are preconditions, invariants and postconditions), a paradigm that comes from the Eiffel OO programming language. However, the Eiffel style is greatly graceful compared to Java.
Example
class SHELL
feature
	… -- Public properties
feature {NONE}
	Enter : CHARACTER is '\r';
	Zero : INTEGER is 0;
	Buffer_size : INTEGER is 256;
 
	buffer : ARRAY[CHARACTER];
	cursor : INTEGER
 
	buffer_cleaning is
		require -- precondition
			not buffer.Void
		do
			buffer.clear_all;
		ensure -- postcondition
			buffer.all_cleared
		rescue
			if … then
			retry
	end; -- buffer_cleaning
	… -- Other private properties
invariant -- invariant
	cursor <= Buffer_size
end -- Shell
Resource(s)
Exception_management.Java.zip 
See also
C++ exception management
The java.lang.Object class is the root of the Java inheritance tree. By default, unless an explicit inheritance declaration, any constructed class inherits from java.lang.Object. So, extends java.lang.Object is an optional expression. This behavior is fairly different from C++.

Multithreading related method(s) in java.lang.Object

void wait() throws InterruptedException // + variants
void notify() // + variants

Object identity related method(s) in java.lang.Object

protected Object clone() throws CloneNotSupportedException
boolean equals(Object o)
int hashCode()
Example
String s1 = "Franck";
String s2 = "Franck";
assert (s1 != s2); // 's1' and 's2' are NOT the same memory object
assert (s1.equals(s2)); // However, 's1' equals to 's2'
assert (s1.hashCode() == s2.hashCode());

Utility related method(s) in java.lang.Object

String toString()

Finalization related method(s) in java.lang.Object

protected void finalize() throws Throwable
Example
System.finalization();
Runtime.getRuntime().runFinalization();
Resource(s)
Hashtable_illustration.Java.zip 
See also
N/A
From Java 5, Java supports the covariance (on return types). As a result, below, cloning in both Female and Male is really considered as an overriding of cloning in Human_being.
Example
public class Human_being {
    java.util.Calendar _birth_date;
    …
    public Human_being cloning() {
        System.out.print("cloning in Human_being...\n");
        return new Human_being(_birth_date.get(java.util.Calendar.DAY_OF_MONTH), _birth_date.get(java.util.Calendar.MONTH), _birth_date.get(java.util.Calendar.YEAR));
    }

public class Female extends Human_being {
 …
    public Female cloning() {
        System.out.print("cloning in Female...\n");
        return new Female(_birth_date.get(java.util.Calendar.DAY_OF_MONTH), _birth_date.get(java.util.Calendar.MONTH), _birth_date.get(java.util.Calendar.YEAR));
    }

public class Male extends Human_being {
 …
    public Male cloning() {
        System.out.print("cloning in Male...\n");
        return new Male(_birth_date.get(java.util.Calendar.DAY_OF_MONTH), _birth_date.get(java.util.Calendar.MONTH), _birth_date.get(java.util.Calendar.YEAR));
    }

Human_being FranckBarbier = new Male(11, 1, 1963);
Human_being clone = FranckBarbier.cloning(); // 'cloning in Male...' is displayed => covariance applies in Java!
Rule(s)
Example
class Human_being
feature
    cloning : like Current is do Result := Current end;
    …
end

x,y : Female -- As direct descendant of 'Human_being'
!!x -- Instantiation
y := x.cloning -- OK, 'cloning' returns a 'Female' object
Resource(s)
Covariance.Java.zip 
See also
C++ covariance
As many languages (C, C++, JavaScript…), Java allows the possibility of having a variable number of arguments for a method (births method of the Family class below).
Example
public class Family {
    java.util.Set<Human_being> _members = new java.util.HashSet<Human_being>();
    …
    public void births(Human_being... children) {
        for (Human_being c : children) {
            _members.add(c);
        }
    }
    …
// Usage:
family.births(new Human_being(6, 12, 1993), new Human_being(15, 4, 1996), new Human_being(22, 2, 2001));
// Alternative usage:
Human_being kids[] = {new Human_being(6, 12, 1993), new Human_being(15, 4, 1996), new Human_being(22, 2, 2001)};
family.births(kids);
Resource(s)
Covariance.Java.zip 
See also
Variable number of arguments in C++
Introspection is the ability of a programming language to be self-described. In practice, types become at run-time (leaf) objects of a given meta-type. java.lang.Class<T> plays this role in Java.
Example
Class<?> class_of_Temperature = Temperature.class; // Access from '.class' Java native operator
Class<?> class_of_Temperature_unit = Temperature.Temperature_unit.class;
assert (class_of_Temperature == class_of_Temperature_unit.getEnclosingClass());
Temperature ambient_temperature = new Temperature(18.F, Temperature.Temperature_unit.Celsius);
assert (class_of_Temperature == ambient_temperature.getClass()); // Access from 'getClass' Java method in 'java.lang.Class<T>'
Rule(s)
Example
java.lang.reflect.Method methods[] = ambient_temperature.getClass().getMethods();
for (int i = 0; i < methods.length; i++) {
    Class<?> types_of_parameters[] = methods[i].getParameterTypes();
    Object arguments[] = null;
    if (methods[i].getName().equals("toString") && methods[i].isVarArgs() == false && methods[i].getParameterTypes().length == 0) {
        System.out.println("Calling 'toString' by introspection on 'ambient_temperature': " + methods[i].invoke(ambient_temperature, arguments));
    }
}
Resource(s)
Introspection.Java.zip 
See also
C++ RunTime Type Information (RTTI)
Serialization
Example
public class Individual implements java.io.Serializable {
    private transient int _age; // '_age' is made 'transient' because its value may be obsolete at the time when 'Individual' objects are reloaded
    …

More on the java.io.Serializable interface

Example
public class GPAO implements java.io.Serializable { … // Non-serializable fields must be marked as 'transient'
Rule(s)
Example
import java.io.*;
…
String filename = "gpao.ser";
GPAO gpao = new GPAO(…);
java.io.FileOutputStream fos = null;
java.io.ObjectOutputStream ous = null;
try {
    fos = new FileOutputStream(filename);
    ous = new ObjectOutputStream(fos);
    ous.writeObject(gpao);
    ous.close();
}
catch(java.io.IOException ioe) {
    …
}

Advanced serialization with XML (java.beans library)

Example
import java.io.*;
…
java.beans.XMLEncoder e = new java.beans.XMLEncoder(new BufferedOutputStream(new FileOutputStream ("gpao.xml")));
e.writeObject(gpao);
e.close();
…
java.beans.XMLDecoder d = new java.beans.XMLDecoder(new BufferedInputStream(new FileInputStream ("gpao.xml")));
GPAO gpao = (GPAO)d.readObject();
d.close();
…
static final long Serial_version_id = 1L; // Versioning
Anonymous classes have no name. They are created on the fly. Their main purpose is avoiding excessive inheritance, i.e., creating descendant classes from other classes or interfaces. For example, java.util.TimerTask is an abstract class. It has a single run method that requires a definition through an inheriting class. Timers accept timer task objects as arguments for later executing run at key instants, e.g., each second. To avoid the creation of such a class (that involves the creation of an associated .java file, etc.) for only defining run, an anonymous class may be useful.
Example
public class Anonymous_class_test {
    public static void main(String[] args) {
        java.util.TimerTask timer_task = new java.util.TimerTask() {
            @Override
            public void run() {
                System.out.println("A behavior…");
            }
        };
        System.out.println(timer_task.getClass().getName()); // 'Anonymous_class_test$1' is displayed!
    …
Rule(s)
Functional interfaces have only one (abstract) method. In the Java library, functional interfaces are annotated with @java.lang.FunctionalInterface. For example, java.lang.Runnable is a functional interface. It has a single run method that requires a (deferred) definition through an inheriting class. Threads requires runnable objects as arguments for later executing (with the start method) run in split threads of control. To avoid the creation of such a class (that involves the creation of an associated .java file) for only defining run, a lambda expression may be useful.
Example
public class Functional_interface_test {
    public static void main(String[] args) {
        Runnable to_do = () -> { // No reference to the 'run' method is required since 'java.lang.Runnable' only owns this single method.
            try {
                Thread.sleep(1000L); // Nothing to do for 1 sec. <=> a kind of delay!
            } catch (InterruptedException ie) {
                java.util.logging.Logger.getLogger("Why it doesn't work?").log(java.util.logging.Level.SEVERE, null, ie);
            }
        };
        new Thread(to_do).start();
    …
Rule(s)
Example
@FunctionalInterface
public interface My_functional_interface {
    void println(Object o);
}
…
My_functional_interface mfi = (o) -> System.out.println(o.toString());
See also
Lambda expression in C++
More on static

Static initializer

Example
final static Terminal[] _Terminals;
…
static { // This code is launched before any statement in the 'main' program:
    int number_of_terminals = …;
    _Terminals = new Terminal[number_of_terminals];
}

Static method hiding using final

Example
public class My_class_mother {
    final static void My_method() {
        System.out.println("I'm both a 'final' and 'static' method in " + My_class_mother.class.getSimpleName() + ", what does it mean?");
    }
}

public class My_class extends My_class_mother {
    //    static void My_method() { // Static methods can be 'hidden' instead of 'overridden', but 'final' in 'My_class_mother' prevents hidding here
// } public static void main(String[] args) { My_class.My_method(); // Normal style, 'final' in 'My_class_mother' prevents any hidding in 'My_class' My_class mc = new My_class(); mc.My_method(); // Weird Java style through instances! } }
Resource(s)
Final_and_static_methods.Java.zip 
The idea of try-with-resource(s) statement block from Java 7 solves historical problems. Typically, having errors when executing any kind of operation on a file invites us to close it in a safely manner. However, the close operation on the java.util.stream.BaseStream<T,S extends BaseStream<T,S>> class itself raises an exception in case of closing problems.

Before Java 7

Example
My_class c = new My_class();
String filename = "c.My_class.ser"; // 'public class My_class implements java.io.Serializable…'
java.io.FileOutputStream fos = null;
java.io.ObjectOutputStream ous = null;
try {
    fos = new java.io.FileOutputStream(filename);
    ous = new java.io.ObjectOutputStream(fos);
    ous.writeObject(c);
}
catch (java.io.FileNotFoundException fnfe) {/* 'fos = new java.io.FileOutputStream(filename);' failed */}
catch (java.io.IOException ioe) {/* 'ous = new java.io.ObjectOutputStream(fos);' or 'ous.writeObject(c);' failed */}
finally {
    if (ous != null) ous.close(); // Compilation problem here!
}

After Java 7

Rule(s)
Example
try (java.io.ObjectOutputStream ous = new java.io.ObjectOutputStream(fos)) {
    ous.writeObject(c);
}
catch (java.io.IOException ioe) {/* 'ous.writeObject(c);' failed, but 'ous' is nonetheless closed */}
Try_with_resources.Java.zip 
From Java 5, significant modifications occur in the way concurrency/multithreading can be managed in Java, including the use of the synchronized and volatile keywords.

Operating system level

Example
java.lang.Process p = new …; // Heavyweight process with probable incompatibilities across OSs like Windows, OS X, LINUX, etc.

Java virtual machine level

Example
java.lang.Thread t = new …; // Lightweight process

Sample

Example
private boolean _stop = false;
private Thread _t1;
…
_t1 = new Thread() {
    public void run() {
        while(! _stop) …
    }
};
…
_t1.start(); // 'Thread' objects are NEVER restartable!
…
_stop = true; // Never used '_t1.stop();'
_t1 = null; // For garbage collector

Support in java.lang.Object

Rule(s)
Example
public class Simple_dead_lock {
    private int i = 0;
    synchronized public void f() {
        System.err.println("Start of 'f()'");
        i++;
        g();
        System.err.println("Value of 'i': " + i);
        notify();
    }
    synchronized private void g() {
        System.err.println("Start of 'g()'");
        try {
            wait(5000); // Dead lock occurs for 5 sec.
        }
        catch(InterruptedException ie) {
            ie.printStackTrace();
        }
        System.err.println("End of 'wait()'");
    }
    public static void main(String[] args) {
        Simple_dead_lock sdl = new Simple_dead_lock();
        sdl.f();
    }
}

synchronized keyword

Synchronized method

Example
synchronized void f() { …

Synchronized block

Example
Temperature t = new Temperature();
synchronized(t) { // Mutex acquisition on 't'
    // Synchronized code…
} // Mutex release on 't'

volatile keyword

Rule(s)

java.util.concurrent package

Rule(s)
java.util.concurrent.atomic.AtomicReference<String> _given_name = new java.util.concurrent.atomic.AtomicReference<String>("Not yet defined");
java.util.concurrent.atomic.AtomicReference<String> _surname = new java.util.concurrent.atomic.AtomicReference<String>("Not yet defined");
Resource(s)
Multithreading.Java.zip 
From Java 7, java.nio.file.FileSystems, java.nio.file.Files or java.nio.file.Paths (list is not exhaustive, see also: Oracle Java 8 API) are utility classes for quicker and non-blocking file manipulation. In this cope, one may imagine the creation a My_Web subfolder (with deletion if already existing) from an existing Web folder. Next a template index.html file in Web is copied to My_Web.
Example (creation)
java.nio.file.Files.createDirectories(java.nio.file.FileSystems.getDefault().getPath("Web").resolve("My_Web"));
index_html_file = java.nio.file.FileSystems.getDefault().getPath("Web" + java.io.File.separator + "My_Web" + java.io.File.separator + "index.html");
java.nio.file.Files.copy((new java.io.File("Web" + java.io.File.separator + "index.html")).toPath(), index_html_file, java.nio.file.StandardCopyOption.REPLACE_EXISTING);
Example (deletion)
java.nio.file.Files.deleteIfExists(index_html_file);
java.nio.file.Files.deleteIfExists(java.nio.file.FileSystems.getDefault().getPath("Web").resolve("My_Web"));
Example (character-based reading and writing)
java.util.List<String> lines = java.nio.file.Files.readAllLines(index_html_file, java.nio.charset.Charset.defaultCharset());
for (String line : lines) {
    if (line.contains("FB")) lines.set(lines.indexOf(line), "Franck Barbier");
}
java.nio.file.Files.write(index_html_file, lines, java.nio.charset.Charset.defaultCharset());
WebSockets is a popular technology in Internet programming. One may simply write a Java WebSockets server: look at here. Instead, one may reuse libraries like Java-Websocket, Jetty, Tyrus or Vert.x.