logo

Operatörer i Java

Operatör i Java är en symbol som används för att utföra operationer. Till exempel: +, -, *, / etc.

Det finns många typer av operatörer i Java som anges nedan:

  • Unär operatör,
  • Aritmetisk operatör,
  • Skiftförare,
  • Relationell operatör,
  • Bitwise Operator,
  • logisk operatör,
  • Ternär operatör och
  • Uppdragsoperatör.

Java-operatörsföreträde

OperatörstypKategoriFöreträde
Unarypostfix <em>expr</em> ++ <em>expr</em> --
prefix++ <em>expr</em> -- <em>expr</em> + <em>expr</em> - <em>expr</em> ~ !
Aritmetiskmultiplikativ* / %
tillsats+ -
Flyttaflytta&lt;&gt; &gt;&gt;&gt;
Relationelltjämförelse = instanceof
jämlikhet== !=
Bitvisbitvis OCH&amp;
bitvis exklusivt ELLER^
bitvis inklusive ELLER|
Logisklogiskt OCH&amp;&amp;
logiskt ELLER||
Ternärternär? :
Uppdraguppdrag= += -= *= /= %= &amp;= ^= |= &lt;&gt;= &gt;&gt;&gt;=

Java Unary-operatör

Java unary-operatorerna kräver bara en operand. Unära operatorer används för att utföra olika operationer, dvs.

  • öka/minska ett värde med ett
  • förnekar ett uttryck
  • invertering av värdet på ett booleskt värde

Java Unary Operator Exempel: ++ och --

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

Produktion:

 10 12 12 10 

Java Unary Operator Exempel 2: ++ och --

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

Produktion:

 22 21 

Java Unary Operator Exempel: ~ och !

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

Produktion:

 -11 9 false true 

Java aritmetiska operatörer

Java aritmetiska operatorer används för att utföra addition, subtraktion, multiplikation och division. De fungerar som grundläggande matematiska operationer.

Exempel på Java Arithmetic 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 }} 

Produktion:

 15 5 50 2 0 

Java Arithmetic Operator Exempel: Uttryck

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

Produktion:

 21 

Java vänsterväxlingsoperatör

Java left shift-operatorn << används för att flytta alla bitar i ett värde till vänster sida av ett specificerat antal gånger.

Exempel på 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 högerväxlingsoperatör

Java högerskiftoperatorn >> används för att flytta värdet på den vänstra operanden till höger med antalet bitar som anges av den högra operanden.

Java Right Shift Operator Exempel

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

Produktion:

 2 5 2 

Java Shift Operator Exempel: >> 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); }} 

Produktion:

 5 5 -5 1073741819 

Java OCH Operator Exempel: Logical && och Bitwise &

Den logiska &&-operatorn kontrollerar inte det andra villkoret om det första villkoret är falskt. Den kontrollerar det andra villkoret endast om det första är sant.

Operatorn bitvis & kontrollerar alltid båda villkoren om det första villkoret är sant eller falskt.

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

Java OCH Operator Exempel: Logisk && 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);>

Java ELLER Operator Exempel: Logisk || och Bitwise |

Den logiska || operatören kontrollerar inte det andra villkoret om det första villkoret är sant. Den kontrollerar det andra villkoret endast om det första är falskt.

Den bitvisa | operatören kontrollerar alltid båda villkoren oavsett om det första villkoret är sant eller falskt.

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

Produktion:

 true true true 10 true 11 

Java ternär operatör

Java Ternary operator används som en rad ersättning för if-then-else-satsen och används mycket i Java-programmering. Det är den enda villkorliga operatorn som tar tre operander.

Java Ternary Operator Exempel

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

Ett annat exempel:

 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 Assignment Operator

Java assignment operator är en av de vanligaste operatorerna. Den används för att tilldela värdet till höger till operanden till vänster.

Exempel på Java Assignment Operator

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

Produktion:

 14 16 

Exempel på Java Assignment Operator

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

Produktion:

siffror för alfabetet
 13 9 18 9 

Java Assignment Operator Exempel: Lägger till kort

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

Produktion:

 Compile time error 

Efter typgjutning:

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

Produktion:

 20 

Du kanske också gillar

Operatörsväxling i Java