<!
DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Fiche de révision Java</title>
<style>
body {
font-family: Arial, sans-serif;
line-height: 1.6;
color: #333;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
h1 {
color: #2c3e50;
text-align: center;
border-bottom: 2px solid #3498db;
padding-bottom: 10px;
}
h2 {
color: #3498db;
margin-top: 30px;
border-left: 4px solid #3498db;
padding-left: 10px;
}
h3 {
color: #2980b9;
margin-top: 20px;
}
pre {
background-color: #f8f8f8;
border: 1px solid #ddd;
border-radius: 4px;
padding: 15px;
overflow-x: auto;
}
code {
font-family: 'Courier New', Courier, monospace;
background-color: #f8f8f8;
padding: 2px 4px;
}
table {
border-collapse: collapse;
width: 100%;
margin: 20px 0;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
.important {
background-color: #ffecb3;
padding: 10px;
border-left: 4px solid #ff9800;
margin: 15px 0;
}
.definition {
background-color: #e1f5fe;
padding: 10px;
border-left: 4px solid #03a9f4;
margin: 15px 0;
}
</style>
</head>
<body>
<h1>Fiche de révision Java</h1>
<h2>1. Introduction au langage Java</h2>
<div class="definition">
<p>Le langage Java est un langage généraliste de programmation créé en 1995
par Sun Microsystems (racheté par Oracle en 2009).</p>
<p><strong>Caractéristiques :</strong> orienté-objet, modulaire, rigoureux
(erreurs à la compilation), portable (bytecode interprété)</p>
</div>
<h3>1.1 Environnement Java</h3>
<p>Java est un langage <strong>interprété</strong> :</p>
<ol>
<li>Code source écrit dans des fichiers <code>.java</code></li>
<li>Compilation par <code>javac</code> en bytecode
(<code>.class</code>)</li>
<li>Interprétation par la JVM (Java Virtual Machine)</li>
</ol>
<h4>Compilation :</h4>
<pre>javac MonProg.java
javac -classpath /prog/exos1:/cours MonProg.java
javac -d /prog/exos1 -classpath /cours MonProg.java</pre>
<h4>Interprétation :</h4>
<pre>java MonProg
java -classpath /prog/exos1:/cours MonProg</pre>
<h3>1.2 Programmation orientée-objet</h3>
<div class="definition">
<p>La POO est centrée sur les données (contrairement à la programmation
procédurale centrée sur les traitements).</p>
<p>Un objet regroupe des données (variables) et des traitements
(méthodes).</p>
</div>
<h4>1.2.1 Classe</h4>
<p>Une classe est un type complexe regroupant des données et des méthodes
(encapsulation).</p>
<pre>class Rectangle {
int longueur;
int largeur;
int origine_x;
int origine_y;
void deplace(int x, int y) {
this.origine_x = this.origine_x + x;
this.origine_y = this.origine_y + y;
}
int surface() {
return this.longueur * this.largeur;
}
}</pre>
<div class="important">
<h5>Encapsulation</h5>
<p>Droits d'accès aux données d'un objet :</p>
<ul>
<li><code>public</code> : accès complet depuis l'extérieur</li>
<li><code>private</code> : accès uniquement depuis l'intérieur de la
classe</li>
</ul>
</div>
<h5>Méthode constructeur</h5>
<p>Méthode spéciale invoquée lors de la création d'un objet :</p>
<ul>
<li>Même nom que la classe</li>
<li>Pas de valeur de retour</li>
<li>Plusieurs constructeurs possibles (surcharge)</li>
</ul>
<pre>class Rectangle {
//...
Rectangle(int lon, int lar) {
this.longueur = lon;
this.largeur = lar;
this.origine_x = 0;
this.origine_y = 0;
}
//...
}</pre>
<h4>1.2.2 Objet</h4>
<p><strong>Instanciation :</strong> création d'un objet à partir d'une
classe</p>
<pre>Cercle mon_rond;
mon_rond = new Cercle();
// Ou en une ligne
Rectangle mon_rectangle = new Rectangle(15, 5);</pre>
<div class="important">
<p>En Java, les variables désignant un objet sont des pointeurs (passage
par référence).<br>
Les variables primitives sont passées par valeur.</p>
</div>
<p><strong>Accès aux variables et méthodes :</strong></p>
<pre>int temp = mon_rectangle.longueur;
mon_rectangle.deplace(10, -3);</pre>
<p>Le mot-clé <code>this</code> référence l'objet courant :</p>
<pre>class Carre {
int cote;
int origine_x;
int origine_y;
Carre(int cote, int x, int y) {
this.cote = cote;
this.origine_x = x;
this.origine_y = y;
}
Carre(int cote) {
this(cote, 0, 0); // Appel à l'autre constructeur
}
}</pre>
<h2>2. Syntaxe du langage</h2>
<ul>
<li>Fin d'instruction : <code>;</code></li>
<li>Commentaires : <code>// commentaire</code> ou <code>/* commentaire
*/</code></li>
<li>Identificateurs : lettres, chiffres (pas en 1ère position), _ et $</li>
</ul>
<h3>2.1 Types de données</h3>
<h4>2.1.1 Types primitifs</h4>
<table>
<tr>
<th>Type</th>
<th>Classe équivalente</th>
<th>Valeurs</th>
<th>Défaut</th>
</tr>
<tr>
<td>boolean</td>
<td>Boolean</td>
<td>true ou false</td>
<td>false</td>
</tr>
<tr>
<td>byte</td>
<td>Byte</td>
<td>-128 à 127</td>
<td>0</td>
</tr>
<tr>
<td>char</td>
<td>Character</td>
<td>\u0000 à \uFFFF</td>
<td>\u0000</td>
</tr>
<tr>
<td>short</td>
<td>Short</td>
<td>-32768 à 32767</td>
<td>0</td>
</tr>
<tr>
<td>int</td>
<td>Integer</td>
<td>-2147483648 à 2147483647</td>
<td>0</td>
</tr>
<tr>
<td>long</td>
<td>Long</td>
<td>-2<sup>63</sup> à 2<sup>63</sup>-1</td>
<td>0</td>
</tr>
<tr>
<td>float</td>
<td>Float</td>
<td>±3.4×10<sup>38</sup>, ±1.4×10<sup>-45</sup></td>
<td>0.0</td>
</tr>
<tr>
<td>double</td>
<td>Double</td>
<td>±1.7×10<sup>308</sup>, ±4.9×10<sup>-324</sup></td>
<td>0.0</td>
</tr>
</table>
<div class="important">
<p>Java est très rigoureux sur le typage. Les conversions doivent être
explicites :</p>
<pre>int a;
double b = 5.0;
a = (int)b; // Cast obligatoire</pre>
</div>
<h4>2.1.2 Tableaux et matrices</h4>
<pre>// Déclaration
int[] mon_tableau; // Méthode préférée
int mon_tableau2[]; // Style C
// Initialisation avec taille fixe
int[] mon_tableau = new int[20];
// Accès à la taille
int taille = mon_tableau.length;
// Accès aux éléments (indice 0 à n-1)
mon_tableau[3] = 42;</pre>
<h4>2.1.3 Chaînes de caractères</h4>
<p>Classe <code>String</code> du package <code>java.lang</code> :</p>
<ul>
<li>Immuable (valeur non modifiable)</li>
<li>Concaténation avec l'opérateur <code>+</code></li>
</ul>
<pre>String s1 = "hello";
String s2 = "world";
String s3 = s1 + " " + s2; // "hello world"
// Initialisation
String s = new String(); // chaîne vide
String s2 = new String("hello world");</pre>
<h3>2.2 Opérateurs</h3>
<p>Opérateurs par ordre de priorité décroissante :</p>
<table>
<tr>
<th>Priorité</th>
<th>Opérateur</th>
<th>Description</th>
</tr>
<tr>
<td>1</td>
<td>++, --, +, -, !, (type)</td>
<td>Incrémentation, décrémentation, signes, négation, cast</td>
</tr>
<tr>
<td>2</td>
<td>*, /, %</td>
<td>Multiplication, division, modulo</td>
</tr>
<tr>
<td>3</td>
<td>+, -</td>
<td>Addition, soustraction, concaténation</td>
</tr>
<tr>
<td>4</td>
<td><<, >></td>
<td>Décalage de bits</td>
</tr>
<tr>
<td>5</td>
<td><, <=, >, >=, instanceof</td>
<td>Comparaisons, test de type</td>
</tr>
<tr>
<td>6</td>
<td>==, !=</td>
<td>Égalité, différence</td>
</tr>
<tr>
<td>7</td>
<td>&</td>
<td>ET bit à bit, ET booléen</td>
</tr>
<tr>
<td>8</td>
<td>^</td>
<td>OU exclusif</td>
</tr>
<tr>
<td>9</td>
<td>|</td>
<td>OU bit à bit, OU booléen</td>
</tr>
<tr>
<td>10</td>
<td>&&</td>
<td>ET logique</td>
</tr>
<tr>
<td>11</td>
<td>||</td>
<td>OU logique</td>
</tr>
<tr>
<td>12</td>
<td>?:</td>
<td>Opérateur ternaire</td>
</tr>
<tr>
<td>13</td>
<td>=</td>
<td>Assignation</td>
</tr>
</table>
<h3>2.3 Structures de contrôle</h3>
<h4>2.3.1 Instructions conditionnelles</h4>
<pre>// If-else
if (condition) {
// bloc1
} else {
// bloc2
}
// Opérateur ternaire
condition ? instruction1 : instruction2;</pre>
<h4>2.3.2 Instructions itératives</h4>
<p><strong>While :</strong></p>
<pre>while (condition) {
// bloc
}</pre>
<p><strong>Do-While :</strong></p>
<pre>do {
// bloc
} while (condition);</pre>
<p><strong>For :</strong></p>
<pre>for (initialisation; condition; incrément) {
// bloc
}
// Exemple
for (int i = 0, j = 49; (i < 25) && (j >= 25); i++, j--) {
// instructions
}</pre>
<h4>2.3.3 Instructions break et continue</h4>
<ul>
<li><code>break</code> : sortie immédiate du bloc</li>
<li><code>continue</code> : passage à l'itération suivante</li>
</ul>
<pre>for (int i = 0, j = 0; i < 100; i++) {
if (i > tab.length) {
break; // Sortie de la boucle
}
if (tab[i] == null) {
continue; // Passe à l'itération suivante
}
tab2[j] = tab[i];
j++;
}</pre>
<h2>3. Éléments de programmation Java</h2>
<h3>3.1 Premiers pas</h3>
<div class="definition">
<p>Un programme Java est un ensemble de classes. L'exécution commence par
une <strong>classe exécutable</strong> contenant la méthode <code>public static
void main(String[] args)</code>.</p>
</div>
<h4>3.1.1 Classe HelloWorld</h4>
<pre>public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello world");
}
}</pre>
<div class="important">
<p>Chaque classe publique doit être implémentée dans un fichier séparé
ayant le même nom que la classe (ex: <code>HelloWorld.java</code>).</p>
</div>
<h4>3.1.2 Packages</h4>
<p>Les packages organisent les classes par thème :</p>
<table>
<tr>
<th>Package</th>
<th>Description</th>
</tr>
<tr>
<td>java.awt</td>
<td>Classes graphiques et interfaces</td>
</tr>
<tr>
<td>java.io</td>
<td>Gestion des entrées/sorties</td>
</tr>
<tr>
<td>java.lang</td>
<td>Classes de base (importé par défaut)</td>
</tr>
<tr>
<td>java.util</td>
<td>Classes utilitaires</td>
</tr>
<tr>
<td>javax.swing</td>
<td>Autres classes graphiques</td>
</tr>
</table>
<p><strong>Importation :</strong></p>
<pre>// Une classe spécifique
import java.util.Date;
// Toutes les classes d'un package
import java.util.*;</pre>
<p><strong>Création d'un package :</strong></p>
<pre>package fr.emse;
import java.util.Date;
public class DateMain {
// ...
}</pre>
<h3>3.2 Variables et méthodes</h3>
<h4>3.2.1 Visibilité des champs</h4>
<table>
<tr>
<th>Modificateur</th>
<th>Même classe</th>
<th>Même package</th>
<th>Sous-classe autre package</th>
<th>Autre classe</th>
</tr>
<tr>
<td><code>public</code></td>
<td>Oui</td>
<td>Oui</td>
<td>Oui</td>
<td>Oui</td>
</tr>
<tr>
<td><code>protected</code></td>
<td>Oui</td>
<td>Oui</td>
<td>Oui</td>
<td>Non</td>
</tr>
<tr>
<td>défaut (sans modificateur)</td>
<td>Oui</td>
<td>Oui</td>
<td>Non</td>
<td>Non</td>
</tr>
<tr>
<td><code>private</code></td>
<td>Oui</td>
<td>Non</td>
<td>Non</td>
<td>Non</td>
</tr>
</table>
<h4>3.2.2 Variables et méthodes de classe</h4>
<p>Le mot-clé <code>static</code> permet de définir des variables et méthodes
appartenant à la classe plutôt qu'aux objets :</p>
<ul>
<li>Variables de classe : valeur commune à tous les objets</li>
<li>Méthodes de classe : accessibles sans instancier d'objet</li>
</ul>
<pre>public final class Math {
public static final double PI = 3.14159265358979323846;
public static double toRadians(double angdeg) {
return angdeg / 180.0 * PI;
}
}
// Utilisation
System.out.println("pi = " + Math.PI);
System.out.println("90° = " + Math.toRadians(90));</pre>
<div class="important">
<p>Exemples importants de méthodes/variables static :</p>
<ul>
<li>La méthode <code>main</code> est une méthode de classe</li>
<li><code>System.out</code> est une variable de classe représentant la
sortie standard</li>
</ul>
</div>
</body>
</html>