logo

Opérateurs en Java

Opérateur dans Java est un symbole utilisé pour effectuer des opérations. Par exemple : +, -, *, /etc.

Il existe de nombreux types d'opérateurs en Java qui sont indiqués ci-dessous :

  • Opérateur unaire,
  • Opérateur arithmétique,
  • Opérateur de quart,
  • Opérateur relationnel,
  • Opérateur au niveau du bit,
  • Opérateur logique,
  • Opérateur ternaire et
  • Opérateur d'assignation.

Priorité des opérateurs Java

Type d'opérateurCatégoriePriorité
Unairesuffixe <em>expr</em> ++ <em>expr</em> --
préfixe++ <em>expr</em> -- <em>expr</em> + <em>expr</em> - <em>expr</em> ~ !
Arithmétiquemultiplicatif* / %
additif+ -
Changementchangement&lt;&gt; &gt;&gt;&gt;
Relationnelcomparaison = instanceof
égalité== !=
Au niveau du bitET au niveau du bit&amp;
OU exclusif au niveau du bit^
OU inclusif au niveau du bit|
LogiqueET logique&amp;&amp;
OU logique||
Ternaireternaire? :
Affectationaffectation= += -= *= /= %= &amp;= ^= |= &lt;&gt;= &gt;&gt;&gt;=

Opérateur unaire Java

Les opérateurs unaires Java ne nécessitent qu'un seul opérande. Les opérateurs unaires sont utilisés pour effectuer diverses opérations, à savoir :

  • incrémenter/décrémenter une valeur de un
  • nier une expression
  • inverser la valeur d'un booléen

Exemple d'opérateur unaire Java : ++ et --

 public class OperatorExample{ public static void main(String args[]){ int x=10; System.out.println(x++);//10 (11) System.out.println(++x);//12 System.out.println(x--);//12 (11) System.out.println(--x);//10 }} 

Sortir:

 10 12 12 10 

Opérateur unaire Java Exemple 2 : ++ et --

 public class OperatorExample{ public static void main(String args[]){ int a=10; int b=10; System.out.println(a++ + ++a);//10+12=22 System.out.println(b++ + b++);//10+11=21 }} 

Sortir:

conversion de chaîne Java en entier
 22 21 

Exemple d'opérateur unaire Java : ~ et !

 public class OperatorExample{ public static void main(String args[]){ int a=10; int b=-10; boolean c=true; boolean d=false; System.out.println(~a);//-11 (minus of total positive value which starts from 0) System.out.println(~b);//9 (positive of total minus, positive starts from 0) System.out.println(!c);//false (opposite of boolean value) System.out.println(!d);//true }} 

Sortir:

 -11 9 false true 

Opérateurs arithmétiques Java

Les opérateurs arithmétiques Java sont utilisés pour effectuer des additions, des soustractions, des multiplications et des divisions. Ils agissent comme des opérations mathématiques de base.

planification du tournoi à la ronde

Exemple d'opérateur arithmétique Java

 public class OperatorExample{ public static void main(String args[]){ int a=10; int b=5; System.out.println(a+b);//15 System.out.println(a-b);//5 System.out.println(a*b);//50 System.out.println(a/b);//2 System.out.println(a%b);//0 }} 

Sortir:

 15 5 50 2 0 

Exemple d'opérateur arithmétique Java : expression

 public class OperatorExample{ public static void main(String args[]){ System.out.println(10*10/5+3-1*4/2); }} 

Sortir:

 21 

Opérateur de décalage gauche Java

L'opérateur de décalage à gauche Java << est utilisé pour décaler tous les bits d'une valeur vers la gauche d'un nombre de fois spécifié.

Exemple d'opérateur de décalage gauche Java

 public class OperatorExample{ public static void main(String args[]){ System.out.println(10&lt;<2); 10*2^2="10*4=40" system.out.println(10<<3); 10*2^3="10*8=80" system.out.println(20<<2); 20*2^2="20*4=80" system.out.println(15<<4); 15*2^4="15*16=240" }} < pre> <p> <strong>Output:</strong> </p> <pre> 40 80 80 240 </pre> <h3>Java Right Shift Operator</h3> <p>The Java right shift operator &gt;&gt; is used to move the value of the left operand to right by the number of bits specified by the right operand.</p> <h3>Java Right Shift Operator Example</h3> <pre> public OperatorExample{ public static void main(String args[]){ System.out.println(10&gt;&gt;2);//10/2^2=10/4=2 System.out.println(20&gt;&gt;2);//20/2^2=20/4=5 System.out.println(20&gt;&gt;3);//20/2^3=20/8=2 }} </pre> <p> <strong>Output:</strong> </p> <pre> 2 5 2 </pre> <h3>Java Shift Operator Example: &gt;&gt; vs &gt;&gt;&gt;</h3> <pre> public class OperatorExample{ public static void main(String args[]){ //For positive number, &gt;&gt; and &gt;&gt;&gt; works same System.out.println(20&gt;&gt;2); System.out.println(20&gt;&gt;&gt;2); //For negative number, &gt;&gt;&gt; changes parity bit (MSB) to 0 System.out.println(-20&gt;&gt;2); System.out.println(-20&gt;&gt;&gt;2); }} </pre> <p> <strong>Output:</strong> </p> <pre> 5 5 -5 1073741819 </pre> <h3>Java AND Operator Example: Logical &amp;&amp; and Bitwise &amp;</h3> <p>The logical &amp;&amp; operator doesn&apos;t check the second condition if the first condition is false. It checks the second condition only if the first one is true.</p> <p>The bitwise &amp; operator always checks both conditions whether first condition is true or false.</p> <pre> public class OperatorExample{ public static void main(String args[]){ int a=10; int b=5; int c=20; System.out.println(a <b&&a<c); false && true="false" system.out.println(a <b&a<c); & }} < pre> <p> <strong>Output:</strong> </p> <pre> false false </pre> <h3>Java AND Operator Example: Logical &amp;&amp; vs Bitwise &amp;</h3> <pre> public class OperatorExample{ public static void main(String args[]){ int a=10; int b=5; int c=20; System.out.println(a <b&&a++<c); 10 11 false && true="false" system.out.println(a); because second condition is not checked system.out.println(a <b&a++<c); }} < pre> <p> <strong>Output:</strong> </p> <pre> false 10 false 11 </pre> <h3>Java OR Operator Example: Logical || and Bitwise |</h3> <p>The logical || operator doesn&apos;t check the second condition if the first condition is true. It checks the second condition only if the first one is false.</p> <p>The bitwise | operator always checks both conditions whether first condition is true or false.</p> <pre> public class OperatorExample{ public static void main(String args[])} </pre> <p> <strong>Output:</strong> </p> <pre> true true true 10 true 11 </pre> <h3>Java Ternary Operator</h3> <p>Java Ternary operator is used as one line replacement for if-then-else statement and used a lot in Java programming. It is the only conditional operator which takes three operands.</p> <h3>Java Ternary Operator Example</h3> <pre> public class OperatorExample{ public static void main(String args[]){ int a=2; int b=5; int min=(a <b)?a:b; system.out.println(min); }} < pre> <p> <strong>Output:</strong> </p> <pre> 2 </pre> <p>Another Example:</p> <pre> public class OperatorExample{ public static void main(String args[]){ int a=10; int b=5; int min=(a <b)?a:b; system.out.println(min); }} < pre> <p> <strong>Output:</strong> </p> <pre> 5 </pre> <h3>Java Assignment Operator</h3> <p>Java assignment operator is one of the most common operators. It is used to assign the value on its right to the operand on its left.</p> <h3>Java Assignment Operator Example</h3> <pre> public class OperatorExample{ public static void main(String args[]){ int a=10; int b=20; a+=4;//a=a+4 (a=10+4) b-=4;//b=b-4 (b=20-4) System.out.println(a); System.out.println(b); }} </pre> <p> <strong>Output:</strong> </p> <pre> 14 16 </pre> <h3>Java Assignment Operator Example</h3> <pre> public class OperatorExample{ public static void main(String[] args){ int a=10; a+=3;//10+3 System.out.println(a); a-=4;//13-4 System.out.println(a); a*=2;//9*2 System.out.println(a); a/=2;//18/2 System.out.println(a); }} </pre> <p> <strong>Output:</strong> </p> <pre> 13 9 18 9 </pre> <h3>Java Assignment Operator Example: Adding short</h3> <pre> public class OperatorExample{ public static void main(String args[]){ short a=10; short b=10; //a+=b;//a=a+b internally so fine a=a+b;//Compile time error because 10+10=20 now int System.out.println(a); }} </pre> <p> <strong>Output:</strong> </p> <pre> Compile time error </pre> <p>After type cast:</p> <pre> public class OperatorExample{ public static void main(String args[]){ short a=10; short b=10; a=(short)(a+b);//20 which is int now converted to short System.out.println(a); }} </pre> <p> <strong>Output:</strong> </p> <pre> 20 </pre> <hr> <h3>You may also like</h3>  <strong>Operator Shifting in Java</strong> <p></p> <hr></b)?a:b;></pre></b)?a:b;></pre></b&&a++<c);></pre></b&&a<c);></pre></2);>

Opérateur de décalage à droite Java

L'opérateur de décalage à droite Java >> est utilisé pour déplacer la valeur de l'opérande de gauche vers la droite du nombre de bits spécifié par l'opérande de droite.

Exemple d'opérateur Java Right Shift

 public OperatorExample{ public static void main(String args[]){ System.out.println(10&gt;&gt;2);//10/2^2=10/4=2 System.out.println(20&gt;&gt;2);//20/2^2=20/4=5 System.out.println(20&gt;&gt;3);//20/2^3=20/8=2 }} 

Sortir:

'l'algorithme de Kruskal'
 2 5 2 

Exemple d'opérateur Java Shift : >> vs >>>

 public class OperatorExample{ public static void main(String args[]){ //For positive number, &gt;&gt; and &gt;&gt;&gt; works same System.out.println(20&gt;&gt;2); System.out.println(20&gt;&gt;&gt;2); //For negative number, &gt;&gt;&gt; changes parity bit (MSB) to 0 System.out.println(-20&gt;&gt;2); System.out.println(-20&gt;&gt;&gt;2); }} 

Sortir:

 5 5 -5 1073741819 

Exemple d'opérateur Java AND : logique && et bit à bit &

L'opérateur logique && ne vérifie pas la deuxième condition si la première condition est fausse. Il vérifie la deuxième condition uniquement si la première est vraie.

L'opérateur bitwise & vérifie toujours les deux conditions si la première condition est vraie ou fausse.

 public class OperatorExample{ public static void main(String args[]){ int a=10; int b=5; int c=20; System.out.println(a <b&&a<c); false && true="false" system.out.println(a <b&a<c); & }} < pre> <p> <strong>Output:</strong> </p> <pre> false false </pre> <h3>Java AND Operator Example: Logical &amp;&amp; vs Bitwise &amp;</h3> <pre> public class OperatorExample{ public static void main(String args[]){ int a=10; int b=5; int c=20; System.out.println(a <b&&a++<c); 10 11 false && true="false" system.out.println(a); because second condition is not checked system.out.println(a <b&a++<c); }} < pre> <p> <strong>Output:</strong> </p> <pre> false 10 false 11 </pre> <h3>Java OR Operator Example: Logical || and Bitwise |</h3> <p>The logical || operator doesn&apos;t check the second condition if the first condition is true. It checks the second condition only if the first one is false.</p> <p>The bitwise | operator always checks both conditions whether first condition is true or false.</p> <pre> public class OperatorExample{ public static void main(String args[])} </pre> <p> <strong>Output:</strong> </p> <pre> true true true 10 true 11 </pre> <h3>Java Ternary Operator</h3> <p>Java Ternary operator is used as one line replacement for if-then-else statement and used a lot in Java programming. It is the only conditional operator which takes three operands.</p> <h3>Java Ternary Operator Example</h3> <pre> public class OperatorExample{ public static void main(String args[]){ int a=2; int b=5; int min=(a <b)?a:b; system.out.println(min); }} < pre> <p> <strong>Output:</strong> </p> <pre> 2 </pre> <p>Another Example:</p> <pre> public class OperatorExample{ public static void main(String args[]){ int a=10; int b=5; int min=(a <b)?a:b; system.out.println(min); }} < pre> <p> <strong>Output:</strong> </p> <pre> 5 </pre> <h3>Java Assignment Operator</h3> <p>Java assignment operator is one of the most common operators. It is used to assign the value on its right to the operand on its left.</p> <h3>Java Assignment Operator Example</h3> <pre> public class OperatorExample{ public static void main(String args[]){ int a=10; int b=20; a+=4;//a=a+4 (a=10+4) b-=4;//b=b-4 (b=20-4) System.out.println(a); System.out.println(b); }} </pre> <p> <strong>Output:</strong> </p> <pre> 14 16 </pre> <h3>Java Assignment Operator Example</h3> <pre> public class OperatorExample{ public static void main(String[] args){ int a=10; a+=3;//10+3 System.out.println(a); a-=4;//13-4 System.out.println(a); a*=2;//9*2 System.out.println(a); a/=2;//18/2 System.out.println(a); }} </pre> <p> <strong>Output:</strong> </p> <pre> 13 9 18 9 </pre> <h3>Java Assignment Operator Example: Adding short</h3> <pre> public class OperatorExample{ public static void main(String args[]){ short a=10; short b=10; //a+=b;//a=a+b internally so fine a=a+b;//Compile time error because 10+10=20 now int System.out.println(a); }} </pre> <p> <strong>Output:</strong> </p> <pre> Compile time error </pre> <p>After type cast:</p> <pre> public class OperatorExample{ public static void main(String args[]){ short a=10; short b=10; a=(short)(a+b);//20 which is int now converted to short System.out.println(a); }} </pre> <p> <strong>Output:</strong> </p> <pre> 20 </pre> <hr> <h3>You may also like</h3>  <strong>Operator Shifting in Java</strong> <p></p> <hr></b)?a:b;></pre></b)?a:b;></pre></b&&a++<c);></pre></b&&a<c);>

Exemple d'opérateur Java AND : logique && vs Bitwise &

 public class OperatorExample{ public static void main(String args[]){ int a=10; int b=5; int c=20; System.out.println(a <b&&a++<c); 10 11 false && true="false" system.out.println(a); because second condition is not checked system.out.println(a <b&a++<c); }} < pre> <p> <strong>Output:</strong> </p> <pre> false 10 false 11 </pre> <h3>Java OR Operator Example: Logical || and Bitwise |</h3> <p>The logical || operator doesn&apos;t check the second condition if the first condition is true. It checks the second condition only if the first one is false.</p> <p>The bitwise | operator always checks both conditions whether first condition is true or false.</p> <pre> public class OperatorExample{ public static void main(String args[])} </pre> <p> <strong>Output:</strong> </p> <pre> true true true 10 true 11 </pre> <h3>Java Ternary Operator</h3> <p>Java Ternary operator is used as one line replacement for if-then-else statement and used a lot in Java programming. It is the only conditional operator which takes three operands.</p> <h3>Java Ternary Operator Example</h3> <pre> public class OperatorExample{ public static void main(String args[]){ int a=2; int b=5; int min=(a <b)?a:b; system.out.println(min); }} < pre> <p> <strong>Output:</strong> </p> <pre> 2 </pre> <p>Another Example:</p> <pre> public class OperatorExample{ public static void main(String args[]){ int a=10; int b=5; int min=(a <b)?a:b; system.out.println(min); }} < pre> <p> <strong>Output:</strong> </p> <pre> 5 </pre> <h3>Java Assignment Operator</h3> <p>Java assignment operator is one of the most common operators. It is used to assign the value on its right to the operand on its left.</p> <h3>Java Assignment Operator Example</h3> <pre> public class OperatorExample{ public static void main(String args[]){ int a=10; int b=20; a+=4;//a=a+4 (a=10+4) b-=4;//b=b-4 (b=20-4) System.out.println(a); System.out.println(b); }} </pre> <p> <strong>Output:</strong> </p> <pre> 14 16 </pre> <h3>Java Assignment Operator Example</h3> <pre> public class OperatorExample{ public static void main(String[] args){ int a=10; a+=3;//10+3 System.out.println(a); a-=4;//13-4 System.out.println(a); a*=2;//9*2 System.out.println(a); a/=2;//18/2 System.out.println(a); }} </pre> <p> <strong>Output:</strong> </p> <pre> 13 9 18 9 </pre> <h3>Java Assignment Operator Example: Adding short</h3> <pre> public class OperatorExample{ public static void main(String args[]){ short a=10; short b=10; //a+=b;//a=a+b internally so fine a=a+b;//Compile time error because 10+10=20 now int System.out.println(a); }} </pre> <p> <strong>Output:</strong> </p> <pre> Compile time error </pre> <p>After type cast:</p> <pre> public class OperatorExample{ public static void main(String args[]){ short a=10; short b=10; a=(short)(a+b);//20 which is int now converted to short System.out.println(a); }} </pre> <p> <strong>Output:</strong> </p> <pre> 20 </pre> <hr> <h3>You may also like</h3>  <strong>Operator Shifting in Java</strong> <p></p> <hr></b)?a:b;></pre></b)?a:b;></pre></b&&a++<c);>

Exemple d'opérateur Java OR : logique || et au niveau du bit |

Le logique || l'opérateur ne vérifie pas la deuxième condition si la première condition est vraie. Il vérifie la deuxième condition uniquement si la première est fausse.

Le bit à bit | L'opérateur vérifie toujours les deux conditions si la première condition est vraie ou fausse.

chaîne concaténer java
 public class OperatorExample{ public static void main(String args[])} 

Sortir:

 true true true 10 true 11 

Opérateur ternaire Java

L'opérateur Java Ternaire est utilisé comme remplacement d'une ligne pour l'instruction if-then-else et est beaucoup utilisé dans la programmation Java. C'est le seul opérateur conditionnel qui prend trois opérandes.

Exemple d'opérateur ternaire Java

 public class OperatorExample{ public static void main(String args[]){ int a=2; int b=5; int min=(a <b)?a:b; system.out.println(min); }} < pre> <p> <strong>Output:</strong> </p> <pre> 2 </pre> <p>Another Example:</p> <pre> public class OperatorExample{ public static void main(String args[]){ int a=10; int b=5; int min=(a <b)?a:b; system.out.println(min); }} < pre> <p> <strong>Output:</strong> </p> <pre> 5 </pre> <h3>Java Assignment Operator</h3> <p>Java assignment operator is one of the most common operators. It is used to assign the value on its right to the operand on its left.</p> <h3>Java Assignment Operator Example</h3> <pre> public class OperatorExample{ public static void main(String args[]){ int a=10; int b=20; a+=4;//a=a+4 (a=10+4) b-=4;//b=b-4 (b=20-4) System.out.println(a); System.out.println(b); }} </pre> <p> <strong>Output:</strong> </p> <pre> 14 16 </pre> <h3>Java Assignment Operator Example</h3> <pre> public class OperatorExample{ public static void main(String[] args){ int a=10; a+=3;//10+3 System.out.println(a); a-=4;//13-4 System.out.println(a); a*=2;//9*2 System.out.println(a); a/=2;//18/2 System.out.println(a); }} </pre> <p> <strong>Output:</strong> </p> <pre> 13 9 18 9 </pre> <h3>Java Assignment Operator Example: Adding short</h3> <pre> public class OperatorExample{ public static void main(String args[]){ short a=10; short b=10; //a+=b;//a=a+b internally so fine a=a+b;//Compile time error because 10+10=20 now int System.out.println(a); }} </pre> <p> <strong>Output:</strong> </p> <pre> Compile time error </pre> <p>After type cast:</p> <pre> public class OperatorExample{ public static void main(String args[]){ short a=10; short b=10; a=(short)(a+b);//20 which is int now converted to short System.out.println(a); }} </pre> <p> <strong>Output:</strong> </p> <pre> 20 </pre> <hr> <h3>You may also like</h3>  <strong>Operator Shifting in Java</strong> <p></p> <hr></b)?a:b;></pre></b)?a:b;>

Un autre exemple:

 public class OperatorExample{ public static void main(String args[]){ int a=10; int b=5; int min=(a <b)?a:b; system.out.println(min); }} < pre> <p> <strong>Output:</strong> </p> <pre> 5 </pre> <h3>Java Assignment Operator</h3> <p>Java assignment operator is one of the most common operators. It is used to assign the value on its right to the operand on its left.</p> <h3>Java Assignment Operator Example</h3> <pre> public class OperatorExample{ public static void main(String args[]){ int a=10; int b=20; a+=4;//a=a+4 (a=10+4) b-=4;//b=b-4 (b=20-4) System.out.println(a); System.out.println(b); }} </pre> <p> <strong>Output:</strong> </p> <pre> 14 16 </pre> <h3>Java Assignment Operator Example</h3> <pre> public class OperatorExample{ public static void main(String[] args){ int a=10; a+=3;//10+3 System.out.println(a); a-=4;//13-4 System.out.println(a); a*=2;//9*2 System.out.println(a); a/=2;//18/2 System.out.println(a); }} </pre> <p> <strong>Output:</strong> </p> <pre> 13 9 18 9 </pre> <h3>Java Assignment Operator Example: Adding short</h3> <pre> public class OperatorExample{ public static void main(String args[]){ short a=10; short b=10; //a+=b;//a=a+b internally so fine a=a+b;//Compile time error because 10+10=20 now int System.out.println(a); }} </pre> <p> <strong>Output:</strong> </p> <pre> Compile time error </pre> <p>After type cast:</p> <pre> public class OperatorExample{ public static void main(String args[]){ short a=10; short b=10; a=(short)(a+b);//20 which is int now converted to short System.out.println(a); }} </pre> <p> <strong>Output:</strong> </p> <pre> 20 </pre> <hr> <h3>You may also like</h3>  <strong>Operator Shifting in Java</strong> <p></p> <hr></b)?a:b;>

Opérateur d'affectation Java

L'opérateur d'affectation Java est l'un des opérateurs les plus courants. Il permet d'attribuer la valeur à sa droite à l'opérande à sa gauche.

Exemple d'opérateur d'affectation Java

 public class OperatorExample{ public static void main(String args[]){ int a=10; int b=20; a+=4;//a=a+4 (a=10+4) b-=4;//b=b-4 (b=20-4) System.out.println(a); System.out.println(b); }} 

Sortir:

 14 16 

Exemple d'opérateur d'affectation Java

 public class OperatorExample{ public static void main(String[] args){ int a=10; a+=3;//10+3 System.out.println(a); a-=4;//13-4 System.out.println(a); a*=2;//9*2 System.out.println(a); a/=2;//18/2 System.out.println(a); }} 

Sortir:

 13 9 18 9 

Exemple d'opérateur d'affectation Java : ajout d'un court

 public class OperatorExample{ public static void main(String args[]){ short a=10; short b=10; //a+=b;//a=a+b internally so fine a=a+b;//Compile time error because 10+10=20 now int System.out.println(a); }} 

Sortir:

 Compile time error 

Après la conversion du type :

commandes git pour push
 public class OperatorExample{ public static void main(String args[]){ short a=10; short b=10; a=(short)(a+b);//20 which is int now converted to short System.out.println(a); }} 

Sortir:

 20 

Tu pourrais aussi aimer

Changement d'opérateur en Java