logo

Оператори в Java

Оператор в Java е символ, който се използва за извършване на операции. Например: +, -, *, / и т.н.

Има много видове оператори в Java, които са дадени по-долу:

  • Унарен оператор,
  • аритметичен оператор,
  • Оператор на смяна,
  • Релационен оператор,
  • Побитов оператор,
  • логически оператор,
  • Тернарен оператор и
  • Оператор за присвояване.

Предимство на Java оператор

Тип операторКатегорияПредимство
Единиченпостфикс <em>expr</em> ++ <em>expr</em> --
префикс++ <em>expr</em> -- <em>expr</em> + <em>expr</em> - <em>expr</em> ~ !
Аритметикамултипликативен* / %
добавка+ -
Shiftсмяна&lt;&gt; &gt;&gt;&gt;
Релационнисравнение = instanceof
равенство== !=
Побитовопобитово И&amp;
побитово изключително ИЛИ^
побитово включително ИЛИ|
Логичнологично И&amp;&amp;
логическо ИЛИ||
Троичентроичен? :
Възлаганезадание= += -= *= /= %= &amp;= ^= |= &lt;&gt;= &gt;&gt;&gt;=

Унарен оператор на Java

Унарните оператори на Java изискват само един операнд. Унарните оператори се използват за извършване на различни операции, т.е.

  • увеличаване/намаляване на стойност с единица
  • отричане на израз
  • обръщане на стойността на булева стойност

Пример за унарен оператор на Java: ++ и --

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

Изход:

сортирайте arraylist в java
 10 12 12 10 

Java унарен оператор Пример 2: ++ и --

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

Изход:

 22 21 

Пример за унарен оператор на Java: ~ и !

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

Изход:

 -11 9 false true 

Аритметични оператори на Java

Аритметичните оператори на Java се използват за извършване на събиране, изваждане, умножение и деление. Те действат като основни математически операции.

Пример за аритметичен оператор на 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 }} 

Изход:

 15 5 50 2 0 

Пример за аритметичен оператор на Java: израз

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

Изход:

 21 

Ляв оператор на Java

Операторът за ляво преместване << на Java се използва за изместване на всички битове в дадена стойност вляво определен брой пъти.

Пример за ляв оператор на 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);>

Оператор за дясна смяна на Java

Операторът за десен преместване >> на Java се използва за преместване на стойността на левия операнд надясно с броя битове, определени от десния операнд.

Пример за оператор за десен Shift в Java

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

Изход:

 2 5 2 

Пример за оператор за преместване в Java: >> срещу >>>

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

Изход:

 5 5 -5 1073741819 

Пример за Java И оператор: Логически && и Побитово &

Логическият && оператор не проверява второто условие, ако първото условие е невярно. Той проверява второто условие само ако първото е вярно.

Операторът побитово & винаги проверява и двете условия дали първото условие е вярно или невярно.

 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 И оператор: Логически && срещу побитово &

 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 оператор ИЛИ Пример: Логически || и побитово |

Логичното || операторът не проверява второто условие, ако първото условие е вярно. Той проверява второто условие само ако първото е невярно.

Побитово | операторът винаги проверява и двете условия дали първото условие е вярно или невярно.

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

Изход:

 true true true 10 true 11 

Тернарен оператор на Java

Операторът Java Ternary се използва като заместител на един ред за израз if-then-else и се използва много в програмирането на Java. Това е единственият условен оператор, който приема три операнда.

токенизатор на низове java

Пример за троичен оператор на 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;>

Друг пример:

 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 оператор за присвояване

Операторът за присвояване на Java е един от най-често срещаните оператори. Използва се за присвояване на стойността отдясно на операнда отляво.

Пример за оператор за присвояване на 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); }} 

Изход:

 14 16 

Пример за оператор за присвояване на 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); }} 

Изход:

 13 9 18 9 

Пример за оператор за присвояване на Java: Добавяне на кратко

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

Изход:

 Compile time error 

След преобразуване на типа:

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

Изход:

 20 

Може да харесате още

Преместване на оператора в Java