logo

Operatoren in Java

Operator In Java ist ein Symbol, das zum Ausführen von Operationen verwendet wird. Zum Beispiel: +, -, *, / usw.

Es gibt viele Arten von Operatoren in Java, die im Folgenden aufgeführt sind:

  • Unärer Operator,
  • Arithmetischer Operator,
  • Schichtbetreiber,
  • Vergleichsoperator,
  • Bitweiser Operator,
  • Logischer Operator,
  • Ternärer Operator und
  • Aufgabenverwalter.

Vorrang des Java-Operators

OperatortypKategorieVorrang
EinstelligPostfix <em>expr</em> ++ <em>expr</em> --
Präfix++ <em>expr</em> -- <em>expr</em> + <em>expr</em> - <em>expr</em> ~ !
Arithmetikmultiplikativ* / %
Zusatzstoff+ -
SchichtSchicht&lt;&gt; &gt;&gt;&gt;
RelationalVergleich = instanceof
Gleichwertigkeit== !=
Bitweisebitweises UND&amp;
bitweises Exklusiv-ODER^
bitweises inklusives ODER|
Logischlogisches UND&amp;&amp;
logisches ODER||
Ternärternär? :
AbtretungAbtretung= += -= *= /= %= &amp;= ^= |= &lt;&gt;= &gt;&gt;&gt;=

Unärer Java-Operator

Die unären Java-Operatoren erfordern nur einen Operanden. Unäre Operatoren werden verwendet, um verschiedene Operationen auszuführen, z. B.:

  • Erhöhen/Erniedrigen eines Werts um eins
  • einen Ausdruck negieren
  • Invertieren des Werts eines booleschen Werts

Beispiel für einen unären Java-Operator: ++ und --

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

Ausgabe:

Java-Schalter
 10 12 12 10 

Beispiel 2 für einen unären Java-Operator: ++ und --

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

Ausgabe:

 22 21 

Beispiel für einen unären Java-Operator: ~ und !

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

Ausgabe:

 -11 9 false true 

Arithmetische Java-Operatoren

Java-Arithmetikoperatoren werden zum Ausführen von Additionen, Subtraktionen, Multiplikationen und Divisionen verwendet. Sie fungieren als grundlegende mathematische Operationen.

Python speichert JSON in einer Datei

Beispiel für einen arithmetischen Java-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 }} 

Ausgabe:

 15 5 50 2 0 

Beispiel für einen arithmetischen Java-Operator: Ausdruck

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

Ausgabe:

 21 

Java-Operator für die linke Verschiebung

Der Java-Linksverschiebungsoperator << wird verwendet, um alle Bits in einem Wert um eine angegebene Anzahl von Malen nach links zu verschieben.

Beispiel für einen Java-Linksverschiebungsoperator

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

Der Java-Rechtsverschiebungsoperator >> wird verwendet, um den Wert des linken Operanden um die vom rechten Operanden angegebene Anzahl von Bits nach rechts zu verschieben.

Beispiel für einen 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 }} 

Ausgabe:

 2 5 2 

Beispiel für einen Java-Shift-Operator: >> 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); }} 

Ausgabe:

 5 5 -5 1073741819 

Beispiel für einen Java-AND-Operator: Logisches && und bitweises &

Der logische &&-Operator prüft die zweite Bedingung nicht, wenn die erste Bedingung falsch ist. Die zweite Bedingung wird nur überprüft, wenn die erste wahr ist.

Der bitweise &-Operator prüft immer beide Bedingungen, ob die erste Bedingung wahr oder falsch ist.

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

Beispiel für einen Java-AND-Operator: Logisches && vs. bitweises &

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

Beispiel für einen Java-ODER-Operator: Logisch || und bitweise |

Das logische || Der Operator prüft die zweite Bedingung nicht, wenn die erste Bedingung wahr ist. Die zweite Bedingung wird nur überprüft, wenn die erste falsch ist.

Das bitweise | Der Operator prüft immer beide Bedingungen, ob die erste Bedingung wahr oder falsch ist.

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

Ausgabe:

wie man str in int umwandelt
 true true true 10 true 11 

Ternärer Java-Operator

Der ternäre Java-Operator wird als einzeiliger Ersatz für die if-then-else-Anweisung verwendet und kommt häufig in der Java-Programmierung zum Einsatz. Es ist der einzige bedingte Operator, der drei Operanden akzeptiert.

Beispiel für einen ternären Java-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;>

Ein anderes Beispiel:

Gewerkschaft vs. Gewerkschaft alle
 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-Zuweisungsoperator

Der Java-Zuweisungsoperator ist einer der gebräuchlichsten Operatoren. Es wird verwendet, um den Wert auf der rechten Seite dem Operanden auf der linken Seite zuzuweisen.

Beispiel für einen Java-Zuweisungsoperator

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

Ausgabe:

 14 16 

Beispiel für einen Java-Zuweisungsoperator

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

Ausgabe:

 13 9 18 9 

Beispiel für einen Java-Zuweisungsoperator: Kurz hinzufügen

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

Ausgabe:

 Compile time error 

Nach der Typumwandlung:

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

Ausgabe:

 20 

Sie können auch mögen

Operatorverschiebung in Java