- Java Standard Edition (JDK)
- Eclipse IDE for Java Developers
- Atom o Notepad++ (o qualsiasi editor di testo)
Alberto Ferrari
Università degli Studi di Parma


javac) non produce codice eseguibile da una particolare piattaforma, come farebbe un compilatore C
malloc, free e distruttori: metà dei bug in C e C++ legati alla gestione della memoria
newclass HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string
}
}
mainmainmainjavac HelloWorldApp.java
.java (source).class, contenente il bytecode-classpathCLASSPATHjava HelloWorldApp
.class (bytecode)java Xmain della classe Xjava X arg1 arg 2main: arg1, arg2 ecc.-classpath ...
File - New - Java projecthelloHelloWorldApp, con metodo mainRun as - Java applicationimport java.util.Scanner;
class HelloUser {
public static final main(String[] args) {
Scanner input = new Scanner(System.in); // to parse user input
System.out.println("Name, age?");
String name = input.next(); // get a word from console
int age = input.nextInt(); // get an int ...
System.out.println("Hello, " + name + ".");
System.out.println("You're " + age + " years old.");
}
}
nextLine di Scanner per leggere una intera riga di testobyte, short, int, longfloat, doublebooleanchar=, += ecc.==, !=, <, <=, >, >=+, -, *, /, %++, -- (prefix, postfix)&&, ||, !String greeting = "Hello world!";char c = greeting.charAt(1); // 'e'charint len = greeting.length();char occupa 16 bit (codifica UTF-16)charString txt = string1 + string2;concat di StringTAB, LF, CR, BELL ecc.LF e CR su diversi sistemi

int x = input.nextInt();
int y;
if (x >= 0) {
y = x;
System.out.println("" + x + "is positive");
} else {
y = -x
System.out.println("" + x + "is negative");
}
System.out.println("abs = " + y);
Per casi più complessi, esiste anche il costrutto switch-case
==equals (boolean) e compareTo (-1, 0, +1)String a = input.next(), b = input.next();
if (a.equals(b)) {
System.out.println("The words are equal");
} else if (a.compareTo(b) == -1) {
System.out.println("The words are ordered");
} else {
System.out.println("The words are inverted");
}
int total = 0, count = 0;
System.out.println("Val (0 to end)? ");
int val = input.nextInt();
while (val != 0) {
total += val; ++count;
System.out.println("Val (0 to end)? ");
int val = input.nextInt();
}
if (count > 0) {
float avg = total / float(count);
System.out.println("Avg: " + avg);
}
In alternativa, si può usare un ciclo do-while
length - 1!String[] toBuy = {"spam", "eggs", "beans"};
int[] rainfallData = {13, 24, 18, 15};
String[] months = {"Jan", "Feb", "Mar",
"Apr", "May", "Jun",
"Jul", "Aug", "Sep",
"Oct", "Nov", "Dec"};
// declaration
int[] k;
// creation
k = new int[3];
String[] toBuy = {"spam", "eggs", "beans"};
System.out.println(toBuy.length);
System.out.println(toBuy[1]); // eggs
toBuy[1] = "bacon";
ArrayIndexOutOfBoundsExceptionadd, get, set, sizeArrayList<String> toBuy = new ArrayList<>();
toBuy.add("spam");
System.out.println(toBuy.size()); // 1
toBuy.add("eggs");
toBuy.add("beans");
System.out.println(toBuy.size()); // 3
System.out.println(toBuy.get(2)); // beans
toBuy.set(2, "bacon");
System.out.println(toBuy.get(2)); // bacon
// see ArrayList documention
for (int i = 0; i < toBuy.size(); i++) {
String item = toBuy.get(i);
System.out.println(item);
}
for (String item : toBuy) { System.out.println(item); }
int[] rainfallData = {13, 24, 18, 15};
for (int val : rainfallData) { System.out.println(val); }
int y = in.nextInt();
for (int x = 1; x <= 10; x++) {
System.out.format("%4d", x * y);
}
System.out.println();
for (int y = 1; y <= 10; y++) {
for (int x = 1; x <= 10; x++) {
System.out.format("%4d", x * y);
}
System.out.println();
}
int[][] matrix = {{2, 4, 3, 8},
{9, 3, 2, 7},
{5, 6, 9, 1}};
int rows = matrix.length;
int cols = matrix[0].length;
for (int x = 0; x < cols; x ++) {
int total = 0;
for (int y = 0; y < rows; y++) {
int val = matrix[y][x];
total += val;
}
System.out.println("Col #" + x + " sums to " + total);
}
r di un cerchior è negativo, però, mostrare un messaggio d'erroreDefinire una costante PI = 3.14159
Altrimenti usare Math.PI
a, b, cControllare prima di tutto se a è minore degli altri due
Altrimenti controllare se b è minore di c
Altrimenti ...
Generatore di numeri pseudo-casuali: Random random = new Random()
Estrazione numero tra 0 e 89: random.nextInt(90)
ny tra n ed 1...y ripetizioni di y4444
333
22
1
Usare due cicli for annidati
All'inizio fissare y pari ad un certo valore e scrivere una sola riga; es.: y = 4 → “4444”
Poi racchiudere tutto in un ciclo for esterno, per variare y:
y parte da n, arriva ad 1, ad ogni passo decresce di 1
0 ed 1 presentiUsare un ciclo for sulla stringa
'A' a 'Z')Creare un array di 26 elementi, inizialmente tutti posti a 0
Ciascun elemento è il contatore per una certa lettera
L'indice del contatore corrispondente ad una lettera val può essere ottenuto come val - 'A'
0n, il lato di una matrice quadratan2 valori della matricek, da cercarek è presente nella matricek è presente sulle diagonali principali
rows×colsrows e cols, però celle in numero pariUsare un array semplice (anzichè una matrice, array di array)
Andare a capo quando i % cols == cols - 1
COLS×ROWS caratteriCOLS colonne × ROWS righe, valori prefissatiUsare una matrice (predefinita): char[][] matrix = new char[ROWS][COLS]
Alberto Ferrari
Università degli Studi di Parma