[ ] | ( ) | |||
++ | -- | ! | ~ | |
* | / | % | ||
+ | - | << | >> | >>> |
< | > | <= | >= | |
= = | != | |||
& | ||||
^ | ||||
| | ||||
&& | ||||
|| | ||||
?: | ||||
= | += | … |
la plupart - de gauche à droite
= += ... de droite à gauche
Les parenthèses:
l'expression dans les parenthèses est
traité comme une unité
z = p * r % q + w / x - y
==> z =((((p * r) % q) + (w / x)) - y)
6
1 2 4
3 5
z = a + b + k* (c = d = 0)
==> z = ((a + b) +( k* ((c = (d = 0)) )))
6 1
5 4 3 2
Presque tous les opérateurs ne sont utilisable que sur les type primitifs. L'exception sont '=', '= =', et '!=' qu'on peut utiliser avec tous les objets et en plus la classe "String" support les opérateur '+' et '+='.
Opérateur '+' est utilisé avec les objets de type "String" pour concaténation. Si un expression démarre avec un objet "String" tous les opérandes qui suivent sont convertis en chaînes (dans les derniers versions on fait la conversion même si l'objet "String" n'est pas le premier opérande:
int x = 0, y = 1, z =
2;
String sString = "x,
y, z ";
System.out.println(sString
+ x + y + z); // x,y,z 012
System.out.println(sString
+ (x + y + z)); // x,y,z 3
opérateur >>>
public
class Operators { public static void main(String arg[]){ int i=37; int j=42; double x=5.7; double y=1.5; System.out.println("1:"+ i/j); System.out.println("2:"+ i++ + " "+i);i=37; System.out.println("3:"+ ++i + " "+i);i=37; System.out.println("4:"+ (i=j)+" "+i+" "+j);i=37; System.out.println("5:"+ i%j); System.out.println("6:"+ (double)i/j); System.out.println("7:"+ x/y); System.out.println("8:"+ x++ +" "+x); x=5.5; System.out.println("9:"+ ++x+" "+x); x=5.5; System.out.println("10:"+ x%y); System.out.println("11:"+ i+x); System.out.println("12:"+ (i+x)); System.out.println("13:"+ (i>x)+ " "+(i<x)+" "+(i==j)); System.out.println("14"+ ((i>x)||(i<x))); System.out.println("15:"+ (((i>x)&&(i<x)))); System.out.println("16:"+ !(i>x)); System.out.println("17:"+ (i>j?x:y)); System.out.println("18:"+ (16>>2)); System.out.println("19:"+ (16<<2)); i=-8; System.out.println("20:"+ (i>>2)); // 11111111 ... 11111000 i=-1; System.out.println("21:"+ (i>>2)+" "+i); // 11111111 ... 11111111 System.out.println("22:"+ (i>>>2)); } } |
{ } un bloc
if (expression booléenne) instruction
if (expression booléenne) {
instruction
...
instruction
}
if (expression booléenne) instruction else instruction | ![]() |
if (expression booléenne) instruction else instruction
if (expression booléenne) {
instruction
...
instruction
}
else {
instruction
...
instruction
}
switch (expression intégrale) {
case
constante 1: liste instructions 1
}
switch (expression intégrale) {
|
Ex: int grade = 4; switch (grade) { case 0; case 1: case 2: System.out.println("échoué"); break; case 3: System.out.println("passable"); break; case 4: System.out.println("bien"); break; case 5: System.out.println("très bien");break; case 6: System.out.println("avec félicitations");break; default: System.out.println("Ca n'existe pas!"); } |
break;
for(initialisation liste; expression booléenne; update liste) instruction
étiquette:
for(initialisation liste; expression
booléenne;
update liste) instruction
break; break étiquette; |
public class Brk { public static void main(String arg[]) { int[][] numbers = { { 1, 2, 3 }, { 6, 5, 12}, { 6, 8, 18 } }; int searchNum = 6,i=0,j=0; boolean foundNum = false; outLoop: for (i=0; i < numbers.length; i++) { for (j=0; j < numbers[i].length; j++) { if (searchNum == numbers[i][j]) { foundNum = true; break outLoop; } } } if (foundNum) System.out.println(searchNum + " found on ["+i+","+j+"]"); else System.out.println(searchNum + " not found!"); } } |
continue; continue étiquette; |
public class Cntn { public static void main(String arg[]) { int[][] numbers = { { 1, 2, 3 }, { 6, 5, 12}, { 6, 8, 18 } }; int searchNum = 6,i=0,j=0; outLoop: for (i=0; i < numbers.length; i++) { for (j=0; j < numbers[i].length; j++) { if (searchNum == numbers[i][j]) { System.out.println(searchNum + " found on line:"+i); continue outLoop; } } } } } |
while(expression booléenne) instruction;
étiquette:
while(expression booléenne) instruction;
do instruction while(expression booléenne)
étiquette:
do instruction while(expression
booléenne)
return;