introduction au langage de programmation java · 2016-04-16 · introduction au langage de...
TRANSCRIPT
![Page 1: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/1.jpg)
# 1
Introduction au langage deprogrammation Java
Christian Bac, Denis Conan
CSC 4002Octobre 2015
Revision : 1527
![Page 2: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/2.jpg)
Introduction au langage de programmation Java
# 2
Sommaire du cours
1 Introduction à Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Concepts de bases de Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 Classes et objets en Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364 Généralisation spécialisation en Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585 Organisation des sources Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 836 API Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 957 Exceptions en Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1308 Concepts objets avancés en Java. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .142
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 2/169
![Page 3: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/3.jpg)
Introduction au langage de programmation Java
# 3
1 Introduction à Java
1.1 Vous avez dit Java ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.2 Que recouvre le mot Java ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91.3 Caractéristiques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121.4 Historique de l’API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161.5 Environnements de développement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171.6 En savoir plus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 3/169
![Page 4: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/4.jpg)
Introduction au langage de programmation Java 1 Introduction à Java
# 4
1.1 Vous avez dit Java ?
■ À quoi cela sert-il ?
■ Pourquoi nous ?
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 4/169
![Page 5: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/5.jpg)
1 Introduction à Java 1.1 Vous avez dit Java ?
# 5
1.1.1 Android
■ Android : Java est le logiciel prépondérant des + 500 000 applications.
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 5/169
![Page 6: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/6.jpg)
1 Introduction à Java 1.1 Vous avez dit Java ?
# 6
1.1.2 CAS
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 6/169
![Page 7: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/7.jpg)
1 Introduction à Java 1.1 Vous avez dit Java ?
# 7
1.1.3 Java partout !
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 7/169
![Page 8: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/8.jpg)
1 Introduction à Java 1.1 Vous avez dit Java ?
# 8
1.1.4 Pourquoi nous ?
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 8/169
![Page 9: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/9.jpg)
Introduction au langage de programmation Java 1 Introduction à Java
# 9
1.2 Que recouvre le mot Java ?
■ Un projet et des évolutions sur + de 20 ans
■ Un langage objet
■ Une architecture basée sur une machine virtuelle
■ Des interfaces de programmation objet
■ Des outils (Java Development Kit)
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 9/169
![Page 10: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/10.jpg)
1 Introduction à Java 1.2 Que recouvre le mot Java ?
# 10
1.2.1 Héros
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 10/169
![Page 11: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/11.jpg)
1 Introduction à Java 1.2 Que recouvre le mot Java ?
# 11
1.2.2 Projet et Historique
■ 1990 : équipe autour de James Gosling et sous l’impulsion de Bill Joy
■ 1993 : projet de langage pour l’électronique grand public
■ 1995 : Java Development Kit (JDK) 1.0 b1 + support dans le navigateur Netscape
■ 1998 : mise en place du Java Community Process
■ 1999 : Java 2 Standard Edition (J2SE) 1.2 (Java DataBase Connector amélioré,interface graphique Swing, collections)
■ 2005 : J2SE 1.5, alors renommé J2SE 5 (types paramétrés, annotations, collectionsrevues)
■ 2006-2007 : J2SE6 et libération du code passage sous licence General PublicLicense, alias GNU/GPL
■ 2009 : rachat de SUN microsystems par ORACLE
■ 2011 : J2SE 7
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 11/169
![Page 12: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/12.jpg)
Introduction au langage de programmation Java 1 Introduction à Java
# 12
1.3 Caractéristiques
■ Objet
■ Exécuté par une machine virtuelle
■ Permettant d’écrire des programmes robustes
■ Simple : C comme syntaxe de base
■ Sûr, multi-activité, réparti
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 12/169
![Page 13: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/13.jpg)
1 Introduction à Java 1.3 Caractéristiques
# 13
1.3.1 Java un langage Objet
■ Tout est classe sauf les types primitifs (int, float, double, etc.)
■ Tout objet est manipulé à travers une référence
■ La spécialisation est simple entre les classes
■ Toutes les classes dérivent de java.lang.Object
■ Les tableaux sont construits à partir d’une classe particulière
■ L’API (en anglais, Application Programming Interface) est un ensemble de classes
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 13/169
![Page 14: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/14.jpg)
1 Introduction à Java 1.3 Caractéristiques
# 14
1.3.2 Machine Virtuelle Java
■ Les compilateurs génèrent du code intermédiaire (en anglais, bytecode)
■ Ce code intermédiaire est interprété par les « Java Virtual Machines » (JVM)
■ Modèle « compile once execute everywhere »
■ Java Virtual Machine (JVM) :♦ s’exécutent sur les systèmes d’exploitation (par exemple avec la commande java)♦ ou sont intégrées dans les navigateurs Web
■ Sémantique du langage plus stricte que celle du langage C
■ Taille et domaine de valeur des types primitifs identiques sur toutes les plate-formes
■ Code source Unicode (accents et autres glyphes)
■ Bibliothèques standards riches
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 14/169
![Page 15: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/15.jpg)
1 Introduction à Java 1.3 Caractéristiques
# 15
1.3.3 Robustesse
■ Conçu pour du logiciel embarqué
■ Gestion de la mémoire avec ramasse miettes (en anglais, garbage collector)
■ Pas d’accès à la mémoire physique
■ Gestion des erreurs par exception (voir section )
■ Contrôle des types à l’exécution
■ Compilateurs stricts et prévenants par exemple :♦ Avertissement lorsqu’une variable est affectée et non utilisée♦ Erreur à l’utilisation de variables non initialisées♦ Erreur lorsque les exceptions ne sont pas gérées
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 15/169
![Page 16: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/16.jpg)
Introduction au langage de programmation Java 1 Introduction à Java
# 16
1.4 Historique de l’API
■ Années de réalisation, et nombre de classes et d’interfaces des versions de Java♦ Java SE 7 2011 : 3 977♦ Java SE 6 2007 : 3 777♦ J2SE 5.0 2004 : 3 270♦ J2SE 1.4 2002 : 2 723♦ J2SE 1.3 2000 : 1 840♦ J2SE 1.2 1998 : 1 524♦ JDK 1.1 1997 : 477♦ JDK 1.0 1996 : 211
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 16/169
![Page 17: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/17.jpg)
Introduction au langage de programmation Java 1 Introduction à Java
# 17
1.5 Environnements de développement
■ Java Development Kit : compilateur, JVM, Appletviewer...
■ Eclipse, NetBeans, Jbuilder, Visual J++...
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 17/169
![Page 18: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/18.jpg)
1 Introduction à Java 1.5 Environnements de développement
# 18
1.5.1 Java Standard Development Kit
■ javac : compilateur
■ java : JVM interpréteur de bytecode
■ javadoc : générateur de documentation
■ appletviewer : environnement pour applet
■ javah : générateur d’entêtes pour mélange avec code natif en C (JNI)
■ javap : désassembleur de code intermédiaire
■ jdb : dévermineur
■ javakey : générateur de clés pour signer le code
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 18/169
![Page 19: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/19.jpg)
Introduction au langage de programmation Java 1 Introduction à Java
# 19
1.6 En savoir plus
■ Java Series : http://docs.oracle.com/javase//
♦ The Java Tutorial, Campione, Walrath, 5th edition, 2013,♦ The Java Programming Language, Arnold, Gosling, Holmes, 5th edition, 2013,♦ The Java Language Specification, J. Gosling, B. Joy, G. Steele, G. Bracha, A.
Bukley, 4th edition, 2013,♦ The Java Virtual Machine Specification, T. Lindholm, F. Yellin, G. Bracha, A.
Bukley, 3rd edition, 2013
■ Livres♦ D. Flanagan, « Java in a Nutshell : A Desktop Quick Reference, 5th Edition »,
O’Reilly, 2005♦ D. Flanagan, « Java Examples in a nutshell : A Tutorial Companion to Java in a
Nutshell », 3rd Edition, 2004♦ P. Niemeyer and J. Knudsen, « Learning Java », O’Reilly, 2005♦ J. Bloch, « Effective Java, 2nd Edition », Addison Wesley, 2008
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 19/169
![Page 20: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/20.jpg)
1 Introduction à Java 1.6 En savoir plus
# 20
1.6.1 Sites Web
■ http://moodle.tem-tsp.eu/moodle/view.php?id=26
■ http://www-inf.it-sudparis.eu/COURS/CSC4002/
■ http://http://www.oracle.com/technetwork/java/index.html: OracleJava(tm) Technology Home Page
■ http://www.javaworld.com: Infos
■ http://www.developer.com/java: Infos Liens
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 20/169
![Page 21: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/21.jpg)
Introduction au langage de programmation Java 1 Introduction à Java
# 21
Questions sur les concepts Java
■ En Java tout est-il objet ?
■ La machine virtuelle Java :♦ garantit-elle l’uniformité des variables de type primitif ?♦ met-elle en œuvre la gestion mémoire et en particulier le ramasse miettes ?
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 21/169
![Page 22: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/22.jpg)
Introduction au langage de programmation Java
# 22
2 Concepts de bases de Java
2.1 Syntaxe de base . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232.2 Types Primitifs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .272.3 Tableaux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322.4 Méthodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332.5 Exemple de passage d’arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 22/169
![Page 23: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/23.jpg)
Introduction au langage de programmation Java 2 Concepts de bases de Java
# 23
2.1 Syntaxe de base
■ Syntaxe de base C
♦ Instructions de contrôle : if, switch, for, while, do while
♦ Opérateurs : tous les opérateurs du C par exemple, + - / * %,♦ Blocs, instructions simples : {}, « ; »
■ Quelques différences♦ final remplace const
♦ espaces de nommage plus nombreux : classe, fichier, groupe de classes♦ private remplace static
♦ static prend un sens associé au concept de classe
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 23/169
![Page 24: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/24.jpg)
2 Concepts de bases de Java 2.1 Syntaxe de base
# 24
2.1.1 Premier en C1 #i n c l u d e <s t d l i b . h>
#i n c l u d e <s t d i o . h>t yp ed e f enum { FAUX=0, VRAI=1 } Booleen ;
5 s t a t i c Booleen e s tP r em i e r ( i n t nombre ) {i n t borne=nombre /2 ;
i f ( nombre==1) r e t u r n FAUX;9 f o r ( i n t pred=2; pred<=borne ; pred++) {
i f ( nombre%pred==0) {r e t u r n FAUX;
}13 }
r e t u r n VRAI ;}
17 i n t main ( ) {i n t nb ;p r i n t f ( " S a i s i r ␣une␣ v a l e u r ␣>␣0␣ : ␣" ) ;s c a n f ( "%d" , &nb ) ;
21
i f ( nb<=0) {
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 24/169
![Page 25: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/25.jpg)
2 Concepts de bases de Java 2.1 Syntaxe de base
# 25
p r i n t f ( " E r r e u r ␣de␣ s a i s i e \n" ) ;r e t u r n EXIT_FAILURE ;
25 }p r i n t f ( " Le␣nombre␣%d␣" , nb ) ;i f ( e s tP r em i e r ( nb ) ) {
p r i n t f ( " e s t ␣ p r em i e r \n" ) ;29 } e l s e {
p r i n t f ( "n ’ e s t ␣ pas ␣ p r em i e r \n" ) ;}r e t u r n EXIT_SUCCESS ;
33 }
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 25/169
![Page 26: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/26.jpg)
2 Concepts de bases de Java 2.1 Syntaxe de base
# 26
2.1.2 Premier en Javapackage complements . commeenc ;
2 p u b l i c c l a s s Premie r {p r i v a t e s t a t i c boo lean e s tP r em i e r ( f i n a l i n t nb ) {
i f ( nb == 1) { r e t u r n f a l s e ; }f o r ( i n t i =2; i<= nb /2 ; i++) {
6 i f ( nb%i == 0) {r e t u r n f a l s e ;
}}
10 r e t u r n t r u e ;}p u b l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) {
i n t nombre = Conso l e . r e a d I n t ( " Ent r e z ␣un␣nombre␣ e n t i e r " ) ;14 System . out . p r i n t l n ( " Le␣nombre␣" + nombre + "␣ es t− i l ␣ p r em i e r ␣? " ) ;
System . out . p r i n t ( " Le␣nombre␣" + nombre ) ;i f ( e s tP r em i e r ( nombre ) ) {
System . out . p r i n t l n ( "␣ e s t ␣ p r em i e r " ) ;18 } e l s e {
System . out . p r i n t l n ( "␣n ’ e s t ␣ pas ␣ p r em i e r " ) ;}
}22 }
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 26/169
![Page 27: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/27.jpg)
Introduction au langage de programmation Java 2 Concepts de bases de Java
# 27
2.2 Types Primitifs
■ pas des classes♦ short(2 octets), int (4 octets), long (8 octets)♦ float (4 octets), double (8 octets)♦ boolean (true/false), byte (1 octet), char (2 octets)
■ Variables déclarées n’importe où dans un bloc
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 27/169
![Page 28: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/28.jpg)
2 Concepts de bases de Java 2.2 Types Primitifs
# 28
2.2.1 Conversions de type
■ Conversions sur les types primitifs :♦ Conversions sûres OK (d’un type vers un type plus complexe)♦ Conversions non sûres (d’un type vers un type plus pauvre) nécessitent un
transtypage (en anglais, cast) explicite
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 28/169
![Page 29: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/29.jpg)
2 Concepts de bases de Java 2.2 Types Primitifs
# 29
2.2.2 Exemple de conversions
p u b l i c c l a s s CastExampleF ixed {2 p u b l i c s t a t i c vo id main ( f i n a l S t r i n g [ ] a r g s ) {
i n t i = 2127272 ; // 0x002075A8l ong m;s h o r t s ;
6 byte b ;m = i ; // promot ion OK// E x p l i c i t c a s t neededi = ( i n t ) m;
10 s = ( s h o r t ) i ; // 0x75A8b = ( byte ) i ; // 0xA8System . out . p r i n t l n ( "m=␣" + m + " ; ␣ s=␣" + s + " ; ␣b=␣" + b ) ;
}14 }
Résultat de l’exécution :m= 2127272; s= 30120; b= -88;
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 29/169
![Page 30: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/30.jpg)
2 Concepts de bases de Java 2.2 Types Primitifs
# 30
2.2.3 Tableaux
■ Les tableaux sont des objets
■ Leur taille est fixe
■ Leur taille est allouée dynamiquement par l’utilisation du mot réservé new
■ L’accès aux entrées est identique à celle en langage C une fois l’allocation faite
■ Erreur d’accès en cas de dépassement de taille
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 30/169
![Page 31: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/31.jpg)
2 Concepts de bases de Java 2.2 Types Primitifs
# 31
2.2.4 Exemple avec un tableau
p u b l i c c l a s s ArrayExample {2 p u b l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) {
i n t [ ] a r r a yO f I n t ; // or i n t a r r a y O f I n t [ ] ; // d e c l a r a t i o n de l a v a r i a b l e a r r a y O f I n ta r r a yO f I n t = new i n t [ 4 2 ] ; // c r e a t i o n du t a b l e a u e t a s s o c i a t i o n a a r r a y O f I n ta r r a yO f I n t [ 0 ] = 3 ; // a f f e c t a t i o n d ’ un e l ement du t a b l e a u
6
System . out . p r i n t l n ( " Ar ray ␣ l e n g t h ␣"+ a r r a yO f I n t . l e n g t h ) ; // o b t e n t i o n de l a t a i l l e du t a b l e a u (42)System . out . p r i n t l n ( a r r a yO f I n t [ 4 2 ] ) ; // i m p o s s i b l e l e v e e d ’ un e x c e p t i o n
}10 }
Résultat de l’exécution :Array length 42Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 42at ArrayExample.main(ArrayExample.java:8)
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 31/169
![Page 32: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/32.jpg)
Introduction au langage de programmation Java 2 Concepts de bases de Java
# 32
2.3 Tableaux
■ Déclaration de la référence
int[] arrayOfInt; // or int arrayOfInt[];
■ Création avec association de taille
arrayOfInt = new int[42];
■ Taille
int t = arrayOfInt.length;
■ Compatibilité avec C :
int[] primes = {1, 2, 3, 5, 7, 7+4};
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 32/169
![Page 33: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/33.jpg)
Introduction au langage de programmation Java 2 Concepts de bases de Java
# 33
2.4 Méthodes
■ Nom donné aux opérations
■ Méthodes associées aux classes (pas de méthode sans classe)
■ Surcharge (en anglais, overloading) des méthodes possible
■ Méthode d’instance
■ Méthode de classe avec mot réservé static
■ Prototype de la méthode public static void main(final String [])
■ Passage d’arguments♦ Par valeur pour les types primitifs♦ Par référence pour les objets et les tableaux
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 33/169
![Page 34: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/34.jpg)
Introduction au langage de programmation Java 2 Concepts de bases de Java
# 34
2.5 Exemple de passage d’arguments
p u b l i c c l a s s EssParm {2 p r i v a t e s t a t i c vo id add ( i n t c , f i n a l i n t [ ] as ) {
c++;as [ 0 ] ++;System . out . p r i n t l n ( " add␣ : ␣c=" + c + " , ␣ as [0 ]= " + as [ 0 ] ) ;
6 }p u b l i c s t a t i c vo id main ( f i n a l S t r i n g a rgv [ ] ) {
i n t i = 0 ;i n t s [ ] = new i n t [ 1 0 ] ;
10 s [ 0 ] = 0 ;add ( i , s ) ;System . out . p r i n t l n ( "main␣ : ␣ i=" + i + " , ␣ s [0]= " + s [ 0 ] ) ;
}14 }
Résultat de l’exécution :
add : c=1, as[0]=1main : i=0, s[0]=1
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 34/169
![Page 35: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/35.jpg)
Introduction au langage de programmation Java 2 Concepts de bases de Java
# 35
Questions sur la syntaxe de base de Java
■ En Java un fichier source peut-il contenir des méthodes ?
■ Dans un bloc d’instructions (partie entre accolades) :♦ la ligne suivante est-elle acceptable en C et en Java ?
int a = 0, b = 1, c;
♦ la ligne suivante est-elle acceptable en C et en Java ?if ( a = b ) {c = a;}
■ L’accès à un élément de tableau est-il réalisé de manière identique en C et en Java ?
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 35/169
![Page 36: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/36.jpg)
Introduction au langage de programmation Java
# 36
3 Classes et objets en Java
3.1 Classe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373.2 Objet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413.3 Attributs et méthodes de classe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493.4 Association entre classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .53
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 36/169
![Page 37: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/37.jpg)
Introduction au langage de programmation Java 3 Classes et objets en Java
# 37
3.1 Classe
■ Classe Personne en UML
Opérations d’instance
de la classe
Nom de la classe
Personne
nom : Stringprénom : StringnbParticipations : Integer = 0nbOrganisations : Integer = 0
Personne Personne
Attributs d’instance
voter(Bulletin b)consulterResultat(Scrutin s)seRetirerDUnScrutin(Scrutin s)
de la classe
Personne
Constructeur(String n, String p)
voter(Bulletin)consulterResultat(Scrutin)seRetirerDUnScrutin(Scrutin)
nom : Stringprénom : StringnbParticipations : IntegernbOrganisations : Integer
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 37/169
![Page 38: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/38.jpg)
3 Classes et objets en Java 3.1 Classe
# 38
3.1.1 Classe Personne en Java
package s t ud s ;2 p u b l i c c l a s s Personne {
p r i v a t e S t r i n g nom , prenom ;p r i v a t e i n t n bP a r t i c i p a t i o n s = 0 , nbOrgan i s a t i o n s = 0 ;p u b l i c Personne ( f i n a l S t r i n g n , f i n a l S t r i n g p ) {
6 nom = n ; prenom = p ;}p u b l i c vo id v o t e r ( f i n a l Bu l l e t i n b ){ }p u b l i c vo id c o n s u l t e r R e s u l t a t ( f i n a l S c r u t i n s ) { }
10 p u b l i c vo id s eRe t i r e rDUnSc r u t i n ( f i n a l S c r u t i n s ) { }@Over r idep u b l i c S t r i n g t o S t r i n g ( ){
r e t u r n " Personne ␣" + nom + " , ␣"+ prenom +" , ␣"+ nbOrgan i s a t i o n s + " , ␣" + n bP a r t i c i p a t i o n s ;14 }
}
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 38/169
![Page 39: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/39.jpg)
3 Classes et objets en Java 3.1 Classe
# 39
3.1.2 Instance de classe en UML
Identifiant
de l’objet
Nom
de la classe
Type
de l’attribut
Valeur
de l’attribut
nom : Stringprénom : StringnbParticipations : Integer = 0
Personne
nom = "Dupont"prénom = "Julien"nbParticipations = 7nbOrganisations = 3
j:Personne
Constructeur(String n,String p)
seRetirerDUnScrutin(Scrutin s)
nbOrganisations : Integer =0
voter(Bulletin b)consulterResultat(Scrutin s)
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 39/169
![Page 40: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/40.jpg)
3 Classes et objets en Java 3.1 Classe
# 40
3.1.3 Instance de classe en Java
1 import s t ud s . Personne ;c l a s s I n i t P e r s o n n e {
p u b l i c s t a t i c vo id main ( f i n a l S t r i n g [ ] a r g s ) {Personne j ; // r e f e r e n c e
5 j = new Personne ( "Dupont " , " J u l i e n " ) ; // i n s t a n c e c r e a t i o nSystem . out . p r i n t l n ( j ) ;
}}
Résultat de l’exécution :
studs.Personne@f62373
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 40/169
![Page 41: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/41.jpg)
Introduction au langage de programmation Java 3 Classes et objets en Java
# 41
3.2 Objet
■ Création d’un objet♦ Disposer d’une référence : Personne p;
♦ Créer l’objet : new + appel à un constructeur▶ p = new Personne("Dupont", "Julien");
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 41/169
![Page 42: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/42.jpg)
3 Classes et objets en Java 3.2 Objet
# 42
3.2.1 Constructeurs en Java
■ Méthode particulière :♦ Nom identique à la classe♦ Aucun type de retour♦ Appelé directement à travers new
■ Si aucun constructeur n’est spécifié, le compilateur en fabrique un par défaut quiinitialise les attributs à 0 (correspondant à leur type)
■ La surcharge des méthodes (overloading) permet à une classe d’avoir plusieursconstructeurs qui diffèrent par le nombre et le type de leurs arguments
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 42/169
![Page 43: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/43.jpg)
3 Classes et objets en Java 3.2 Objet
# 43
3.2.2 Exemples de constructeurspackage s t u d s c t r s ;import s t ud s . B u l l e t i n ;import s t ud s . S c r u t i n ;
4 p u b l i c c l a s s Personne {p r i v a t e S t r i n g nom , prenom ;p r i v a t e i n t n bP a r t i c i p a t i o n s = 0 , nbOrgan i s a t i o n s = 0 ;p u b l i c Personne ( f i n a l S t r i n g n , f i n a l S t r i n g p ) {
8 nom = n ; prenom = p ;}p u b l i c Personne ( f i n a l S t r i n g n , f i n a l S t r i n g p ,
f i n a l i n t nbp , f i n a l i n t nbo ) {12 nom = n ; prenom = p ;
n bP a r t i c i p a t i o n s = nbp ; nbOrgan i s a t i o n s = nbo ;}p u b l i c vo id v o t e r ( f i n a l Bu l l e t i n b ) { }
16 p u b l i c vo id c o n s u l t e r R e s u l t a t ( f i n a l S c r u t i n s ) { }p u b l i c vo id s eRe t i r e rDUnSc r u t i n ( f i n a l S c r u t i n s ) { }
}
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 43/169
![Page 44: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/44.jpg)
3 Classes et objets en Java 3.2 Objet
# 44
3.2.3 this
■ this permet de référencer l’instance courante ;
■ Est associé au concept d’auto-référence de l’objet
■ this permet de décrire un attribut ou une méthode de l’instance courante sansambiguïté
■ this(...) en tant que méthode fait appel à un constructeur
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 44/169
![Page 45: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/45.jpg)
3 Classes et objets en Java 3.2 Objet
# 45
3.2.4 Exemples d’utilisation de this
package s t u d s t h i s ;2 import s t ud s . B u l l e t i n ;
import s t ud s . S c r u t i n ;p u b l i c c l a s s Personne {
p r i v a t e S t r i n g nom , prenom ;6 p r i v a t e i n t n bP a r t i c i p a t i o n s = 0 , nbOrgan i s a t i o n s = 0 ;
p u b l i c Personne ( f i n a l S t r i n g nom , f i n a l S t r i n g prenom ) {t h i s . nom = nom ; t h i s . prenom = prenom ;
10 }p u b l i c Personne ( f i n a l S t r i n g n , f i n a l S t r i n g p ,
f i n a l i n t nbp , f i n a l i n t nbo ) {t h i s (n , p ) ;
14 n bP a r t i c i p a t i o n s = nbp ; nbOrgan i s a t i o n s = nbo ;}vo id v o t e r ( f i n a l Bu l l e t i n b ){ }vo id c o n s u l t e r R e s u l t a t ( f i n a l S c r u t i n s ) { }
18 vo id s eRe t i r e rDUnSc r u t i n ( f i n a l S c r u t i n s ) { }}
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 45/169
![Page 46: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/46.jpg)
3 Classes et objets en Java 3.2 Objet
# 46
3.2.5 Destruction des objets
■ Pas de technique particulière pour détruire un objet
■ Objet détruit lorsqu’il n’est plus référencé
■ Utilisation d’un ramasse miettes
■ Le Garbage Collector détruit les objets non référencés
■ La destruction est asynchrone
■ Il n’y a pas de garantie sur la destruction (le programme peut se terminer avant quel’objet ne soit détruit)
■ Si la classe décrit une méthode appelée finalize, cette méthode est appelée avantla libération de la mémoire de l’objet
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 46/169
![Page 47: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/47.jpg)
3 Classes et objets en Java 3.2 Objet
# 47
3.2.6 Abstraction et encapsulation
■ Une classe correspond aux concepts d’abstraction et d’encapsulation
■ C’est une abstraction car elle présente une interface (ensemble de méthodes) quipermettent d’interagir avec les objets
■ Une classe encapsule des données (ensemble d’attributs)
■ Pour garantir sa robustesse et son indépendance du reste du code, une classe doitmasquer son implantation
■ En particulier, elle ne doit pas permettre la modification de ses attributs sanscontrôle
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 47/169
![Page 48: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/48.jpg)
3 Classes et objets en Java 3.2 Objet
# 48
3.2.7 Visibilité des méthodes
■ Les méthodes publiques (public) d’une classe définissent son interface publique
■ Les méthodes privées (private), ne sont accessibles que depuis l’intérieur de laclasse
■ Les méthodes protégées (protected), sont accessibles depuis l’intérieur de la classeet de l’intérieur de ses classes dérivées
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 48/169
![Page 49: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/49.jpg)
Introduction au langage de programmation Java 3 Classes et objets en Java
# 49
3.3 Attributs et méthodes de classe
Attribut
d’instance
Attribut
de classe
Opération
d’instance
Opération
de classe
Personne j:Personne
nom : Stringprénom : StringnbParticipations : IntegernbOrganisation : IntegernbTotalParticipations : Integer
Constructeur(String n, String p)
m:Personne
voter(Bulletin)consulterResultat()seRetirerDUnScrutin()getNbTotalParticipations()
nbOrganisation = 1nbParticipations = 5prénom = Marienom = Martin
nom = Dupontprénom = JuliennbParticipations = 7nbOrganisation = 3
nbTotalParticipations = 12
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 49/169
![Page 50: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/50.jpg)
3 Classes et objets en Java 3.3 Attributs et méthodes de classe
# 50
3.3.1 Attributs et méthodes de classe Java1 package s t u d s s t a t ;
p u b l i c c l a s s Personne {p r i v a t e S t r i n g nom , prenom ;p r i v a t e i n t n bP a r t i c i p a t i o n s = 0 , nbOrgan i s a t i o n s = 0 ;
5 p r i v a t e s t a t i c i n t n bTo t a l P a r t i c i p a t i o n s = 0 ;p u b l i c Personne ( f i n a l S t r i n g nom , f i n a l S t r i n g prenom ){
t h i s . nom = nom ; t h i s . prenom = prenom ;}
9 p u b l i c Personne ( f i n a l S t r i n g nom , f i n a l S t r i n g prenom ,f i n a l i n t nbp , f i n a l i n t nbo ) {
t h i s (nom , prenom ) ;n bP a r t i c i p a t i o n s = nbp ;
13 n bTo t a l P a r t i c i p a t i o n s += nbp ;nbOrgan i s a t i o n s = nbo ;
}p u b l i c s t a t i c i n t g e tNbTo t a l P a r t i c i p a t i o n s ( ) {
17 r e t u r n n bTo t a l P a r t i c i p a t i o n s ;}p u b l i c S t r i n g t o S t r i n g ( ) {
r e t u r n nom + "␣" + prenom + "␣nbp␣" + n bP a r t i c i p a t i o n s21 + "␣nbo␣" + nbOrgan i s a t i o n s + "␣nbt ␣"
+ nbTo t a l P a r t i c i p a t i o n s ;
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 50/169
![Page 51: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/51.jpg)
3 Classes et objets en Java 3.3 Attributs et méthodes de classe
# 51
}}
import s t u d s s t a t . Personne ;p u b l i c c l a s s Te s tS t a t i cPe r s onn e {
p u b l i c s t a t i c vo id main ( f i n a l S t r i n g [ ] a r g s ) {4 Personne j = new Personne ( "Dupont " , " J u l i e n " , 7 , 3 ) ;
Personne m = new Personne ( "Mart in " , "Mar ie " , 5 , 1 ) ;System . out . p r i n t l n ( j ) ;System . out . p r i n t l n (m) ;
8 }}
Affichage :
Dupont Julien nbp 7 nbo 3 nbt 12Martin Marie nbp 5 nbo 1 nbt 12
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 51/169
![Page 52: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/52.jpg)
3 Classes et objets en Java 3.3 Attributs et méthodes de classe
# 52
3.3.2 Attributs et méthodes de classe
■ Les attributs statiques sont partagés par toutes les instances de la classe
■ Les méthodes statiques ne peuvent accéder qu’aux attributs statiques
■ Il n’est pas nécessaire d’instancier la classe pour accéder à ses membres statiques
■ Les instances peuvent accéder aux attributs statiques
■ Les instances peuvent invoquer les méthodes statiques
■ Les méthodes statiques ne sont jamais associées à l’auto référence de l’objet (this)
■ Quelques exemples avec la classe java.lang.math :
pi = Math.PI; // static attribute ’PI’b = Math.sqrt(2.0);// method call of static ’sqrt’
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 52/169
![Page 53: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/53.jpg)
Introduction au langage de programmation Java 3 Classes et objets en Java
# 53
3.4 Association entre classes
Association Nom del’association
Sens delecture del’association
Scrutin
listeBDE:Scrutin
organise
organise
*Personne
m:Personne
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 53/169
![Page 54: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/54.jpg)
3 Classes et objets en Java 3.4 Association entre classes
# 54
3.4.1 Exemple d’associationpackage s t u d s o r g ;p u b l i c c l a s s Personne {
3 p r i v a t e S t r i n g nom , prenom ;p r i v a t e i n t n bP a r t i c i p a t i o n s = 0 , nbOrgan i s a t i o n s = 0 ;p r i v a t e S c r u t i n [ ] s c r u t i n sO r g a n i s e s ;p u b l i c Personne ( f i n a l S t r i n g nom , f i n a l S t r i n g prenom ) {
7 t h i s . nom = nom ; t h i s . prenom = prenom ;s c r u t i n sO r g a n i s e s = new S c r u t i n [ 1 0 ] ;
}p u b l i c S c r u t i n o r g a n i s e r S c r u t i n ( f i n a l S t r i n g nom){
11 S c r u t i n s = new S c r u t i n (nom ) ;s c r u t i n sO r g a n i s e s [ n bO rgan i s a t i o n s ] = s ;nbO rgan i s a t i o n s ++;r e t u r n s ;
15 }p u b l i c S t r i n g t o S t r i n g ( ) {
r e t u r n g e tC l a s s ( ) . getName ( ) + "␣"+nom+"␣"+prenom+"␣nbp␣"+ n bP a r t i c i p a t i o n s + "␣nbo␣" + nbOrgan i s a t i o n s ;
19 }}
package s t u d s o r g ;
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 54/169
![Page 55: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/55.jpg)
3 Classes et objets en Java 3.4 Association entre classes
# 55
p u b l i c c l a s s S c r u t i n {p r i v a t e S t r i n g nomScrut in ;
4 p u b l i c S c r u t i n ( f i n a l S t r i n g nom) {nomScrut in = nom ;
}/∗ @see j a v a . l a n g . Object#t o S t r i n g ( ) ∗/
8 @Over r idep u b l i c S t r i n g t o S t r i n g ( ) {
r e t u r n " S c r u t i n ␣ [ nomScrut in=" + nomScrut in + " ] " ;}
12
}
import s t ud s o r g . ∗ ;p u b l i c c l a s s I n i t S c r u t i n {
3 p u b l i c s t a t i c vo id main ( f i n a l S t r i n g [ ] a r g s ) {Personne p ; // r e f e r e n c ep = new Personne ( "Dupont " , " J u l i e n " ) ; // i n s t a n c e c r e a t i o nS c r u t i n bde = p . o r g a n i s e r S c r u t i n ( " E l e c t i o n ␣bde␣2010 " ) ;
7 System . out . p r i n t l n ( bde ) ;}
}
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 55/169
![Page 56: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/56.jpg)
3 Classes et objets en Java 3.4 Association entre classes
# 56
3.4.2 Association entre classes en Java
■ Mise en place à l’aide d’attributs
■ Dans l’une ou l’autre des classes selon la navigabilité de l’association
■ Mise en place au moment de la construction des instances
■ Multiplicité de l’association représentée à l’aide d’un tableau ou d’une collection
■ Difficulté lorsque l’association est bidirectionnelle, par quoi commencer ?♦ Utilisation d’une méthode qui permet d’affecter un attribut d’une classe en
dehors du constructeur♦ Passage de la référence d’un des objets dans l’appel au constructeur de l’autre
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 56/169
![Page 57: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/57.jpg)
Introduction au langage de programmation Java 3 Classes et objets en Java
# 57
Questions sur les classes et objet
■ Les attributs d’instance d’une classe servent-ils à représenter l’état des objets de laclasse ?
■ Les objets sont-ils créés par l’appel à un constructeur de la classe ?
■ Dans la section 50, est-il légal d’écrire :♦ ligne 12 this.nbParticipations au lieu de nbParticipations
♦ ligne 17 this.nbTotalParticipations au lieu de nbTotalParticipations
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 57/169
![Page 58: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/58.jpg)
Introduction au langage de programmation Java
# 58
4 Généralisation spécialisation en Java
4.1 Généralisation spécialisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594.2 Polymorphisme . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664.3 Redéfinition de méthodes dans les classes dérivées . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 694.4 Héritage, membres et visibilité . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714.5 Classes abstraites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734.6 Exemple de classe Abstraites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764.7 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 58/169
![Page 59: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/59.jpg)
Introduction au langage de programmation Java 4 Généralisation spécialisation en Java
# 59
4.1 Généralisation spécialisation
Personne
nomnuméroSécu
Vendeur
ancienneténomDuStand
Avocat
nombreAffairesadresseCabinet
Enseignant
nombreCoursspecialité
Permanent
numéroBureau
Vacataire
nombreVacations
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 59/169
![Page 60: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/60.jpg)
4 Généralisation spécialisation en Java 4.1 Généralisation spécialisation
# 60
4.1.1 Héritage : comment en Java
■ Une classe ne peut hériter que d’une autre classe (héritage simple)
■ Une classe hérite d’une autre par l’utilisation du mot réservé extends
■ Une classe pour laquelle aucune spécialisation n’est explicitée spécialiseimplicitement la classe java.lang.Object
■ L’opérateur instanceof permet de tester si une référence correspond à un objetd’une classe donnée
■ Le mot réservé final utilisé devant le mot clé class interdit toute spécialisation dela classe sur laquelle il est utilisé
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 60/169
![Page 61: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/61.jpg)
4 Généralisation spécialisation en Java 4.1 Généralisation spécialisation
# 61
4.1.2 Héritage et constructeur
■ Création d’un objet de classe dérivée =⇒ création de la partie de l’objetcorrespondant à la classe parente
■ Appel dans le constructeur de la classe dérivée d’un des constructeurs de la classeparente par utilisation du mot réservé super()
♦ En première ligne du constructeur de la classe enfant♦ Si aucun appel =⇒ appel au constructeur sans argument de la classe parente
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 61/169
![Page 62: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/62.jpg)
4 Généralisation spécialisation en Java 4.1 Généralisation spécialisation
# 62
4.1.3 Exemple de classe parente et classe dérivée
package pe r s onne s ;p u b l i c c l a s s Personne {
3 p r i v a t e S t r i n g nom , prenom , numSecu ;p u b l i c Personne ( f i n a l S t r i n g n , f i n a l S t r i n g p , f i n a l S t r i n g ns ){
nom = n ; prenom = p ; numSecu = ns ;}
7 p u b l i c S t r i n g t o S t r i n g ( ){r e t u r n " Personne : ␣" + nom + "␣" + prenom + "␣" + numSecu ;
}}
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 62/169
![Page 63: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/63.jpg)
4 Généralisation spécialisation en Java 4.1 Généralisation spécialisation
# 63
package pe r s onne s ;2 p u b l i c c l a s s Ense i gnan t extends Personne {
p r i v a t e i n t nbCours ; p r i v a t e S t r i n g s p e c i a l i t e ;p u b l i c Ense i gnan t ( f i n a l S t r i n g n , f i n a l S t r i n g p , f i n a l S t r i n g ns ,
f i n a l i n t nbc , f i n a l S t r i n g s p e c i a l i t e ) {6 super (n , p , ns ) ;
nbCours = nbc ;t h i s . s p e c i a l i t e = s p e c i a l i t e ;
}10 p u b l i c S t r i n g t o S t r i n g ( ){
r e t u r n " Ense i gnan t : ␣" + nbCours + "␣" +s p e c i a l i t e ;}
}
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 63/169
![Page 64: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/64.jpg)
4 Généralisation spécialisation en Java 4.1 Généralisation spécialisation
# 64
4.1.4 Utilisation de classe parente et dérivée
import pe r s onne s . ∗ ;p u b l i c c l a s s I n h e r i t E x amp l e {
3 p u b l i c s t a t i c vo id main ( f i n a l S t r i n g [ ] a r g s ) {Personne p = new Personne ( "Dupont " , " J u l i e n " ,
" 1 8 3 1 2 9 1 . . . " ) ;En se i gnan t e = new Ense i gnan t ( "Durand " , " Em i l i e " ,
7 " 2 7 8 0 6 3 3 . . . " , 3 , " Cloud␣Computing " ) ;System . out . p r i n t l n ( "p␣ e s t ␣une␣Personne ␣"
+ (p i n s t a n c e o f Personne ) ) ;System . out . p r i n t l n ( "p␣ e s t ␣un␣ Ense i gnan t ␣"
11 + (p i n s t a n c e o f Ense i gnan t ) ) ;
System . out . p r i n t l n ( " e␣ e s t ␣une␣Personne ␣"+ ( e i n s t a n c e o f Personne ) ) ;
15 System . out . p r i n t l n ( " e␣ e s t ␣un␣ Ense i gnan t ␣"+ ( e i n s t a n c e o f Ense i gnan t ) ) ;
}}
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 64/169
![Page 65: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/65.jpg)
4 Généralisation spécialisation en Java 4.1 Généralisation spécialisation
# 65
Résultat de l’exécution :p est une Personne truep est une Enseignant falsee est une Personne truee est une Enseignant true
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 65/169
![Page 66: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/66.jpg)
Introduction au langage de programmation Java 4 Généralisation spécialisation en Java
# 66
4.2 Polymorphisme
■ Affectation de références dans un arbre d’héritage
■ Upcast : une référence d’une classe B peut référencer toute instance de la classe Bou de ses classes dérivées
■ Downcast : une référence d’une classe D dérivée d’une classe B peut recevoir uneréférence de la classe B si et seulement si :♦ L’objet référencé est bien du type D ou d’un de ses types dérivés♦ Le changement de type doit être explicite (en anglais, cast) vers le nouveau type
(D)
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 66/169
![Page 67: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/67.jpg)
4 Généralisation spécialisation en Java 4.2 Polymorphisme
# 67
4.2.1 Exemple de Upcast et Downcastimport pe r s onne s . Personne ;
2 import pe r s onne s . Ense i gnan t ;p u b l i c c l a s s UpDownExample {
p u b l i c s t a t i c vo id main ( f i n a l S t r i n g [ ] a r g s ) {Personne p ;
6 Ense i gnan t e = new Ense i gnan t ( "Durand " , " Em i l i e " ," 2 7 8 0 6 3 3 . . . " , 3 , " Cloud␣Computing " ) ;
p = e ; // UpCastSystem . out . p r i n t l n ( " Apres ␣p␣=␣e ; ␣p␣ e s t ␣une␣Personne ␣"
10 + (p i n s t a n c e o f Personne )+ " ; ␣p␣ e s t ␣un␣ Ense i gnan t ␣"+ (p i n s t a n c e o f Ense i gnan t ) ) ;
e = ( Ense i gnan t ) p ; // Downcast14 System . out . p r i n t l n ( " Apres ␣e␣=␣p ; ␣e␣ e s t ␣une␣Personne ␣"
+ ( e i n s t a n c e o f Personne )+ " ; ␣e␣ e s t ␣un␣ Ense i gnan t ␣"+ ( e i n s t a n c e o f Ense i gnan t ) ) ;
18 }}
Apres p = e; p est une Personne trueApres p = e; p est un Enseignant true
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 67/169
![Page 68: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/68.jpg)
4 Généralisation spécialisation en Java 4.2 Polymorphisme
# 68
Apres e=p; e est une Personne trueApres e=p; e est un Enseignant true
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 68/169
![Page 69: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/69.jpg)
Introduction au langage de programmation Java 4 Généralisation spécialisation en Java
# 69
4.3 Redéfinition de méthodes dans les classes dérivées
■ Redéfinition possible des méthodes publiques héritées (en anglais overriding)
■ Peut être annotée par @Override.
■ Liaison dynamique :♦ Redéfinition la plus proche dans l’arbre d’héritage invoquée♦ Même à travers une référence d’une classe parente♦ Accès explicite à une méthode redéfinie de la classe parente par super
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 69/169
![Page 70: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/70.jpg)
4 Généralisation spécialisation en Java 4.3 Redéfinition de méthodes dans les classes dérivées
# 70
4.3.1 Polymorphisme et liaison dynamique avec toString
1 import pe r s onne s . Personne ;import pe r s onne s . Ense i gnan t ;p u b l i c c l a s s TestPolyMorph {
p u b l i c s t a t i c vo id main ( f i n a l S t r i n g [ ] a r g s ) {5 Personne p = new Personne ( "Dupont " , " J u l i e n " ,
" 1 8 3 1 2 9 1 . . . " ) ;En se i gnan t e = new Ense i gnan t ( "Durand " , " Em i l i e " ,
" 2 7 8 0 6 3 3 . . . " , 3 , " Cloud␣Computing " ) ;9 System . out . p r i n t l n ( p . t o S t r i n g ( ) ) ;
System . out . p r i n t l n ( e . t o S t r i n g ( ) ) ;p = e ; // UpCastSystem . out . p r i n t l n ( p . t o S t r i n g ( ) ) ;
13 }}
Personne: Dupont JulienEnseignant: 3 Cloud ComputingEnseignant: 3 Cloud Computing
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 70/169
![Page 71: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/71.jpg)
Introduction au langage de programmation Java 4 Généralisation spécialisation en Java
# 71
4.4 Héritage, membres et visibilité
■ Une classe D héritant d’une classe B hérite de tous ses membres :♦ Les membres private de B sont inaccessibles depuis D
♦ Les membres protected de B sont accessibles depuis D
♦ Les membres public sont accessibles depuis D comme depuis les autres classes
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 71/169
![Page 72: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/72.jpg)
Introduction au langage de programmation Java 4 Généralisation spécialisation en Java
# 72
Questions généralisation/spécialisation en Java
■ À quoi sert le mot réservé extends dans une déclaration de classe ?
■ À quoi sert le mot super ?
■ Peut-on interdire la redéfinition d’une méthode dans une classe dérivée ?
■ Peut-on interdire la généralisation/spécialisation d’une classe ?
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 72/169
![Page 73: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/73.jpg)
Introduction au langage de programmation Java 4 Généralisation spécialisation en Java
# 73
4.5 Classes abstraites
En UML :
Nom de la classeen italique ou
utilisation du
stéréotype <<abstract>>
auteurtitredateCréation
transporter()dupliquer()afficher()
GraphiqueLivre Texte Vidéo
largeurhauteur
longueur durée
afficher() afficher() afficher()afficher()
Média<< abstract >>
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 73/169
![Page 74: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/74.jpg)
4 Généralisation spécialisation en Java 4.5 Classes abstraites
# 74
4.5.1 Classes abstraites : principes
■ Classe qui ne peut pas être instanciée. Par exemple la classe Média de la diapositiveprécédente
■ Spécialisation nécessaire jusqu’à obtenir des classes instanciables : par exemple, laclasse Livre dans la même diapositive
■ Impose un comportement commun à toutes les classes dérivées
■ Possibilité de créer des tableaux ou des collections de références de classe abstraite
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 74/169
![Page 75: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/75.jpg)
4 Généralisation spécialisation en Java 4.5 Classes abstraites
# 75
4.5.2 Classes abstraites en Java
■ Méthodes abstraites sans corps de méthode
■ Classe abstraite si méthode abstraite
■ Classe dérivée : définit toutes les méthodes ou reste abstraite
■ Classe qualifiée abstraite pour empêcher la création d’instances
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 75/169
![Page 76: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/76.jpg)
Introduction au langage de programmation Java 4 Généralisation spécialisation en Java
# 76
4.6 Exemple de classe Abstraites
nbTotalParticipationsnbTotalOrganisationsnbParticipationsnbOrganisationsprenomnom
nbTotalScrutin
Choix
/nbBulletinsPour/nbBulletinsContre*
ScrutinPréférences
nbTotal
intituléPréférencetexteCommentairePréf
Préférence
*
Personne
pourOuContre
Bulletin
*
* *
*
*
Studs
concerne
co
nc
ern
e
concerne
organise
*
*
intituléScrutintexteCommentaireScrutindateCréationdateDébutdateLimitedateLimiteExistence
Scrutin
ouvertureAvancéefermetureAvancéedestructionAvancée
nbChoix
PlageHoraire
dateheureDébutheureFin
*
ScrutinPlagesHoraires
nbTotal
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 76/169
![Page 77: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/77.jpg)
4 Généralisation spécialisation en Java 4.6 Exemple de classe Abstraites
# 77
4.6.1 Exemple : classes abstraitespackage s t u d s a b s t r a c t ;
2 p u b l i c a b s t r a c t c l a s s Choix {p r i v a t e Bu l l e t i n [ ] l e s B u l l e t i n s ;i n t nbBu l l e t i n sPou r , n bBu l l e t i n sCon t r e , i n d e x B u l l e t i n s ;p u b l i c Choix ( ) {
6 l e s B u l l e t i n s = new Bu l l e t i n [ 1 0 0 ] ;i n d e x B u l l e t i n s = 0 ;n bBu l l e t i n sPou r = 0 ;n bBu l l e t i n sC on t r e = 0 ;
10 }p u b l i c i n t a d dBu l l e t i n ( B u l l e t i n b ) {
l e s B u l l e t i n s [ i n d e x B u l l e t i n s++] = b ;i f ( b . ge tVa lue ( ) ) {
14 nbBu l l e t i n sPou r++ ;} e l s e {
nbBu l l e t i n sC on t r e++ ;}
18 r e t u r n i n d e x B u l l e t i n s ;}p u b l i c i n t ge tNbBu l l e t i n sPou r ( ) {
r e t u r n nbBu l l e t i n sPou r ;22 }
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 77/169
![Page 78: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/78.jpg)
4 Généralisation spécialisation en Java 4.6 Exemple de classe Abstraites
# 78
p u b l i c i n t g e tNbBu l l e t i n sCon t r e ( ){r e t u r n nbBu l l e t i n sC on t r e ;
}26 p u b l i c i n t cherche rRang ( B u l l e t i n b ){
f o r ( i n t i = 0 ; i < i n d e x B u l l e t i n s ; i ++){i f ( b . e qu a l s ( l e s B u l l e t i n s [ i ] ) ) r e t u r n i ;
}30 r e t u r n −1;
}p u b l i c vo id r e t i r e r B u l l e t i n ( f i n a l Bu l l e t i n r e t r a i t ) {
i n t rang = cherche rRang ( r e t r a i t ) ;34 i f ( rang > −1){
c ompa c t e rBu l l e t i n s ( rang ) ;}
}38 p u b l i c vo id c ompa c t e rBu l l e t i n s ( i n t r ){
f o r ( i n t i = r ; i < i n d e x B u l l e t i n s ; i ++){l e s B u l l e t i n s [ i ] = l e s B u l l e t i n s [ i +1] ;
}42 i n d e x B u l l e t i n s −−;
}p u b l i c vo id mo d i f i e r B u l l e t i n ( f i n a l Bu l l e t i n changed ) {
i n t rang = cherche rRang ( changed ) ;46 i f ( rang >=0) {
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 78/169
![Page 79: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/79.jpg)
4 Généralisation spécialisation en Java 4.6 Exemple de classe Abstraites
# 79
Bu l l e t i n aChanger = l e s B u l l e t i n s [ rang ] ;i f ( aChanger . ge tVa lue ( ) != changed . ge tVa lue ( ) ) {
aChanger . s e tVa l u e ( changed . ge tVa lue ( ) ) ;50 }
i f ( aChanger . ge tVa lue ( ) ) {nbBu l l e t i n sCon t r e−− ;n bBu l l e t i n sPou r++;
54 } e l s e {nbBu l l e t i n sC on t r e++ ;nbBu l l e t i n sPou r −−;
}58 }
}}
package s t u d s a b s t r a c t ;p u b l i c c l a s s Studs {
p r i v a t e Choix [ ] l e s C h o i x ;4 p r i v a t e S c r u t i n [ ] l e s S c r u t i n s ;
p r i v a t e Bu l l e t i n [ ] l e s B u l l e t i n s ;p r i v a t e Personne [ ] l e s P a r t i c i p a n t s ;p u b l i c Studs ( ) {
8 l e s C h o i x = new Choix [ 1 0 0 ] ;l e s S c r u t i n s = new S c r u t i n [ 1 0 0 ] ;
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 79/169
![Page 80: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/80.jpg)
4 Généralisation spécialisation en Java 4.6 Exemple de classe Abstraites
# 80
l e s B u l l e t i n s = new Bu l l e t i n [ 1 0 0 ] ;l e s P a r t i c i p a n t s = new Personne [ 1 0 0 ] ;
12 }@Over r idep u b l i c S t r i n g t o S t r i n g ( ) {
r e t u r n " Studs ␣" + l e sCh o i x + l e s S c r u t i n s + l e s B u l l e t i n s + l e s P a r t i c i p a n t s ;16 }
}
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 80/169
![Page 81: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/81.jpg)
Introduction au langage de programmation Java 4 Généralisation spécialisation en Java
# 81
4.7 Interfaces
UML :
saPlace()avancer()reculer()monter()descendre()
saPlace()avancer()reculer()monter()descendre()
PersonnePolygone
sommets nom
<<interface>>Déplaçable
numSécu
saPlace()
avancer()
reculer()
monter()
descendre()
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 81/169
![Page 82: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/82.jpg)
4 Généralisation spécialisation en Java 4.7 Interfaces
# 82
4.7.1 Interfaces en Java
■ Les membres d’une interface sont principalement des méthodes abstraites
■ Une interface est déclarée avec le mot réservé interface
■ Une interface peut hériter (extends) de plusieurs interfaces
■ Une classe peut implémenter (implements) une ou plusieurs interfaces
■ Rappel : une classe n’hérite (extends) que d’une seule classe
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 82/169
![Page 83: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/83.jpg)
Introduction au langage de programmation Java
# 83
5 Organisation des sources Java
5.1 Programme en C (rappel) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845.2 Exécution d’un programme (rappel) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855.3 Exécution d’un programme sur une machine virtuelle . . . . . . . . . . . . . . . . . . . . . . . . . . . 865.4 Dans le cas de Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 875.5 Unités de compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 885.6 Paquetages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 895.7 Visibilité en Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 83/169
![Page 84: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/84.jpg)
Introduction au langage de programmation Java 5 Organisation des sources Java
# 84
5.1 Programme en C (rappel)PROGRAMME
fichier2.c
(source)
(module)
fichier1.c
(source)
(module)
fichier3.c
(source)
(module)
fichier2.o
(objet)
(binaire)
(relogeable)
fichier3.o
(objet)
(binaire)
(relogeable)
fichier1.o
libc.a
(objet)
(binaire)
(relogeable)
a.out
(exécutable)
(binaire)
(chargeable)
Compilation Compilation Compilation
Edition de Liens
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 84/169
![Page 85: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/85.jpg)
Introduction au langage de programmation Java 5 Organisation des sources Java
# 85
5.2 Exécution d’un programme (rappel)
Operating System
Processus
CPU
Donnees
Texte
Libc.soTas
Pile
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 85/169
![Page 86: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/86.jpg)
Introduction au langage de programmation Java 5 Organisation des sources Java
# 86
5.3 Exécution d’un programme sur une machine virtuelle
Operating System
JVM
CPU
Donnees
fichiers .class
Texte
Libc.soTas
Pile
������
��������
��������
��������
������������
������������
������������
��������
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 86/169
![Page 87: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/87.jpg)
Introduction au langage de programmation Java 5 Organisation des sources Java
# 87
5.4 Dans le cas de Java
■ Point d’entrée public static void main(String args[]) dans une classe
■ Classes chargées à la demande (en anglais dynamic loading)
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 87/169
![Page 88: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/88.jpg)
Introduction au langage de programmation Java 5 Organisation des sources Java
# 88
5.5 Unités de compilation
■ Un fichier source Java = une unité de compilation
■ Recommandation : une seule classe par fichier source
■ Obligation : nom du fichier source = nom de sa classe publique
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 88/169
![Page 89: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/89.jpg)
Introduction au langage de programmation Java 5 Organisation des sources Java
# 89
5.6 Paquetages
■ Paquetages : regroupement de classes dans un espace de nommage
■ Noms des classes : « packagename.classname »
■ Espace de nommage associé à la compilation et à l’exécution♦ Classe Personne du paquetage studs doit être dans un fichier correspondant au
chemin « studs/Personne.java »
■ Ceci permet au compilateur et à la JVM de trouver les fichiers compilés
■ Mot réservé package : nom de paquetage des classes dans l’unité de compilation
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 89/169
![Page 90: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/90.jpg)
5 Organisation des sources Java 5.6 Paquetages
# 90
5.6.1 Chemin de recherche
■ variable d’environnement CLASSPATH liste des répertoires de recherche pour lecompilateur et la JVM
■ Noms des classes complets contient le nom de paquetage
■ import : permet d’établir un alias
■ API Java est organisée en paquetages (java.lang, java.io...)
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 90/169
![Page 91: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/91.jpg)
5 Organisation des sources Java 5.6 Paquetages
# 91
5.6.2 Exemple■ CLASSPATH : CLASSPATH=/src:/java■ Fichier /java/studs/Personne.java contient le code suivant :
package studs;public class Personne {
private String nom, prenom;private int nbParticipations = 0, nbOrganisations = 0;
public Personne(String n, String p){n = nom; prenom = p;
}public void voter(Bulletin b){ }public void consulterResultat(Scrutin s) { }public void seRetirerDUnScrutin(Scrutin s) { }
}
■ Fichier InitPersonne.java dans le répertoire /src peut contenir le code suivant :import studs.Personne;class InitPersonne {
public static void main(String[] args) {Personne j; // referencej = new Personne("Dupont", "Julien"); // instance creationSystem.out.println(j);
}}
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 91/169
![Page 92: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/92.jpg)
Introduction au langage de programmation Java 5 Organisation des sources Java
# 92
5.7 Visibilité en Java
■ Un membre de la classe C peut être :♦ private : accessible seulement à la classe♦ package friendly (défaut) : accessible aux classes dans le même paquetage♦ protected : accessible aux classed du paquetage et à toute classe dérivée en
dehors du paquetage♦ public : accessible à toutes les classes
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 92/169
![Page 93: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/93.jpg)
5 Organisation des sources Java 5.7 Visibilité en Java
# 93
5.7.1 Table de visibilité
package p2;
class C4 extends C1 {...
}
class C5 {...
}
package p1;
class C1 {public int a;
}class C2 extends C1 {
...}class C3 {
...}
protected int b;int c; // friendlyprivate int d;
a b c d
Accessible de C2 oui oui oui -
Accessible de C3 oui oui oui -
Accessible de C4 oui oui - -
Accessible de C5 oui - - -
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 93/169
![Page 94: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/94.jpg)
Introduction au langage de programmation Java 5 Organisation des sources Java
# 94
Questions organisation des sources en Java
■ Le nom d’un paquetage est-il lié à l’organisation des fichiers sources ?
■ Dans quel paquetage se trouve java.lang.Object ?
■ Les paquetages servent-ils seulement à la compilation des classes ?
■ Les paquetages impactent-ils la visibilité des membres des classes ?
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 94/169
![Page 95: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/95.jpg)
Introduction au langage de programmation Java
# 95
6 API Java
6.1 Premières classes de l’API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 966.2 Classe java.lang.Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 976.3 Interface de programmation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1016.4 java.lang.* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1036.5 java.util.*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .108
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 95/169
![Page 96: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/96.jpg)
Introduction au langage de programmation Java 6 API Java
# 96
6.1 Premières classes de l’API
■ java.lang.Object
■ java.lang.String
■ java.lang.Integer
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 96/169
![Page 97: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/97.jpg)
Introduction au langage de programmation Java 6 API Java
# 97
6.2 Classe java.lang.Object
■ public String toString()
■ protected Object clone() throws CloneNotSupportedException
■ public boolean equals(Object obj)
■ protected void finalize() throws Throwable
■ public final Class getClass()
■ public int hashCode()
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 97/169
![Page 98: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/98.jpg)
6 API Java 6.2 Classe java.lang.Object
# 98
6.2.1 Égalité
Les objets sont manipulés par des références ⇒
■ o1 == o2
♦ Teste si les références sont égales
■ o1.equals(o3)
♦ Teste si le contenu de l’objet référencé par o3 est comparable au contenu del’objet référencé par o1
obj1
o2
o1
o3
obj2
"texte" "texte"
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 98/169
![Page 99: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/99.jpg)
6 API Java 6.2 Classe java.lang.Object
# 99
6.2.2 Exemple d’égalité
p u b l i c c l a s s S t r i n gEqu a l s {p u b l i c s t a t i c vo id main ( f i n a l S t r i n g a rgv [ ] ) {
3 S t r i n g s = " Ja " , j = " Java " ;S t r i n g m = s + " va " ;System . out . p r i n t l n (m) ;i f ( j == m) { System . out . p r i n t l n ( " j ␣==␣m␣Vra i " ) ;
7 } e l s e { System . out . p r i n t l n ( " j ␣==␣m␣Faux " ) ;}i f ( j . e q u a l s (m) ) {
System . out . p r i n t l n ( " j . e q u a l s (m) ␣ Vra i " ) ;11 } e l s e {
System . out . p r i n t l n ( " j . e q u a l s (m) ␣Faux " ) ;}
}15 }
Traces d’exécution :Javaj == m Fauxj.equals(m) Vrai
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 99/169
![Page 100: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/100.jpg)
6 API Java 6.2 Classe java.lang.Object
# 100
6.2.3 Exemple de méthode equals1 package s t u d s e q u a l s ;
p u b l i c c l a s s Personne {p r i v a t e S t r i n g nom , prenom ;p u b l i c Personne ( f i n a l S t r i n g nom , f i n a l S t r i n g prenom ){
5 t h i s . nom = nom ; t h i s . prenom = prenom ;}@Over r idep u b l i c boo lean e qua l s ( Object ob j ) {
9 i f ( t h i s == ob j ) r e t u r n t ru e ;i f ( ob j == n u l l ) r e t u r n f a l s e ;i f ( ! ( ob j i n s t a n c e o f Personne ) ) r e t u r n f a l s e ;Personne o th e r = ( Personne ) ob j ;
13 i f (nom == n u l l ) {i f ( o t h e r . nom != n u l l ) r e t u r n f a l s e ;
} e l s e i f ( ! nom . e qua l s ( o t h e r . nom) ) r e t u r n f a l s e ;i f ( prenom == n u l l ) {
17 i f ( o t h e r . prenom != n u l l ) r e t u r n f a l s e ;} e l s e i f ( ! prenom . e qua l s ( o t h e r . prenom ) ) r e t u r n f a l s e ;r e t u r n t ru e ;
}21 }
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 100/169
![Page 101: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/101.jpg)
Introduction au langage de programmation Java 6 API Java
# 101
6.3 Interface de programmation
■ API Java organisée en paquetages
■ java.lang.* importé de manière automatique
■ Interface de programmation disponible quelle que soit la plate-forme
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 101/169
![Page 102: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/102.jpg)
6 API Java 6.3 Interface de programmation
# 102
6.3.1 Quelques paquetages Java
■ java.lang
■ java.util
■ java.io
■ java.net
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 102/169
![Page 103: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/103.jpg)
Introduction au langage de programmation Java 6 API Java
# 103
6.4 java.lang.*
<<Interface>>
Cloneable
Object
ThreadGroup
Boolean
Thread
Throwable
Void
Byte
Double
Long
Short
Float
Integer
String
<<Interface>>
Comparable
<<Interface>>
Iterable
<<Interface>>
Runnable
Boolean
Character
System
Class
Math
<<Abstract>>
StringBuffer
Runtime
Process<<Abstract>>
Number
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 103/169
![Page 104: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/104.jpg)
6 API Java 6.4 java.lang.*
# 104
6.4.1 Exemple avec la classe Integer
p u b l i c c l a s s I n t ege rExamp l e {p u b l i c s t a t i c vo id main ( f i n a l S t r i n g a rgv [ ] ) {
3 I n t e g e r i = new I n t e g e r ( " 4567 " ) ;i n t j = i . i n tVa l u e ( ) ;i = 4567 ; // box ing i = new I n t e g e r ( 4 5 6 7 ) ;j = i ; // unbox ing i . i n t V a l u e ( )
7 System . out . p r i n t l n ( " I n t e g e r ␣ i ␣ : " + i + "␣ i n t ␣ j ␣ : " + j + "\n" ) ;}
}
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 104/169
![Page 105: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/105.jpg)
6 API Java 6.4 java.lang.*
# 105
6.4.2 Classe String
■ Gestion des chaînes de caractères
■ Un objet de type String n’est pas modifiable
■ Les constantes littérales comme “abc” sont converties en String
■ L’opérateur « + » permet la concaténation des String
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 105/169
![Page 106: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/106.jpg)
6 API Java 6.4 java.lang.*
# 106
6.4.3 Exemple pour Stringimport j a v a . u t i l . Date ;p u b l i c c l a s s St r ingExamp le {
3 p u b l i c s t a t i c vo id main ( f i n a l S t r i n g a rgv [ ] ) {S t r i n g s = "\ u00ca t r e ␣ou␣ne␣ pas ␣\ u00ea t r e " ;i n t l g = s . l e n g t h ( ) ;System . out . p r i n t l n ( s + "␣" + l g ) ;
7 System . out . p r i n t l n ( " Java " . s u b s t r i n g ( 2 , 3 ) ) ;System . out . p r i n t l n ( " Java " + " So f t " ) ;char [ ] data = { ’ J ’ , ’ a ’ , ’ v ’ , ’ a ’ } ;S t r i n g n = new S t r i n g ( data ) ; System . out . p r i n t l n ( n ) ;
11 S t r i n g p = S t r i n g . va lueOf (Math . PI ) ; System . out . p r i n t l n ( p ) ;S t r i n g d = S t r i n g . va lueOf (new Date ( ) ) ; System . out . p r i n t l n ( d ) ;
}}
Résultat de l’exécution :Être ou ne pas être 19vJavaSoftJava3.141592653589793Wed Sep 22 12:25:04 CEST 2010Java
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 106/169
![Page 107: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/107.jpg)
Introduction au langage de programmation Java 6 API Java
# 107
Questions API java.lang
■ Pourquoi faire dériver toutes les classes de java.lang.Object ?
■ Pourquoi cette classe fournit-elle les méthodes suivantes :1. toString
2. equals
■ La classe integer est finale, à votre avis pourquoi ? (hors cours)
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 107/169
![Page 108: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/108.jpg)
Introduction au langage de programmation Java 6 API Java
# 108
6.5 java.util.*
Object
BitSet
Calendar
Locale
Currency
Date
<<Interface>>
Serializable
<<Interface>>
Cloneable
java.lang java.iojava.util
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 108/169
![Page 109: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/109.jpg)
6 API Java 6.5 java.util.*
# 109
6.5.1 Classe paramétrée
Opérationgénérique
Attributgénérique
Paramètrede type
élément : Etaille : IntegerpremierÉlément : EdernierÉlément : E
ajouter(E)estPrésent(E)retirer(E)
ListeDeChosesE
■ Comme en UML, classe associée à la classe des objets qu’elles servent à manipuler
■ Classe associée spécifiée entre « <> »
■ Exemple : Vector<String> myStringVector = new Vector<String>();
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 109/169
![Page 110: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/110.jpg)
6 API Java 6.5 java.util.*
# 110
6.5.2 Classes Abstraites des collections
<<Interface>>Enumeration<E>
<<Interface>>
Map<K,V>
<<Interface>>
List<E>
<<Interface>>
Set<E>
<<Abstract>>
AbstractMap<K,V>
<<Abstract>>
AbstractList<E> AbstractCollection<E><<Abstract>>
<<Abstract>>AbstractSet<E>
<<Abstract>>Dictionary<K,V>
<<Interface>>
Iterator<E>
java.lang.object
Collection<E><<Interface>>
<<Interface>>
java.lang.Iterable
java.util
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 110/169
![Page 111: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/111.jpg)
6 API Java 6.5 java.util.*
# 111
6.5.3 Classes instanciables des collections
<<Interface>>Enumeration<E>
<<Interface>>
Map<K,V>
<<Interface>>
List<E>
<<Interface>>
Set<E>
<<Abstract>>
AbstractMap<K,V>
<<Abstract>>
AbstractList<E> AbstractCollection<E><<Abstract>>
<<Abstract>>AbstractSet<E>
<<Abstract>>Dictionary<K,V>
<<Interface>>
Iterator<E>
HashMap<K,V>
Vector<E>
Hashtable<K,V>
Collection<E><<Interface>>
<<Interface>>
java.lang.Iterable
java.util
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 111/169
![Page 112: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/112.jpg)
6 API Java 6.5 java.util.*
# 112
6.5.4 Collections
■ Collection : interface commune à toutes les collections ;
■ List : interface permettant de gérer une liste ;
■ Iterator : interface permettant de parcourir une collection ;
■ Vector : réalisation de liste dans une sorte de tableau à taille variable ;
■ Map : interface permettant de gérer des tables associant une clé à un objet ;
■ Hashtable : tableau associatif
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 112/169
![Page 113: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/113.jpg)
6 API Java 6.5 java.util.*
# 113
6.5.5 Interface Iterable
■ provient de java.lang
■ Préconise : Iterator<T> iterator()
■ Iterator<> décrit 121
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 113/169
![Page 114: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/114.jpg)
6 API Java 6.5 java.util.*
# 114
6.5.6 Interface Collection<E>
Collection la plus générale
■ Insertion à la fin avec boolean add(E )
■ Suppression de tous les éléments avec void clear()
■ Test de la présence d’un objet dans la collection avec boolean contains(E)
■ Test permettant de savoir si la collection est vide avec boolean isEmpty()
■ Suppression du premier élément égal selon equals(E) au paramètre avec booleanremove(E)
■ Nombre d’éléments dans la collection avec int size().
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 114/169
![Page 115: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/115.jpg)
6 API Java 6.5 java.util.*
# 115
6.5.7 Interface List<E>
Collection ordonnée supportant les objets dupliqués
■ Accès à un élément avec E get(int index )
■ Insertion à une position donnée avec add(int, E)
■ Suppression de l’élément à une position donnée avec E remove(int))
■ Recherche de l’indice d’un objet s’il est dans le vecteur avec int indexOf(E)
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 115/169
![Page 116: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/116.jpg)
6 API Java 6.5 java.util.*
# 116
6.5.8 Classe Vector<E>
■ Gestion d’une collection d’éléments de la classe E dans un tableau dynamique
■ Pas de contrainte d’unicité sur les éléments
■ Dérive de la classe AbstractList
■ donc implémente l’interface List
■ donc implémente l’interface Collection
■ donc implémente aussi l’interface Iterable.
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 116/169
![Page 117: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/117.jpg)
6 API Java 6.5 java.util.*
# 117
6.5.9 Boucle pour les collections
■ Simplification de l’écriture des parcours de collections à partir de Java 5.
■ Utilisation du for avec une collection, un tableau
■ Syntaxe : for(Type t : collection)
■ Itère les objets de la collection dans la référence t
■ Type de collection et type de t compatibles
■ Ressemble aux foreach de php
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 117/169
![Page 118: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/118.jpg)
6 API Java 6.5 java.util.*
# 118
6.5.10 Exemple for-each sur un Vector
import j a v a . u t i l . L i s t ;2 import j a v a . u t i l . Vecto r ;
p u b l i c c l a s s VectorForExample {p u b l i c s t a t i c vo id main ( S t r i n g a rgv [ ] ) {
L i s t <St r i ng> v = new Vector<St r i ng >() ;6 v . add ( " John " ) ; v . add ( " Georges " ) ;
System . out . p r i n t l n ( " T a i l l e ␣de␣ l a ␣ l i s t e ␣" + v . s i z e ( ) ) ;f o r ( S t r i n g s : v ){
System . out . p r i n t l n ( "␣"+s ) ;10 }
}}
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 118/169
![Page 119: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/119.jpg)
6 API Java 6.5 java.util.*
# 119
6.5.11 Exemple de classe avec Vectorimport j a v a . u t i l . Vecto r ;import j a v a . u t i l . L i s t ;p u b l i c c l a s s VectorExampleB i s {
4 p u b l i c s t a t i c vo id main ( S t r i n g a rgv [ ] ) {L i s t <St r i ng> v=new Vector<St r i ng >() ;v . add ( "un" ) ; v . add ( " deux " ) ; v . add (2 , " t r o i s " ) ;System . out . p r i n t l n ( " Contenu␣du␣ v e c t e u r ␣ : " ) ;
8 f o r ( i n t i =0; i<v . s i z e ( ) ; i++) {System . out . p r i n t l n ( "␣␣" + v . ge t ( i ) ) ;
}i f ( v . remove ( "un" ) ) System . out . p r i n t l n ( "un␣ removed " ) ;
12 v . remove ( 0 ) ;System . out . p r i n t l n ( " Contenu␣du␣ v e c t e u r ␣ : " ) ;f o r ( i n t i =0; i<v . s i z e ( ) ; i++) {
System . out . p r i n t l n ( "␣␣" + v . ge t ( i ) ) ;16 }
}}
Trace de l’exécution :
Contenu du vecteur :
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 119/169
![Page 120: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/120.jpg)
6 API Java 6.5 java.util.*
# 120
undeuxtrois
un removedContenu du vecteur :
trois
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 120/169
![Page 121: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/121.jpg)
6 API Java 6.5 java.util.*
# 121
6.5.12 Interface Iterator
■ Il s’agit d’une interface qui fournit une technique pour parcourir les éléments d’unecollection
■ Iterator est spécialisé dans le parcours séquentiel des éléments d’une collection. Ilpropose les méthodes suivantes :♦ hasNext() retourne vrai si l’itérateur contient encore une élément♦ next() retourne l’élément suivant♦ remove() supprime le dernier élément retourné par next() de la collection
■ toutes les collections possèdent la méthode iterator()
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 121/169
![Page 122: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/122.jpg)
6 API Java 6.5 java.util.*
# 122
6.5.13 Exemple avec Iterator
import j a v a . u t i l . Vecto r ;2 import j a v a . u t i l . L i s t ;
import j a v a . u t i l . I t e r a t o r ;p u b l i c c l a s s V e c t o r I t e r a t o r {
p u b l i c s t a t i c vo id main ( S t r i n g a rgv [ ] ) {6 L i s t <St r i ng> v = new Vector<St r i ng >() ;
v . add ( " John " ) ; v . add ( " Georges " ) ;I t e r a t o r <St r i ng> e = v . i t e r a t o r ( ) ;w h i l e ( e . hasNext ( ) ) {
10 System . out . p r i n t l n ( "␣" + e . nex t ( ) ) ;}// Parcou r s avec une b o u c l e f o rf o r ( e = v . i t e r a t o r ( ) ; e . hasNext ( ) ; ) {
14 System . out . p r i n t l n ( "␣" + e . nex t ( ) ) ;}
}}
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 122/169
![Page 123: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/123.jpg)
6 API Java 6.5 java.util.*
# 123
6.5.14 Dictionnaires Map<K,V>
■ Les tables de correspondance permettent, à partir d’une clé, de retrouver une valeur
■ Cette interface est associée à deux paramètres qui correspondent à la classe de la cléet à celle de la valeur♦ C’est pourquoi sa définition est notée Map<K,V>
■ Par exemple, Map<String,String> myMap
■ Map supporte les méthodes supplémentaires suivantes, en plus de celles prescritespar Collection :♦ boolean containsKey(K key)
♦ boolean containsValue(V value)
♦ Set<K> keysSet()
♦ Collection<V> values()
♦ V get(K key)
♦ V put(K key, V value)
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 123/169
![Page 124: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/124.jpg)
6 API Java 6.5 java.util.*
# 124
6.5.15 Exemple pour Mapimport j a v a . u t i l . Hash tab l e ;import j a v a . u t i l .Map ;
3 p u b l i c c l a s s HashMapExample{p u b l i c s t a t i c vo id main ( f i n a l S t r i n g a rgv [ ] ) {
Map<St r i ng , S t r i ng> ht = new Hashtab le<St r i ng , S t r i ng >() ;ht . put ( "Ba" , "Bah" ) ; ht . put ( "Aa" , " aha " ) ;
7 ht . put ( "BB" , " bebe " ) ;f o r ( S t r i n g k : ht . keySet ( ) ) {
System . out . p r i n t l n ( " C le ␣ : ␣"+ k+ "␣ Va l eu r ␣ : ␣ " + ht . ge t ( k ) ) ;
11 }f o r ( S t r i n g v : ht . v a l u e s ( ) ) {
System . out . p r i n t l n ( "␣ Va l eu r ␣ : ␣" + v ) ;}
15 }}
Le résultat de l’exécution de l’exemple est le suivant :
Cle : Ba Valeur : BahCle : BB Valeur : bebeCle : Aa Valeur : aha
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 124/169
![Page 125: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/125.jpg)
6 API Java 6.5 java.util.*
# 125
Valeur : BahValeur : bebeValeur : aha
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 125/169
![Page 126: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/126.jpg)
6 API Java 6.5 java.util.*
# 126
6.5.16 Dictionnaire Hashtable<K,V>
■ Ces dictionnaires réalisent l’interface Map.
■ La classe associée à la clé doit supporter les méthodes hashCode() et equals()
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 126/169
![Page 127: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/127.jpg)
6 API Java 6.5 java.util.*
# 127
6.5.17 Exemple pour Hashtable
import j a v a . u t i l . Hash tab l e ;import j a v a . u t i l .Map ;p u b l i c c l a s s Hashtab leExample {
4 p u b l i c s t a t i c vo id main ( f i n a l S t r i n g a rgv [ ] ) {Map<St r i ng , S t r i ng> ht = new Hashtab le<St r i ng , S t r i ng >() ;ht . put ( "Ba" , "Bah" ) ; ht . put ( "Aa" , " aha " ) ;ht . put ( "BB" , " bebe " ) ;
8 f o r ( S t r i n g k : ht . keySet ( ) ) {System . out . p r i n t l n ( " C le ␣ : ␣"+ k + "␣Hash␣ : ␣"+ k . hashCode ( )
+ "␣ Va l eu r ␣ : ␣ " + ht . ge t ( k ) ) ;}
12 }}
Le résultat de l’exécution de l’exemple est le suivant :
Cle : Ba Hash : 2143 Valeur : BahCle : BB Hash : 2112 Valeur : bebeCle : Aa Hash : 2112 Valeur : aha
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 127/169
![Page 128: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/128.jpg)
6 API Java 6.5 java.util.*
# 128
6.5.18 Représentation d’une Hashtable
��������
��������
BebeAha
Bah21432112
BBAa Ba
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 128/169
![Page 129: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/129.jpg)
Introduction au langage de programmation Java 6 API Java
# 129
Questions collections en Java
■ Quelles sont les opérations préconisées par l’interface Collection ?
■ À quel usage est destiné un Iterator ?
■ Quelle est l’utilité du concept de Map ?
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 129/169
![Page 130: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/130.jpg)
Introduction au langage de programmation Java
# 130
7 Exceptions en Java
7.1 Motivation : retours sur un bug . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1317.2 Principes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1327.3 Réalisation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1357.4 Traitement des exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1377.5 Exemple de traitement d’exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 130/169
![Page 131: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/131.jpg)
Introduction au langage de programmation Java 7 Exceptions en Java
# 131 7.1 Motivation : retours sur un bug
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 131/169
![Page 132: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/132.jpg)
Introduction au langage de programmation Java 7 Exceptions en Java
# 132
7.2 Principes
■ Séparation entre flot de gestion des erreurs et flot d’exécution normale♦ Contrôle de flot spécifique pour les exceptions♦ Parties de code balisées relativement au traitement d’erreurs (régions gardées)♦ Parties de code spécifiques pour le traitement d’erreurs
■ Méthode constatant l’erreur et ne possédant pas suffisamment d’informations dansson contexte pour la traiter
■ Nécessité de remonter le chemin d’appel jusqu’à trouver une méthode possédantsuffisamment d’informations pour traiter cette erreur
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 132/169
![Page 133: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/133.jpg)
7 Exceptions en Java 7.2 Principes
# 133
7.2.1 Mise en œuvre
Les méthodes peuvent :
■ Lever une exception :♦ En prévenant les autres méthodes par l’ajout d’une clause throws dans leur
prototype♦ En créant un objet du type exception : classe qui dérive de
java.lang.Exception
♦ Et en utilisant le mot réservé « throw » pour provoquer la remontée du chemind’appel
■ Relayer des exceptions en ajoutant au prototype le mot clef « throws » et la listedes exceptions
■ Traiter des exceptions par des blocs try, catch, et finally
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 133/169
![Page 134: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/134.jpg)
7 Exceptions en Java 7.2 Principes
# 134
7.2.2 Exemple de traitementp u b l i c c l a s s Ar r ayCon t r o l e d {
s t a t i c f i n a l i n t max = 42 ;3 p u b l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) {
i n t i , j = 0 ;i n t [ ] a r r a yO f I n t = new i n t [ max ] ;f o r ( i =0; i<a r r a yO f I n t . l e n g t h ; i ++){
7 a r r a yO f I n t [ i ] = i ;}f o r ( i =0; i<a r g s . l e n g t h ; i ++){
t r y {11 j = I n t e g e r . p a r s e I n t ( a r g s [ i ] ) ;
System . out . p r i n t l n ( a r r a yO f I n t [ j ] ) ;} catch ( NumberFormatException n f e ){
System . out . p r i n t l n ( " Parameter ␣" + a rg s [ i ] + "␣ shou ld ␣be␣an␣ i n t e g e r " ) ;15 } catch ( Ar ray IndexOutOfBoundsExcept ion oobe ){
System . out . p r i n t l n ( " Parameter ␣" + a rg s [ i ]+ "␣ shou ld ␣be␣>=0␣and␣<" + max ) ;
}19 }
}}
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 134/169
![Page 135: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/135.jpg)
Introduction au langage de programmation Java 7 Exceptions en Java
# 135
7.3 Réalisation
■ Classes dérivées de java.lang.Exception
■ Lever une exception consiste à :♦ Créer une instance d’une classe dérivée de java.lang.Exception
♦ Déclencher le déroulement arrière des appels de méthodes qui ont menés à laméthode courante
♦ Arrêter le déroulement arrière à la rencontre d’un bloc try catch pour ce typed’exception
♦ S’il n’y a aucun bloc try catch l’exception remonte jusqu’à la machine virtuelleJava qui interrompt l’exécution
♦ Lorsqu’un bloc try catch correspondant à l’exception est trouvé :▶ Les instructions suivantes du bloc try sont sautées▶ Les instructions du bloc catch qui correspond le mieux sont exécutées
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 135/169
![Page 136: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/136.jpg)
7 Exceptions en Java 7.3 Réalisation
# 136
7.3.1 java.lang.Exception
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 136/169
![Page 137: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/137.jpg)
Introduction au langage de programmation Java 7 Exceptions en Java
# 137
7.4 Traitement des exceptions
Pour les exceptions sous contrôle :
■ Une méthode qui fait appel à une méthode qui peut lever une exception doit définirson comportement relativement à cette exception♦ Traiter l’exception par un bloc try catch
♦ Relayer l’exception par le mot réservé throws
■ Réorganisation du code de manière cohérente dans les blocs try.
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 137/169
![Page 138: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/138.jpg)
Introduction au langage de programmation Java 7 Exceptions en Java
# 138
7.5 Exemple de traitement d’exceptionsp u b l i c c l a s s NumException extends Excep t i on {
p r i v a t e s t a t i c f i n a l long s e r i a l V e r s i o nU ID = 1L ;3 p u b l i c NumException ( f i n a l double va l e u r , f i n a l S t r i n g s ) {
super ( s + "␣ v a l e u r ␣ : ␣" + v a l e u r ) ;}
}
p u b l i c c l a s s TestS imp leExcept {2 /∗∗
∗ P o s i t i f t e s t e s i l a v a l e u r du paramet re v a l e s t p o s i t i v e .∗ @param v a l v a l e u r a t e s t e r∗ @throws NumException s i v a l e s t < 0
6 ∗/p r i v a t e s t a t i c vo id p o s i t i f ( f i n a l double v a l ) throws NumException {
i f ( v a l < 0 . 0 ) throw new NumException ( va l , " Va l eu r ␣ p o s i t i v e ␣ a t t endue . " ) ;r e t u r n ;
10 }/∗∗
∗ t e s t P o s i t i f u t i l i s e p o s i t i f e t r e l a y e l ’ e x c e p t i o n NumException .∗ @param v a l v a l e u r a t e s t e r .
14 ∗ @throws NumException l o r s q u e p o s i t i f emet NumException∗/
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 138/169
![Page 139: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/139.jpg)
Introduction au langage de programmation Java 7 Exceptions en Java
# 139
p r i v a t e s t a t i c vo id t e s t P o s i t i f ( double v a l ) throws NumException {p o s i t i f ( v a l ) ;
18 System . out . p r i n t l n ( "Pas␣d ’ e x c e p t i o n ␣ l e ␣ code ␣ con t i nu e ␣en␣ sequence " ) ;r e t u r n ;
}/∗∗
22 ∗ Tra i t ement des arguments de main par t e s t des∗ v a l e u r s e n t i e r e s p o s i t i v e s .∗ @param argv arguments de l a l i g n e de commande .∗/
26 p u b l i c s t a t i c vo id main ( f i n a l S t r i n g [ ] a rgv ) {i n t i , j = 0 ;f o r ( i =0; i<argv . l e n g t h ; i++) {
t r y {30 j = I n t e g e r . p a r s e I n t ( a rgv [ i ] ) ;
t e s t P o s i t i f ( j ) ;} catch ( NumberFormatException n f e ){
System . out . p r i n t l n ( " Parameter ␣" + argv [ i ] + "␣ shou ld ␣be␣an␣ i n t e g e r " ) ;34 } catch ( NumException n ){
System . out . p r i n t l n ( " Catch␣ dans ␣ l e ␣main␣" + n ) ;}
}38 }
}
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 139/169
![Page 140: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/140.jpg)
Introduction au langage de programmation Java 7 Exceptions en Java
# 140Trace d’exécution avec arguments 2, -3 et 3 :Pas d’exception le code continue en sequenceCatch dans le main NumException: Valeur positive attendue. valeur : -3.0Pas d’exception le code continue en sequence
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 140/169
![Page 141: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/141.jpg)
Introduction au langage de programmation Java 7 Exceptions en Java
# 141
Questions sur les exceptions
■ À quoi servent :1. Le bloc try catch ?2. Le mot réservé new après la directive throw ?
■ Pourquoi créer une classe d’exception ?
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 141/169
![Page 142: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/142.jpg)
Introduction au langage de programmation Java
# 142
8 Concepts objets avancés en Java
8.1 Copie simple/légère . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1438.2 Retour sur hashCode() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1618.3 Retour sur les exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 142/169
![Page 143: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/143.jpg)
Introduction au langage de programmation Java 8 Concepts objets avancés en Java
# 143
8.1 Copie simple/légère
■ Les objets sont manipulés par des références ⇒♦ o2 = o1 : copie la référence o1 dans o2
o1
obj1
obj2
o2 o2
obj1
obj2
o1
"Paul"
"Jean" "Jean"
"Paul"
♦ o2 = o1.clone() : o2 référence un nouvel objet (copie de o1)
o1
o2 o2
o1
"Paul"
"Jean""Jean"
"Paul"
"Jean"
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 143/169
![Page 144: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/144.jpg)
8 Concepts objets avancés en Java 8.1 Copie simple/légère
# 144
8.1.1 Copie pour studs
nbTotalParticipationsnbTotalOrganisationsnbParticipationsnbOrganisationsprenomnom
nbTotalScrutin
Choix
/nbBulletinsPour/nbBulletinsContre*
ScrutinPréférences
nbTotal
intituléPréférencetexteCommentairePréf
Préférence
*
Personne
pourOuContre
Bulletin
*
* *
*
*
Studs
concerne
co
nc
ern
e
concerne
organise
*
*
PlageHoraire
dateheureDébutheureFin
*
ScrutinPlagesHoraires
nbTotal
intituléScrutintexteCommentaireScrutindateCréationdateDébutdateLimitedateLimiteExistence
Scrutin
ouvertureAvancéefermetureAvancéedestructionAvancée
/nbChoix
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 144/169
![Page 145: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/145.jpg)
8 Concepts objets avancés en Java 8.1 Copie simple/légère
# 145
8.1.2 Représentation graphique d’une copie légère dans studs
p:
:String
= "Julien"
:Personne
nomprénomscrutins
:String
= "Dupont"
p1:
:Personne
prénom
scrutins
nom
:Scrutin
organisateurnom
:String
= "Election bde"
:ArrayList
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 145/169
![Page 146: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/146.jpg)
8 Concepts objets avancés en Java 8.1 Copie simple/légère
# 146
8.1.3 Exemple de copie légère1 package s t u d s c l o n e l i g h t ;
import j a v a . u t i l . A r r a y L i s t ;import s t ud s . B u l l e t i n ;p u b l i c c l a s s Personne implements C loneab l e {
5 p r i v a t e S t r i n g nom , prenom ;p r i v a t e i n t n bP a r t i c i p a t i o n s = 0 , nbOrg = 0 ;p r i v a t e Ar r a yL i s t <Sc ru t i n > s c r u t i n s ;p u b l i c Personne ( S t r i n g nom , S t r i n g prenom ){
9 t h i s . nom = nom ; t h i s . prenom = prenom ; s c r u t i n s = new Ar r a yL i s t <Sc ru t i n >() ; }p u b l i c S c r u t i n o r g a n i s e r S c r u t i n ( S t r i n g nom) {
s c r u t i n s . add (new S c r u t i n (nom , t h i s ) ) ; r e t u r n s c r u t i n s . ge t ( nbOrg++); }@Over r ide
13 p u b l i c Personne c l o n e ( ) throws CloneNotSuppor tedExcept ion {r e t u r n ( Personne ) super . c l o n e ( ) ;
}p u b l i c S t r i n g getNom (){ r e t u r n nom;}
17 p u b l i c S t r i n g getPrenom (){ r e t u r n prenom ;}p u b l i c Ar r a yL i s t <Sc ru t i n > g e t S c r u t i n s ( ){ r e t u r n s c r u t i n s ; }p u b l i c vo id v o t e r ( B u l l e t i n b ){ }p u b l i c vo id c o n s u l t e r R e s u l t a t ( S c r u t i n s ) { }
21 p u b l i c vo id s eRe t i r e rDUnSc r u t i n ( S c r u t i n s ) { }}
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 146/169
![Page 147: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/147.jpg)
8 Concepts objets avancés en Java 8.1 Copie simple/légère
# 147
import j a v a . u t i l . A r r a y L i s t ;2 import s t u d s c l o n e l i g h t . S c r u t i n ;
import s t u d s c l o n e l i g h t . Personne ;p u b l i c c l a s s Tes tC l oneL i gh t {
p u b l i c s t a t i c vo id main ( f i n a l S t r i n g [ ] a r g s )6 throws CloneNotSuppor tedExcept ion {
Personne p = new Personne ( "Dupont " , " J u l i e n " ) ;p . o r g a n i s e r S c r u t i n ( " E l e c t i o n ␣bde " ) ;Personne p1 = p . c l o n e ( ) ;
10 i f ( p1 == p) {System . out . p r i n t l n ( "p␣==␣p1" ) ;
}i f ( p . getNom ( ) == p1 . getNom ( ) ) {
14 System . out . p r i n t l n ( "p␣ e t ␣p1␣noms␣==" ) ;}i f ( p . getPrenom ( ) == p1 . getPrenom ( ) ) {
System . out . p r i n t l n ( "p␣ e t ␣p1␣prenoms␣==" ) ;18 }
A r r a yL i s t <Sc ru t i n > a1 , a2 ;a1 = p . g e t S c r u t i n s ( ) ;a2 = p1 . g e t S c r u t i n s ( ) ;
22 i f ( a1 == a2 ) {System . out . p r i n t l n ( "p␣ e t ␣p1␣ s c r u t i n s ␣==" ) ;
} e l s e {
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 147/169
![Page 148: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/148.jpg)
8 Concepts objets avancés en Java 8.1 Copie simple/légère
# 148
f o r ( i n t i = 0 ; i < a1 . s i z e ( ) ; i ++){26 i f ( a1 . ge t ( i ) == a2 . ge t ( i ) ){
System . out . p r i n t l n ( " S c r u t i n ␣de␣ rang ␣" + i +"␣==" ) ;}
}30 }
}}
p et p1 noms ==p et p1 prenoms ==p et p1 scrutins ==
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 148/169
![Page 149: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/149.jpg)
8 Concepts objets avancés en Java 8.1 Copie simple/légère
# 149
8.1.4 Représentation graphique d’une copie plus profonde dansstuds
p:
:Personne
nomprénomscrutins
:String
= "Julien"
:String
= "Dupont"
:String
= "Julien"
:String
= "Dupont"
:Scrutin
organisateurnom
:String
= "Election bde"
:ArrayList :ArrayList
p1:
:Personne
prénom
scrutins
nom
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 149/169
![Page 150: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/150.jpg)
8 Concepts objets avancés en Java 8.1 Copie simple/légère
# 150
8.1.5 Copie plus profonde dans studspackage s t u d s c l o n ed e ep ;import j a v a . u t i l . A r r a y L i s t ;
4 import s t ud s . B u l l e t i n ;p u b l i c c l a s s Personne implements C loneab l e {
p r i v a t e S t r i n g nom , prenom ;p r i v a t e i n t n bP a r t i c i p a t i o n s = 0 , nbOrg = 0 ;
8 p r i v a t e Ar r a yL i s t <Sc ru t i n > s c r u t i n s ;p u b l i c Personne ( f i n a l S t r i n g n , f i n a l S t r i n g p ) {
t h i s . nom = n ;t h i s . prenom = p ;
12 s c r u t i n s = new Ar r a yL i s t <Sc ru t i n >() ;}p u b l i c S c r u t i n o r g a n i s e r S c r u t i n ( f i n a l S t r i n g nom) {
S c r u t i n s = new S c r u t i n (nom , t h i s ) ;16 s c r u t i n s . add ( s ) ;
nbOrg ++;r e t u r n s ;
}20 /∗∗ l e c l o n a g e e s t profond , i l d u p l i q u e l e s o b j e t s r e f e r e n c e s ∗/
@SuppressWarnings ( " unchecked " )@Over r ide
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 150/169
![Page 151: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/151.jpg)
8 Concepts objets avancés en Java 8.1 Copie simple/légère
# 151
p u b l i c Personne c l o n e ( ) throws CloneNotSuppor tedExcept ion {24 Personne c l o n e = new Personne (nom , prenom ) ;
c l o n e . n bP a r t i c i p a t i o n s = n bP a r t i c i p a t i o n s ;c l o n e . nbOrg = nbOrg ;c l o n e . s c r u t i n s = ( A r r a yL i s t <Sc ru t i n >) s c r u t i n s . c l o n e ( ) ;
28 r e t u r n c l o n e ;}p u b l i c S t r i n g getNom ( ) { r e t u r n nom ; }p u b l i c S t r i n g getPrenom ( ) { r e t u r n prenom ; }
32 p u b l i c Ar r a yL i s t <Sc ru t i n > g e t S c r u t i n s ( ) { r e t u r n s c r u t i n s ; }p u b l i c vo id v o t e r ( f i n a l Bu l l e t i n b ){ }p u b l i c vo id c o n s u l t e r R e s u l t a t ( S c r u t i n s ) { }p u b l i c vo id s eRe t i r e rDUnSc r u t i n ( S c r u t i n s ) { }
36 }
import j a v a . u t i l . A r r a y L i s t ;
import s t u d s c l o n ed e ep . S c r u t i n ;4 import s t u d s c l o n ed e ep . Personne ;
p u b l i c c l a s s TestCloneDeep {p u b l i c s t a t i c vo id main ( f i n a l S t r i n g [ ] a r g s )
throws CloneNotSuppor tedExcept ion {8 Personne p = new Personne ( "Dupont " , " J u l i e n " ) ;
p . o r g a n i s e r S c r u t i n ( " E l e c t i o n ␣bde " ) ;
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 151/169
![Page 152: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/152.jpg)
8 Concepts objets avancés en Java 8.1 Copie simple/légère
# 152
Personne p1 = p . c l o n e ( ) ;i f ( p1 == p) {
12 System . out . p r i n t l n ( "p␣==␣p1" ) ;}i f ( p . getNom ( ) == p1 . getNom ( ) ) {
System . out . p r i n t l n ( "p␣ e t ␣p1␣noms␣==" ) ;16 }
i f ( p . getPrenom ( ) == p1 . getPrenom ( ) ) {System . out . p r i n t l n ( "p␣ e t ␣p1␣prenoms␣==" ) ;
}20 Ar r a yL i s t <Sc ru t i n > a1 , a2 ;
a1 = p . g e t S c r u t i n s ( ) ;a2 = p1 . g e t S c r u t i n s ( ) ;i f ( a1 == a2 ) {
24 System . out . p r i n t l n ( "p␣ e t ␣p1␣ s c r u t i n s ␣==" ) ;} e l s e {
f o r ( i n t i = 0 ; i < a1 . s i z e ( ) ; i++) {i f ( a1 . ge t ( i ) == a2 . ge t ( i ) ) {
28 System . out . p r i n t l n ( " S c r u t i n s ␣de␣ rang ␣" + i +"␣==" ) ;} e l s e {
i f ( a1 . ge t ( i ) . g e tO r g an i s a t e u r ( ) == a2 . ge t ( i ) . g e tO r g an i s a t e u r ( ) ) {System . out . p r i n t l n ( " O r agan i s a t e u r ␣ des ␣ s c r u t i n s ␣de␣ rang " + i +"==" ) ;
32 }}
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 152/169
![Page 153: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/153.jpg)
8 Concepts objets avancés en Java 8.1 Copie simple/légère
# 153}
}36 }
}
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 153/169
![Page 154: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/154.jpg)
8 Concepts objets avancés en Java 8.1 Copie simple/légère
# 154
8.1.6 Représentation graphique d’une copie profondep:
:Personne
nomprénomscrutins
:String
= "Julien"
:String
= "Dupont"
:String
= "Julien"
:String
= "Dupont"
:ArrayList :ArrayList
:Scrutin
organisateurnom
:Scrutin
organisateurnom
:String
= "Election bde"
:String
= "Election bde"
p1:
:Personne
prénom
scrutins
nom
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 154/169
![Page 155: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/155.jpg)
8 Concepts objets avancés en Java 8.1 Copie simple/légère
# 155
8.1.7 Clone de Scrutinpackage s t u d s c l o n e ;p u b l i c c l a s s S c r u t i n implements C loneab l e {
3 p r i v a t e Personne o r g a n i s a t e u r ;p r i v a t e S t r i n g nomScrut in ;p u b l i c S c r u t i n ( f i n a l S t r i n g nom , f i n a l Personne o r g a n i s a t e u r ) {
nomScrut in = nom ;7 t h i s . o r g a n i s a t e u r = o r g a n i s a t e u r ;
}/∗∗ l e c l o n a g e d u p l i q u e l e nom mais ne peut pas m o d i f i e r l ’ o r g a n i s a t e u r .
∗ En e f f e t l e c l o n a g e de l ’ o r g a n i s a t i o n c l o n e l e s s c r u t i n s qu ’ i l a11 ∗ o r g a n i s à ľ e t nous e n t r e r i o n s dans une cascade d ’ a p p e l s i n f i n i e
∗∗/@Over r idep u b l i c S c r u t i n c l o n e ( ) throws CloneNotSuppor tedExcept ion {
15 r e t u r n new S c r u t i n (new S t r i n g ( nomScrut in ) , o r g a n i s a t e u r ) ;}/∗∗ Methode pour m o d i f i e r l ’ o r g a n i s a t e u r en cas de c l o n a g e . ∗/vo id s e tO r g a n i s a t e u r ( f i n a l Personne norg ) {
19 o r g a n i s a t e u r = norg ;}p u b l i c Personne g e tO r g an i s a t e u r ( ) {
r e t u r n o r g a n i s a t e u r ;
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 155/169
![Page 156: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/156.jpg)
8 Concepts objets avancés en Java 8.1 Copie simple/légère
# 156 23 }}
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 156/169
![Page 157: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/157.jpg)
8 Concepts objets avancés en Java 8.1 Copie simple/légère
# 157
8.1.8 Clone en copie profonde de Personnepackage s t u d s c l o n e ;import j a v a . u t i l . A r r a y L i s t ;import s t u d s a b s t r a c t . B u l l e t i n ;
4 p u b l i c c l a s s Personne implements C loneab l e {p r i v a t e S t r i n g nom , prenom ;p r i v a t e i n t n bP a r t i c i p a t i o n s = 0 , nbOrg = 0 ;p r i v a t e Ar r a yL i s t <Sc ru t i n > s c r u t i n s ;
8 p u b l i c Personne ( f i n a l S t r i n g n , f i n a l S t r i n g p ) {t h i s . nom = n ;t h i s . prenom = p ;s c r u t i n s = new Ar r a yL i s t <Sc ru t i n >() ;
12 }p u b l i c S c r u t i n o r g a n i s e r S c r u t i n ( f i n a l S t r i n g nom) {
S c r u t i n s = new S c r u t i n (nom , t h i s ) ;s c r u t i n s . add ( s ) ;
16 nbOrg ++;r e t u r n s ;
}/∗∗ l e c l o n a g e e s t profond , i l d u p l i q u e l e s o b j e t s r e f e r e n c e s .
20 ∗ Ce n ’ e s t pas n à ľ c e s s a i r e pour l e s o b j e t s du type S t r i n g∗ du f a i t qu ’ un S t r i n g e s t non m o d i f i a b l e .∗/
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 157/169
![Page 158: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/158.jpg)
8 Concepts objets avancés en Java 8.1 Copie simple/légère
# 158
@Over r ide24 p u b l i c Personne c l o n e ( ) throws CloneNotSuppor tedExcept ion {
Personne c l = new Personne (nom , prenom ) ;c l . n b P a r t i c i p a t i o n s = n bP a r t i c i p a t i o n s ;c l . nbOrg = nbOrg ;
28 f o r ( S c r u t i n s c r : s c r u t i n s ) {S c r u t i n s c l o n e = s c r . c l o n e ( ) ;s c l o n e . s e tO r g a n i s a t e u r ( c l ) ;c l . s c r u t i n s . add ( s c l o n e ) ;
32 }r e t u r n c l ;
}p u b l i c S t r i n g getNom ( ) { r e t u r n nom ; }
36 p u b l i c S t r i n g getPrenom ( ) { r e t u r n prenom ; }p u b l i c Ar r a yL i s t <Sc ru t i n > g e t S c r u t i n s ( ) { r e t u r n s c r u t i n s ; }p u b l i c vo id v o t e r ( f i n a l Bu l l e t i n b ){ }p u b l i c vo id c o n s u l t e r R e s u l t a t ( S c r u t i n s ) { }
40 p u b l i c vo id s eRe t i r e rDUnSc r u t i n ( S c r u t i n s ) { }}
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 158/169
![Page 159: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/159.jpg)
8 Concepts objets avancés en Java 8.1 Copie simple/légère
# 159
8.1.9 Suite exemple de copie profondeimport j a v a . u t i l . A r r a y L i s t ;
3 import s t u d s c l o n e . ∗ ;p u b l i c c l a s s TestC lone {
p u b l i c s t a t i c vo id main ( f i n a l S t r i n g [ ] a r g s )throws CloneNotSuppor tedExcept ion {
7 Personne p = new Personne ( "Dupont " , " J u l i e n " ) ;p . o r g a n i s e r S c r u t i n ( " E l e c t i o n ␣bde␣2010 " ) ;Personne p1 = p . c l o n e ( ) ;i f ( p1 == p) {
11 System . out . p r i n t l n ( "p␣==␣p1" ) ;}i f ( p . getNom ( ) == p1 . getNom ( ) ) {
System . out . p r i n t l n ( "p␣ e t ␣p1␣noms␣==" ) ;15 }
i f ( p . getPrenom ( ) == p1 . getPrenom ( ) ) {System . out . p r i n t l n ( "p␣ e t ␣p1␣prenoms␣==" ) ;
}19 Ar r a yL i s t <Sc ru t i n > a1 , a2 ;
a1 = p . g e t S c r u t i n s ( ) ;a2 = p1 . g e t S c r u t i n s ( ) ;i f ( a1 == a2 ) {
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 159/169
![Page 160: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/160.jpg)
8 Concepts objets avancés en Java 8.1 Copie simple/légère
# 160
23 System . out . p r i n t l n ( "p␣ e t ␣p1␣ s c r u t i n s ␣==" ) ;} e l s e {
f o r ( i n t i = 0 ; i < a1 . s i z e ( ) ; i++) {i f ( a1 . ge t ( i ) == a2 . ge t ( i ) ) {
27 System . out . p r i n t l n ( " S c r u t i n s ␣de␣ rang ␣" + i +"␣==" ) ;} e l s e {
i f ( a1 . ge t ( i ) . g e tO r g an i s a t e u r ( ) == a2 . ge t ( i ) . g e tO r g an i s a t e u r ( ) ) {System . out . p r i n t l n ( " O r agan i s a t e u r ␣ des ␣ s c r u t i n s ␣de␣ rang " + i +"==" ) ;
31 }}
}}
35 }}
Résultat de l’exécution :
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 160/169
![Page 161: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/161.jpg)
Introduction au langage de programmation Java 8 Concepts objets avancés en Java
# 161
8.2 Retour sur hashCode()
■ Méthode facilitant l’utilisation de Hashtable
■ Contrat de base :♦ Doit retourner la même valeur lorsqu’elle est appelée plusieurs fois sur le même
objet♦ Doit retourner la même valeur pour deux objets equals
♦ Peut retourner la même valeur pour deux objets différents (limites du hachage)
■ Algorithme de J. Bloch dans Effective Java
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 161/169
![Page 162: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/162.jpg)
8 Concepts objets avancés en Java 8.2 Retour sur hashCode()
# 162
8.2.1 Exemple hashCodepackage s t u d s e q u a l s ;p u b l i c c l a s s Personne {
p r i v a t e S t r i n g nom , prenom ;4 p u b l i c Personne ( f i n a l S t r i n g nom , f i n a l S t r i n g prenom ){
t h i s . nom = nom ; t h i s . prenom = prenom ;}@Over r ide
8 p u b l i c boo lean e qua l s ( Object ob j ) {i f ( t h i s == ob j ) r e t u r n t ru e ;i f ( ob j == n u l l ) r e t u r n f a l s e ;i f ( ! ( ob j i n s t a n c e o f Personne ) ) r e t u r n f a l s e ;
12 Personne o th e r = ( Personne ) ob j ;i f (nom == n u l l ) {
i f ( o t h e r . nom != n u l l ) r e t u r n f a l s e ;} e l s e i f ( ! nom . e qua l s ( o t h e r . nom) ) r e t u r n f a l s e ;
16 i f ( prenom == n u l l ) {i f ( o t h e r . prenom != n u l l ) r e t u r n f a l s e ;
} e l s e i f ( ! prenom . e qua l s ( o t h e r . prenom ) ) r e t u r n f a l s e ;r e t u r n t ru e ;
20 }}
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 162/169
![Page 163: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/163.jpg)
Introduction au langage de programmation Java 8 Concepts objets avancés en Java
# 163
8.3 Retour sur les exceptions
p u b l i c c l a s s NumException extends Excep t i on {p r i v a t e s t a t i c f i n a l long s e r i a l V e r s i o nU ID = 1L ;
3 p u b l i c NumException ( f i n a l double va l e u r , f i n a l S t r i n g s ) {super ( s + "␣ v a l e u r ␣ : ␣" + v a l e u r ) ;
}}
p u b l i c c l a s s Nu l l E x c e p t i o n extends NumException {2 p r i v a t e s t a t i c f i n a l long s e r i a l V e r s i o nU ID = 1L ;
p u b l i c Nu l l E x c ep t i o n ( ) {super (0 , " Va l eu r ␣ n u l l e ␣non␣ a t t endue " ) ;
}6 }
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 163/169
![Page 164: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/164.jpg)
8 Concepts objets avancés en Java 8.3 Retour sur les exceptions
# 164
8.3.1 Test des exceptionsp u b l i c c l a s s Te s tE x c e p t I nHe r i t {
2 /∗∗∗ Methode v e r i f i a n t qu ’ une v a l e u r e s t p o s i t i v e .∗ @param v a l v a l e u r a t e s t e r∗ @re tu rn v a l e u r d ’ e n t r e e
6 ∗ @throws NumException s i v a l e u r n e g a t i v e∗ @throws N u l l E x c e p t i o n s i v a l e u r e g a l e a z e r o .∗/
p r i v a t e s t a t i c double p o s i t i f O uNu l l ( f i n a l double v a l )10 throws NumException , Nu l l E x c ep t i o n {
i f ( v a l < 0 . 0 ) {throw new NumException ( va l , " P o s i t i f OuNu l l ␣ v a l e u r ␣ p o s i t i v e ␣ a t t endue . " ) ;
}14 i f ( v a l == 0 . 0 ) {
throw new Nu l l E x c e p t i o n ( ) ;}r e t u r n v a l ;
18 }/∗∗
∗ t r a i t e m e n t p a r t i e l d ’ e x c e p t i o n l e s e x c e p t i o n s∗ N u l l E x c e p t i o n son t t r a i t e e s , l e s e x c e p t i o n s NumException
22 ∗ son t t r a i t e e s p a r t i e l l e m e n t e t r e l a y e e s .
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 164/169
![Page 165: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/165.jpg)
8 Concepts objets avancés en Java 8.3 Retour sur les exceptions
# 165
∗ @param v a l v a l e u r a t e s t e r∗ @throws NumException∗/
26 p r i v a t e s t a t i c vo id t e s t P o s i t i f O uNu l l ( f i n a l double v a l )throws NumException {
double d=0;System . out . p r i n t l n ( "Dans␣ t e s t P o s i t i f O uNu l l ␣ v a l ␣=␣"+v a l ) ;
30 t r y {d = p o s i t i f O uNu l l ( v a l ) ;System . out . p r i n t l n ( "Pas␣d ’ e x c e p t i o n ␣ l e ␣ code ␣ con t i nu e ␣en␣ sequence " ) ;
} catch ( Nu l l E x c ep t i o n m) {34 System . out . p r i n t l n ( " Catch␣ Nu l l E x c e p t i o n ␣ dans ␣ t e s t P o s i t i f O uNu l l ␣ " + m) ;
throw m;} catch ( NumException m) {
System . out . p r i n t l n ( " Catch␣NumException␣ dans ␣ t e s t P o s i t i f O uNu l l ␣ "38 + " t r a i t emen t ␣ p a r t i e l ␣ e t ␣ r e l a i s " + m) ;
throw m;}f i n a l l y {
42 System . out . p r i n t l n ( "Dans␣ l e ␣ f i n a l l y ␣de␣ t e s t P o s i t i f O uNu l l ␣ " + d ) ;}
}p u b l i c s t a t i c vo id main ( f i n a l S t r i n g [ ] a r g s ) {
46 i n t j = 0 ;
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 165/169
![Page 166: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/166.jpg)
8 Concepts objets avancés en Java 8.3 Retour sur les exceptions
# 166
f o r ( S t r i n g oneArg : a r g s ){t r y {
j = I n t e g e r . p a r s e I n t ( oneArg ) ;50 t e s t P o s i t i f O uNu l l ( j ) ;
} catch ( NumberFormatException n f e ){System . out . p r i n t l n ( " Parameter ␣" + oneArg
+ "␣ shou ld ␣be␣an␣ i n t e g e r " ) ;54 } catch ( NumException n ){
System . out . p r i n t l n ( " Catch␣ dans ␣ l e ␣main␣" + n ) ;}
}58 }
}
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 166/169
![Page 167: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/167.jpg)
8 Concepts objets avancés en Java 8.3 Retour sur les exceptions
# 167
L’exécution de ce programme avec les arguments du main 2 0 -2 donne l’affichagesuivant :Dans testPositifOuNull val = 2.0Pas d’exception le code continue en sequenceDans le finally de testPositifOuNull 2.0Dans testPositifOuNull val = 0.0Catch NullException dans testPositifOuNull NullException:Valeur nulle non attendue valeur reçue : 0.0Dans le finally de testPositifOuNull 0.0Catch dans le main NullException: Valeur nulle non attendue valeur reçue : 0.0Dans testPositifOuNull val = -2.0Catch NumException dans testPositifOuNull traitement partiel et relaisNumException:PositifOuNull valeur positive attendue. valeur reçue : -2.0
Dans le finally de testPositifOuNull 0.0Catch dans le main NumException: PositifOuNull valeur positive attendue. valeur reçue : -2.0
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 167/169
![Page 168: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/168.jpg)
8 Concepts objets avancés en Java 8.3 Retour sur les exceptions
# 168
8.3.2 RuntimeException
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 168/169
![Page 169: Introduction au langage de programmation Java · 2016-04-16 · Introduction au langage de programmation Java 2 Concepts de bases de Java # 23 2.1 Syntaxe de base Syntaxe de base](https://reader034.vdocuments.site/reader034/viewer/2022042507/5f4365aff3881633dd027cfc/html5/thumbnails/169.jpg)
Introduction au langage de programmation Java
# 169
Bibliographie
[Armstrong, 2006] Armstrong, D. J. (2006). The quarks of object-orienteddevelopment. Communication of the ACM, 49(2) :123–128.
[Knuth, 1973] Knuth, D. E. (1973). The Art of Computer Programming, volume 3. :sorting and searching. Addison-Wesley Publishing Company, Inc.
[Maurer and Lewis, 1975] Maurer, W. D. and Lewis, T. G. (1975). Hash table methods.ACM Computing Survey, 7(1) :5–19.
Télécom SudParis — Christian Bac, Denis Conan — Octobre 2015 — CSC 4002 169/169