logo

Operatoren in Java

Exploitant in Java is een symbool dat wordt gebruikt om bewerkingen uit te voeren. Bijvoorbeeld: +, -, *, / enz.

Er zijn veel soorten operators in Java, die hieronder worden weergegeven:

  • Unaire operator,
  • Rekenkundige operator,
  • Ploegenoperator,
  • relationele operator,
  • Bitwise-operator,
  • Logische operator,
  • Ternaire operator en
  • Opdracht Operator.

Voorrang van Java-operator

OperatortypeCategorieVoorrang
Unairachtervoegsel <em>expr</em> ++ <em>expr</em> --
voorvoegsel++ <em>expr</em> -- <em>expr</em> + <em>expr</em> - <em>expr</em> ~ !
Rekenkundigmultiplicatief* / %
additief+ -
Verschuivingverschuiving&lt;&gt; &gt;&gt;&gt;
relationeelvergelijking = instanceof
gelijkwaardigheid== !=
Bitsgewijsbitsgewijze EN&amp;
bitgewijze exclusieve OR^
bitgewijs inclusief OR|
Logischlogisch EN&amp;&amp;
logisch OF||
Ternairternair? :
Opdrachtopdracht= += -= *= /= %= &amp;= ^= |= &lt;&gt;= &gt;&gt;&gt;=

Java-unaire operator

De unaire Java-operatoren vereisen slechts één operand. Unaire operatoren worden gebruikt om verschillende bewerkingen uit te voeren, bijvoorbeeld:

  • een waarde met één verhogen/verlagen
  • een uitdrukking ontkennen
  • het omkeren van de waarde van een booleaanse waarde

Java Unary Operator Voorbeeld: ++ en --

 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 }} 

Uitgang:

 10 12 12 10 

Java Unary Operator Voorbeeld 2: ++ en --

 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 }} 

Uitgang:

 22 21 

Java Unary Operator Voorbeeld: ~ en !

 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 }} 

Uitgang:

 -11 9 false true 

Java-rekenkundige operatoren

Java-rekenkundige operatoren worden gebruikt om optellen, aftrekken, vermenigvuldigen en delen uit te voeren. Ze fungeren als fundamentele wiskundige bewerkingen.

Voorbeeld van Java-rekenkundige operator

 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 }} 

Uitgang:

 15 5 50 2 0 

Java-rekenkundige operator Voorbeeld: expressie

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

Uitgang:

 21 

Java-linkershift-operator

De Java left shift-operator << wordt gebruikt om alle bits in een waarde een bepaald aantal keren naar de linkerkant te verschuiven.

string naar int in java

Voorbeeld van een Java Left Shift-operator

 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);>

Java Right Shift-operator

De Java-rechtsverschuivingsoperator >> wordt gebruikt om de waarde van de linker operand naar rechts te verplaatsen met het aantal bits dat is opgegeven door de rechter operand.

Voorbeeld van een Java Right Shift-operator

 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 }} 

Uitgang:

 2 5 2 

Voorbeeld van een Java Shift-operator: >> versus >>>

 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); }} 

Uitgang:

 5 5 -5 1073741819 

Java AND Operator Voorbeeld: Logische && en Bitwise &

De logische operator && controleert de tweede voorwaarde niet als de eerste voorwaarde onwaar is. Het controleert de tweede voorwaarde alleen als de eerste waar is.

De bitwise &-operator controleert altijd beide voorwaarden of de eerste voorwaarde waar of onwaar is.

 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);>

Voorbeeld van Java AND-operator: Logisch && versus 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);>

Java OR-operator Voorbeeld: Logisch || en Bitwise |

De logische || De operator controleert de tweede voorwaarde niet als de eerste voorwaarde waar is. Het controleert de tweede voorwaarde alleen als de eerste onwaar is.

De bitsgewijze | De operator controleert altijd aan beide voorwaarden of de eerste voorwaarde waar of onwaar is.

 public class OperatorExample{ public static void main(String args[])} 

Uitgang:

 true true true 10 true 11 

Ternaire Java-operator

De Java Ternary-operator wordt gebruikt als vervanging van één regel voor de if-then-else-instructie en wordt veel gebruikt bij het programmeren in Java. Het is de enige voorwaardelijke operator die drie operanden nodig heeft.

Voorbeeld van een Java-ternaire operator

 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;>

Een ander voorbeeld:

 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;>

Java-toewijzingsoperator

Java-toewijzingsoperator is een van de meest voorkomende operators. Het wordt gebruikt om de waarde rechts ervan toe te wijzen aan de operand links ervan.

Voorbeeld van Java-toewijzingsoperator

 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); }} 

Uitgang:

 14 16 

Voorbeeld van Java-toewijzingsoperator

 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); }} 

Uitgang:

 13 9 18 9 

Java-toewijzingsoperator Voorbeeld: short

 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); }} 

Uitgang:

 Compile time error 

Na typecast:

 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); }} 

Uitgang:

 20 

Dit vind je misschien ook leuk

Operatorverschuiving in Java