linguaggio java - uniroma2.it · software per tlc - v.1.1 3 java development kit • bin : contiene...
TRANSCRIPT
![Page 1: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/1.jpg)
Software per TLC - V.1.1 1
Linguaggio Java
• Robusto– Non permette costrutti “pericolosi”
• Eredità Multipla• Gestione della Memoria
• Orientato agli oggetti– Ogni cosa ha un tipo– Ogni tipo è un oggetto (quasi)
• Protegge e gestisce dagli errori– Verifica gli errori runtime– Gestisce esplicitamente gli errori
![Page 2: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/2.jpg)
Software per TLC - V.1.1 2
Caratteristiche Java - cont.
• Portabile– Astrae l’accesso alle risorse del SO
• File, network, etc.
– Astrae i tipi di dato• int, float, double
• Debug/Linking dinamico– Debug del bytecode– Il bytecode contiene le informazioni per il linking a
runtime
![Page 3: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/3.jpg)
Software per TLC - V.1.1 3
Java Development Kit
• bin: contiene i file eseguibili– javac: compilatore– java: avvia la JVM ed esegue i programmi– jdb: esecuzione in modalità debug– javadoc: genera la documentazione– …
• demo: esempi (anche complicati) per mostrare le potenzialità di Java
• include e lib: codice binario usato da JDK• jre: la Java Virtual Machine• docs: documentazione delle librerie java
![Page 4: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/4.jpg)
Software per TLC - V.1.1 4
Java Virtual Machine
• È un programma installato nel vostro PC• Crea l’ambiente esecutivo per i programmi
– Li intefaccia con la macchina
• Avviata con “java”• Non è legata al linguaggio Java
– Esegue bytecode
• Il bytecode può essere generato in diversi modi– Il principale e javac
![Page 5: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/5.jpg)
Software per TLC - V.1.1 5
JVM e bytecode
• Il programma compilato è “teoricamente” portabile– Non dipende dal SO– Mi serve solo la JVM
• JVM astrae i SO• La JVM non è portabile
– SO diversi hanno JVM diverse
• Tutte le JVM dovrebbero avere le stesse caratteristiche– Spesso non è vero– Ecco il perché del “teoricamente”
![Page 6: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/6.jpg)
Software per TLC - V.1.1 6
Linguaggio ed esecuzione
• Java – Linguaggio (è uno solo)
• Diverse piattaforme– J2SE (Standard Edition)
• Applicazioni desktop
– J2EE (Enterprise Edition)• Applicazioni server oriented
– J2ME (Mobile Edition)• Applicazioni Mobile
– …
![Page 7: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/7.jpg)
Software per TLC - V.1.1 7
Hello world
• Riga1: dichiarazione della classe• Righe 2-7: parentesi graffe che contengono la classe• Riga 3: dichiarazione del metodo main• Riga 5: stampa la stringa “Hello World!” usando una libreria Java
Esercizio 1digitarlo in notepad e salvarlo come HelloWorld.javacompilarlo con javac e eseguirlo con java
Esercizio 2Eseguirlo con Eclipse (vedi Tutorial)
![Page 8: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/8.jpg)
Software per TLC - V.1.1 8
Capitolo 2
1. Classe2. Oggetto3. Membro
• Attributo
• Metodo
4. Costruttore5. Package
![Page 9: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/9.jpg)
Software per TLC - V.1.1 9
Classi ed Oggetti
• Una classe è una astrazione indicante un insieme di oggetti che condividono le stesse funzionalità
• Un oggetto è una istanza (fisica) di una classe
![Page 10: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/10.jpg)
Software per TLC - V.1.1 10
La classe Punto
• Astrazione del punto cartesiano bidimensionale
• Possiamo compilarlo– javac Punto.java
• Non possiamo eseguirlo– java Punto
• Abbiamo definito il “template” ma non l’oggetto– La sua realizzazione fisica nel PC
![Page 11: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/11.jpg)
Software per TLC - V.1.1 11
Gli oggetti Punto
Creo un oggetto
Creo un altro oggetto
La main per potereseguire il codice
![Page 12: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/12.jpg)
Software per TLC - V.1.1 12
Graficamente ...
![Page 13: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/13.jpg)
Software per TLC - V.1.1 13
Osservazioni
• Classe Punto– Definisce la struttura dati
– La usiamo in compilazione– Sono gli oggetti che hanno un ruolo attivo
• A rigore– Le classi non dovrebbero possedere membri
• Nota: sono gli oggetti che possiedono x e y
– Infatti per accedere alla locazione di memoria• nomeOggetto.nomeVariabile• La variabile appartiene a punto1 e non a Punto
![Page 14: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/14.jpg)
Software per TLC - V.1.1 14
Osservazioni
• Prima Eccezione: La classe Principale – Esegue del codice nella classe
– Non su un oggetto creato dalla classe
• Per storia:– Java non usa programmi “chiamanti” come il C++– Java avvia i programmi un metodo “statico” della
classe: la main
– Va sempre dichiarata così:
![Page 15: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/15.jpg)
Software per TLC - V.1.1 15
Astrarre la realtà
![Page 16: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/16.jpg)
Software per TLC - V.1.1 16
UML
![Page 17: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/17.jpg)
Software per TLC - V.1.1 17
Dichiarare Metodi
• Modificatori– cambiano le caratteristiche del metodo– Esempi: public, static
• Tipo di ritorno– Il tipo di dato che il metodo restituisce– Può essere un tipo primitivo (int) od un oggetto (String) o nulla (void)
• Nome del metodo• Parametri
– Dichiarazione di variabili che possono essere passate al metodo– Possono non esserci– Se più di uno vanno separati dalla virgola
• Corpo del metodo– Le istruzioni da eseguire
![Page 18: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/18.jpg)
Software per TLC - V.1.1 18
Esempio
![Page 19: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/19.jpg)
Software per TLC - V.1.1 19
Accesso a Metodi e Variabili
• Metodi:– nomeOggetto.nomeMetodo()
– Nota: l’oggetto1 va creato con un new
• Variabile– nomeOggetto.nomevariabile
![Page 20: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/20.jpg)
Software per TLC - V.1.1 20
Esempio
![Page 21: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/21.jpg)
Software per TLC - V.1.1 21
Dichiarare Variabili
• Modificatori: – cambiano le caratteristiche della variabile
• Tipo di dato– Il tipo di dato della variabile
• Nome della Variabile
• Inizializzazione– Il valore a cui viene impostata la memoria di default
![Page 22: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/22.jpg)
Software per TLC - V.1.1 22
Variabili
• Variabili d’istanza– Dichiarate nella classe ma fuori da un metodo
– Fanno parte dell’oggetto• Vengono allocate con il new dell’oggetto• Vengono de-allocate quando l’oggetto non esiste più
• Variabili locali– Sono dichiarate all’interno dei metodi– Vengono allocate quando si esegue il metodo
![Page 23: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/23.jpg)
Software per TLC - V.1.1 23
Parametri o Argomenti
• Compaiono nella dichiarazione dei metodi
• Sono creati quando chiamiamo il metodo
![Page 24: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/24.jpg)
Software per TLC - V.1.1 24
I Metodi Costruttori
• Metodi speciali con le seguenti proprietà– Hanno lo stesso nome della classe
– Non hanno tipo di ritorno– Sono chiamati automaticamente se creo un
oggetto della classe
![Page 25: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/25.jpg)
Software per TLC - V.1.1 25
Creare oggetti
• Dichiarazione ed istanza
• Solo istanza
– Non è utilizzabile mi manca il riferimento all’oggetto
![Page 26: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/26.jpg)
Software per TLC - V.1.1 26
Costruttori con parametri
• Non posso più usare
• Creo l’oggetto con
![Page 27: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/27.jpg)
Software per TLC - V.1.1 27
Package
• Permette di raggruppare classi java• In pratica:
– È una cartella nel nostro PC
• Per crearlo– Almeno una Classe deve dichiarare
l’appartenenza al package– Tale classe deve risiedere in tale cartella
![Page 28: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/28.jpg)
Software per TLC - V.1.1 28
Per casa
• Fare gli esercizi del Capitolo 2
![Page 29: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/29.jpg)
Software per TLC - V.1.1 29
Capitolo 3
• Stile di codifica• Regole per gli identificatori• Tipi di Dato
– tipi primitivi: int, double, etc
– tipi non primitivi: reference
• Classi di Java– String
• Array
![Page 30: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/30.jpg)
Software per TLC - V.1.1 30
Stile di Codifica e Commenti
![Page 31: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/31.jpg)
Software per TLC - V.1.1 31
Regole per gli identificatori
• Identificatori:– nomi di classi, metodi, variabili, package, etc.
• Primo carattere– A-Z, a-z, _, $
• Altri caratteri– A-Z, a-z, _, $, 0-9
• Non possono essere una keyword java!!– ex new, class, etc
– Elenco delle keyword sul libro
![Page 32: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/32.jpg)
Software per TLC - V.1.1 32
Tipi Primitivi
• Tipi interi– byte, short, int, long
• Tipi a virgola mobile– float e double
• Tipo testuale– char
• Tipo logico-booleano– boolean
![Page 33: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/33.jpg)
Software per TLC - V.1.1 33
Tipi Primitivi
ValoriDimensioneTipo
1.7e+308 (15 decim)
64-bit (IEEE-754)double
3.4e+38 (7 decim.)32-bit (IEEE-754)float
-263 … + 263-1signed 64-bitlong
-231 … + 231-1signed 32-bitint
-32768 … +32767signed 16-bitshort
-128…+127signed 8-bitbyte
‘\n’16-bit Unicodechar
true/false1-bitboolean
![Page 34: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/34.jpg)
Software per TLC - V.1.1 34
Esplicitare il tipo
• il tipo va esplicitato:– 10 è un int (default)
– 10 è anche uno short– 10 è anche un byte
– 10L è un long– 10F è un float
– 10D è un double– 1.0F è un float
– 1.0 è un double (default)
![Page 35: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/35.jpg)
Software per TLC - V.1.1 35
Conversioni di tipo
• Conversioni automatiche se non c’è perdita di precisione:– da numeri interi a numeri in virgola mobile
– fra interi di cardinalità minore a interi di cardinalitàmaggiore (int >> long)
– da float a double
• Negli altri casi devo usare un “casting”esplicito– double >> int– (int)(5.5+0.4) vale 5
![Page 36: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/36.jpg)
Software per TLC - V.1.1 36
Riferimenti ad Oggetti
• Simile alla dichiarazione di tipi primitivi– Il nomeOggetto è detto reference (riferimento)– Contiene un indirizzo di memoria
![Page 37: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/37.jpg)
Software per TLC - V.1.1 37
Schema di allocazione in memoria
![Page 38: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/38.jpg)
Software per TLC - V.1.1 38
Schema di allocazione in memoria
![Page 39: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/39.jpg)
Software per TLC - V.1.1 39
Passaggio dei Parametri
• Il passaggio dei Parametri avviene per valore– Al metodo viene passato il valore della variabile
![Page 40: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/40.jpg)
Software per TLC - V.1.1 40
Passaggio dei Parametri
![Page 41: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/41.jpg)
Software per TLC - V.1.1 41
Passaggio dei Parametri
• Risultato
• Non permetto ad un metodo di cambiare il “reference” dichiarato nel chiamamante
• Riscriviamo il metodo
• Permetto al metodo di cambiare l’oggetto che è“puntato” dal reference
![Page 42: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/42.jpg)
Software per TLC - V.1.1 42
Librerie Standard
• java.io– contiene classi per realizzare l’input – output in Java
• java.awt– contiene classi per realizzare interfacce grafiche, (es. Button)
• java.net– contiene classi per realizzare connessioni, come Socket
• java.applet– contiene un’unica classe: Applet
• java.util– raccoglie classi d’utilità (es. Date)
• java.lang– è il package che contiene le classi nucleo del linguaggio, come
System e String
![Page 43: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/43.jpg)
Software per TLC - V.1.1 43
Usare le Librerie
• Comando import
• Osservazioni:– java.lang.* è importato di default
– Lo * non importa i sottopackage
![Page 44: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/44.jpg)
Software per TLC - V.1.1 44
La classe String
• Creare una stringa
• Oppure
• Le stringhe sono oggetti con metodi:– toUpperCase()– toLowerCase()
– trim()– etc.
![Page 45: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/45.jpg)
Software per TLC - V.1.1 45
Oggetti Immutabili
• Produce
• I metodi di String non cambiano la stringa contenuta nell’oggetto:– Restituiscono un’altra stringa modificata
• Per gli altri metodi di String vedere la documentazione Java
![Page 46: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/46.jpg)
Software per TLC - V.1.1 46
Documentazione Java
• Comando javadoc– Genera codice html dal codice e dai commenti
• La documentazione Java è scritta con javadoc
– Vedere http://java.sun.com/javase/6/docs/api/• Esercizio
– Scrivere commenti di tipo javadoc in una Classe– Eseguire
• javadoc NomeClasse.java
![Page 47: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/47.jpg)
Software per TLC - V.1.1 47
Gli Array
• Dichiarare Array
• Creare Array
• Inizializzare Array
![Page 48: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/48.jpg)
Software per TLC - V.1.1 48
Gli Array
• Dichiarazione/Creazione/Inizializzatione
• Lunghezza di un Array
![Page 49: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/49.jpg)
Software per TLC - V.1.1 49
Array Multidimensionali
• Sono Array di Array– Può non essere rettangolare!!!
![Page 50: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/50.jpg)
Software per TLC - V.1.1 50
Limiti degli Array
• Non possono essere eterogenei• Non si possono ridimensionare
• In realtà gli Array sono oggetti e quindi il seguente codice agisce solo sul riferimento
• Per copiare gli Array si usa arraycopy– Della classe System
• Vedremo classi Java che superano questi problemi
![Page 51: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/51.jpg)
Software per TLC - V.1.1 51
Per casa
• Fare gli esercizi del Capitolo 3
![Page 52: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/52.jpg)
Software per TLC - V.1.1 52
Operatori
• Assegnazione=
• Aritmetici+, -, *, /, %+=, -=, *=, /=, %=
• Unari di pre e post incremento/decremento++, --
• Bitwise∼, &, |, ^, <<, >>, >>>, &=, |=, ^=, <<=, >>=
• Relazionali==, !=, >, <, <=, >=
• Booleani! , &, |, ^, &&, ||, &=, |=, ^=
![Page 53: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/53.jpg)
Software per TLC - V.1.1 53
Operazioni su Stringhe
• Uguaglianza
• Concatenazione
![Page 54: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/54.jpg)
Software per TLC - V.1.1 54
Istruzioni if, else
![Page 55: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/55.jpg)
Software per TLC - V.1.1 55
Istruzioni if, else
![Page 56: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/56.jpg)
Software per TLC - V.1.1 56
Operatore Ternario
• Può sostituire il costrutto if else
• true assegna expr1• false assegna expr2
• Esempio
![Page 57: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/57.jpg)
Software per TLC - V.1.1 57
Istruzione while
![Page 58: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/58.jpg)
Software per TLC - V.1.1 58
Istruzioni do while
![Page 59: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/59.jpg)
Software per TLC - V.1.1 59
Istruzione for
![Page 60: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/60.jpg)
Software per TLC - V.1.1 60
Istruzioni switch case
![Page 61: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/61.jpg)
Software per TLC - V.1.1 61
Istruzioni break e continue
![Page 62: Linguaggio Java - uniroma2.it · Software per TLC - V.1.1 3 Java Development Kit • bin : contiene i file eseguibili – javac: compilatore – java: avvia la JVM ed esegue i programmi](https://reader031.vdocuments.pub/reader031/viewer/2022020318/5c09b23409d3f29a3a8bb051/html5/thumbnails/62.jpg)
Software per TLC - V.1.1 62
Per casa
• Fare gli esercizi del Capitolo 4