Java Operators

Java Operators

An operator is used to perform an operation over one or more operands. It is a character that represents an action, for example * is an arithmatic operator that represents multiplication.

Operator in Java are classified as:-

  1. Assignment Operator
  2. Arithmetic Operator
  3. Auto-increment and Auto-decrement Operator
  4. Logical Operator
  5. Relation Operator
  6. Bitwise Operator
  7. Ternary Operator

1) Assignment Operator:

Assignments operators in java are: =, +=, -=, *=, /=, %=

Operator Description
= It is used to assign the value on the right hand side to the operand on the left. Example: int a=6; The value 6 is assigned to the variable ‘a’
+= Performs addition & assigns the result to the left hand side. Example: a+=6; //(a=a+6)
-= Performs subtraction and assigns the result to the right hand side. Example: a-=6; //(a = a-6)
*= Performs multiplication and assigns the result to the right hand side. Example: a*=6; //(a = a*6)
/= Performs division and assigns the result to the right hand side. Example: a/=6; //(a = a/6)
%= Performs division and assigns the remainder to the right hand side. Example: a%=6; //(a = a%6)
Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AssignmentOperatorDemo</span> </span>{
   <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String args[])</span> </span>{
      <span class="hljs-keyword">int</span> a = <span class="hljs-number">10</span>;
      <span class="hljs-keyword">int</span> b = <span class="hljs-number">20</span>;

      b = a;
      System.out.println(<span class="hljs-string">"= Output: "</span>+b);

      b += a;
      System.out.println(<span class="hljs-string">"+= Output: "</span>+b);
         
      b -= a;
      System.out.println(<span class="hljs-string">"-= Output: "</span>+b);
         
      b *= a;
      System.out.println(<span class="hljs-string">"*= Output: "</span>+b);
         
      b /= a;
      System.out.println(<span class="hljs-string">"/= Output: "</span>+b);
         
      b %= a;
      System.out.println(<span class="hljs-string">"%= Output: "</span>+a);
   }
}
Output

= Output: 10
+= Output: 20
-= Output: 10
*= Output: 100
/= Output: 10
%= Output: 0

2) Arithmetic Operator:

Arithmetic Operator are used to perform arithmetic operations over two operands. Basic arithmetic operators are: +, -, *, /, %

Operator Description
+ Adds both of the operands and returns the result. Example: int a = 10, b=2, c; c = a + b; //c = 12
Subtracts the right hand side operand from the left hand side operand. Example: c = a – b; //c = 2
* Multiplies both the operands and returns the result. Example: c = a * b; //c = 20
/ Divides left hand side operand by the right hand side operand. Example: c = a / b; //c =5
% Divides left hand side operand by the right hand side operand and returns the reminder. Example: c = a % b; //c = 0
Example:

1
2
3
4
5
6
7
8
9
10
11
12
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ArithmeticOperatorDemo</span> </span>{
   <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String args[])</span> </span>{
      <span class="hljs-keyword">int</span> a = <span class="hljs-number">80</span>;
      <span class="hljs-keyword">int</span> b = <span class="hljs-number">10</span>;

      System.out.println(<span class="hljs-string">"a + b: "</span> + (a + b) );
      System.out.println(<span class="hljs-string">"a - b: "</span> + (a - b) );
      System.out.println(<span class="hljs-string">"a * b: "</span> + (a * b) );
      System.out.println(<span class="hljs-string">"a / b: "</span> + (a / b) );
      System.out.println(<span class="hljs-string">"a % b: "</span> + (a % b) );
   }
}
Output:

a + b: 90
a – b: 70
a * b: 800
a / b: 8
a % b: 0

3) Auto-increment and Auto-decrement Operators:

Auto-increment operator is: ++ and auto-decrement operator is: –.
a++ is equivalent to a=a+1.
a– is equivalent to a=a-1.

Example:

1
2
3
4
5
6
7
8
9
10
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AutoOperatorDemo</span> </span>{
   <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String args[])</span></span>{
      <span class="hljs-keyword">int</span> a=<span class="hljs-number">10</span>;
      <span class="hljs-keyword">int</span> b=<span class="hljs-number">20</span>;
      a++;
      b--;
      System.out.println(<span class="hljs-string">"a++ is: "</span>+a);
      System.out.println(<span class="hljs-string">"b-- is: "</span>+b);
   }
}
Output:

a++ is: 11
b– is: 19

4) Logical Operators:

Logical Operators are used with binary variables. They are mainly used in conditional statements and loops for evaluating a condition.
Logical operators in java are:

  1. Logical AND (&&),
  2. Logical OR (||),
  3. Logical NOT (!)
Operator Description
Logical AND If both the operands are true, then the condition evaluates to true. Otherwise as false. Consider if X = true, Y = true, then result is true.
Logical OR If any of the operand is true, then the condition evaluates to true. Otherwise as false. Consider if X = true, Y = true, then result is true.
Logical NOT It would result the opposite result i.e. if X = true then !X = False
Example:

1
2
3
4
5
6
7
8
9
10
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">LogicalOperatorDemo</span> </span>{
   <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String args[])</span> </span>{
      <span class="hljs-keyword">boolean</span> X = <span class="hljs-keyword">true</span>;
      <span class="hljs-keyword">boolean</span> Y = <span class="hljs-keyword">false</span>;

      System.out.println(<span class="hljs-string">"X &amp;&amp; Y: "</span> + (X&amp;&amp;Y));
      System.out.println(<span class="hljs-string">"X || Y: "</span> + (X||Y));
      System.out.println(<span class="hljs-string">"!(X &amp;&amp; Y): "</span> + !(X&amp;&amp;Y));
   }
}
Output:

X && Y: false
X || Y: true
!(X && Y): true

5) Relational Operator:

Relational operators are used to compare the values of two operands. We have six relational operators in Java: ==, !=, >, <, >=, <=.

Operator Description
== Checks whether values of both side of the operands are equal.
!= Checks whether values of both the operands are unequal. Example: check = (a! = b); // true if ‘a’ is not equal to ‘b’
> Checks whether the operand in the left hand side is greater than the right hand side operator. Example: int a = 5, b = 2; (a > b) would return true.
< Checks whether the operand in the left hand side is less than the right hand side operator. Example: int a = 5, b = 2; (a < b) would return false.
>= Checks whether the operand in the left hand side is greater than or equal to the right hand side operator. Example: int a = 2, b = 2; (a >= b) would return true.
<= Checks whether the operand in the left hand side is less than or equal to the right hand side operator. Example: int a = 3, b = 2; (a <= b) would return false.
NOTE: This example is using if-else statement which is our next tutorial, if you are finding it difficult to understand then refer if-else in Java.
Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RelationalOperatorDemo</span> </span>{
   <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String args[])</span> </span>{
      <span class="hljs-keyword">int</span> a = <span class="hljs-number">10</span>;
      <span class="hljs-keyword">int</span> b = <span class="hljs-number">50</span>;
      <span class="hljs-keyword">if</span> (a==b) {
     System.out.println(<span class="hljs-string">"a and b are equal"</span>);
      }
      <span class="hljs-keyword">else</span>{
     System.out.println(<span class="hljs-string">"a and b are not equal"</span>);
      }

      <span class="hljs-keyword">if</span>( a != b ){
     System.out.println(<span class="hljs-string">"a and b are not equal"</span>);
      }
      <span class="hljs-keyword">else</span>{
     System.out.println(<span class="hljs-string">"a and b are equal"</span>);
      }

      <span class="hljs-keyword">if</span>( a &gt; b){
     System.out.println(<span class="hljs-string">"a is greater than b"</span>);
      }
      <span class="hljs-keyword">else</span>{
     System.out.println(<span class="hljs-string">"a is not greater than b"</span>);
      }

      <span class="hljs-keyword">if</span>( a &gt;= b ){
     System.out.println(<span class="hljs-string">"a is greater than or equal to b"</span>);
      }
      <span class="hljs-keyword">else</span>{
     System.out.println(<span class="hljs-string">"a is less than b"</span>);
      }

      <span class="hljs-keyword">if</span>( a &lt; b ){
     System.out.println(<span class="hljs-string">"a is less than b"</span>);
      }
      <span class="hljs-keyword">else</span>{
     System.out.println(<span class="hljs-string">"a is not less than b"</span>);
      }

      <span class="hljs-keyword">if</span>( a &lt;= b){
     System.out.println(<span class="hljs-string">"a is less than or equal to b"</span>);
      }
      <span class="hljs-keyword">else</span>{
     System.out.println(<span class="hljs-string">"a is greater than b"</span>);
      }
   }
}
Output:

a and b are not equal
a and b are not equal
a is not greater than b
a is less than b
a is less than b
a is less than or equal to b

6) Bitwise Operator:

There are six bitwise Operators:

  1. Bitwise complement (~)
  2. Bitwise AND (&)
  3. Bitwise inclusive OR (|)
  4. Bitwise exclusive OR (^)
  5. Left shift (<<)
  6. Right shift (>>)
Operator Description
Bitwise complement This operator inverts a bit pattern making 0’s to 1 and 1’s to 0. Example: 00001100 would be change to 11110011
Bitwise AND Performs bitwise AND operation. Example: consider x = 15, y = 6, (x = 0000 1111, y = 0000 0110), x&y = 6 (0000 0110)
Bitwise inclusive OR Performs bitwise OR operation. Example: consider x = 14, y = 12 (x = 0000 1110, y = 0000 1100), result is: 14 (0000 1110)
Bitwise exclusive OR Performs bitwise XOR operation. Example: consider x = 15, y = 12 (x = 0000 1111, y = 0000 1100) x^y = 3 (0000 0011)
Left shift The operand’s value is moved left by the number of bits specified in the right side of the operator <<. Example: consider x = 7 (0000 0111), x<<2 = 24 (0001 1100)
Right shift The operand’s value is moved right by the number of bits specified in the right side of the operator >>. Example: consider x = 7 (0000 0111), x>>2 = 1 (0001)
Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BitwiseOperatorDemo</span> </span>{
  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String args[])</span> </span>{

     <span class="hljs-keyword">int</span> a = <span class="hljs-number">11</span>;  <span class="hljs-comment">/* 11 = 00001011 */</span>
     <span class="hljs-keyword">int</span> b = <span class="hljs-number">22</span>;  <span class="hljs-comment">/* 22 = 00010110 */</span>
     <span class="hljs-keyword">int</span> result = <span class="hljs-number">0</span>;

     result = a &amp; b;  
     System.out.println(<span class="hljs-string">"a &amp; b: "</span>+result);

     result = a | b;  
     System.out.println(<span class="hljs-string">"a | b: "</span>+result);
   
     result = a ^ b;  
     System.out.println(<span class="hljs-string">"a ^ b: "</span>+result);
   
     result = ~a;  
     System.out.println(<span class="hljs-string">"~a: "</span>+result);
   
     result = a &lt;&lt; <span class="hljs-number">2</span>;  
     System.out.println(<span class="hljs-string">"a &lt;&lt; 2: "</span>+result);
     
     result = a &gt;&gt; <span class="hljs-number">2</span>;  
     System.out.println(<span class="hljs-string">"a &gt;&gt; 2: "</span>+result);
  }
}
Output

a & b: 2
a | b: 31
a ^ b: 29
~a: -12
a << 2: 44
a >> 2: 2

7) Ternary Operator:

This operator evaluates a boolean expression and assign the value based on the result.

Syntax:

1
variable num1 = (expression) ? value <span class="hljs-keyword">if</span> <span class="hljs-keyword">true</span> : value <span class="hljs-keyword">if</span> <span class="hljs-keyword">false</span>

If the expression results true then the first value before the colon (:) is assigned to the variable num1 else the second value is assigned to the num1.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TernaryOperatorDemo</span> </span>{

   <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String args[])</span> </span>{
        <span class="hljs-keyword">int</span> num1, num2;
        num1 = <span class="hljs-number">25</span>;
        <span class="hljs-comment">/* num1 is not equal to 10 that's why
     * the second value after colon is assigned
     * to the variable num2
     */</span>
    num2 = (num1 == <span class="hljs-number">10</span>) ? <span class="hljs-number">100</span>: <span class="hljs-number">200</span>;
    System.out.println( <span class="hljs-string">"num2: "</span>+num2);

    <span class="hljs-comment">/* num1 is equal to 25 that's why
     * the first value is assigned
     * to the variable num2
     */</span>
    num2 = (num1 == <span class="hljs-number">25</span>) ? <span class="hljs-number">100</span>: <span class="hljs-number">200</span>;
    System.out.println( <span class="hljs-string">"num2: "</span>+num2);
   }
}
Output:

num2: 200
num2: 100

Related Post

New features in XCode 10 Supported Configurations Xcode 10 beta requires a Mac running macOS 10.13.4 or later Xcode 10 beta includes SDKs for iOS 12, watchOS 5, macOS ...
Dart Introduction | Dart programming language Dart Introduction Dart is a scalable language that you can use to write simple scripts or full featured apps. Whether you’re creating a mobile app, w...
Getting started with C Getting started with C : If we want to communicate with our computer it is very much needed that our computer understands what we want to say to it. ...
Dart Libraries | Dart Tutorial Dart Libraries Dart has the following libraries, which are included in all Dart platforms: dart:core This library is automatically imported into ev...
Java If-else Statement Java If-else Statement The Java if statement is used to test the condition. It checks boolean condition: true or false. For example, if a number i...
Switch Case Statement Switch Case Statement The switch statement is a multi-way branch statement. It provides an easy way to dispatch execution to different parts of c...

Leave a Reply

Your email address will not be published. Required fields are marked *

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.