Easy Tutorial
For Competitive Exams

Java Programming Operators Theory

Operators

An operator, in Java, is a special symbols performing specific operations on one, two or three operands and then returning a result.

Arithmetic Operators

The arithmetic operators are used to perform basic mathematical calculations.

operator Description
+ Adds two operands
- Subtract second operands from first
* Multiply two operand
/ Divide numerator by denumerator
% Remainder of division

Example:

public class Demo {
    public static void main(String[] args) {
       int x = 10;
       int y = 20;
       System.out.println("x + y = " + (x + y));
       System.out.println("x - y = " + (x - y));
       System.out.println("x * y = " + (x * y));
       System.out.println("y / x = " + (y / x));
       System.out.println("x % 3 = " + (x % 3));
     }
}
Try it Yourself

Solve this:

10743.What is the output of this program?
public class mainclass {
   public static void main(String args[]){
      int var1 = 1 + 5;
      int var2 = var1 / 4;
      System.out.print(var1 + " " + var2+" "+-8%3);
    } 
}
6 1 -2
6 7 8
6 2 1
1 2 1

Unary operators

The unary operators involve in only a single operand.

Operator Description
+ Unary plus operator; indicates positive value (default)
- Unary minus operator; negate an expression.
++ Increment operator; increments a value by 1.
-- Decrement operator; decrements a value by 1;
! Logical complement operator; inverts value of a boolean.

Example:

public class Demo {
    public static void main(String[] args) {
        int x = 10;
        int y = 20;
        System.out.println(+x);
        System.out.println(-y);
        System.out.println(++x);
        System.out.println(--y);
        boolean ok = false;
        System.out.println(ok);
        System.out.println(!ok);
     }
}
Try it Yourself

Solve this!!

10758.
public class increment {
   public static void main(String args[]) {        
      int g = 3;
      System.out.print(++g * 8);
   } 
}
30
31
32
33
10759.
public class Operators1 {
    public static void main(String[] args{
        int i=0;
        System.out.println(i++);
        System.out.println(i);
        System.out.println(++i);
    }
}
0 1 2
1 1 1
1 0 1
1 2 1
Explanation:

In the first print statement,i is initialized to 0 and then increased by 1 because it is post increment operator(first assign and then increment) but value is still 0 here.

In the second print statement,i value is 1 because its value is incremented in first statement.

In the third print statement, i value is 2 because i is incremented by pre increment operator(first increment and then assign)

so value of i is 0 1 2.

Relational Operators

The relational operators are used to compare two operands or two expressions and result is a boolean.

operator

description

==

Check if two operand are equal

!=

Check if two operand are not equal.

>

Check if operand on the left is greater than operand on the right

<

Check operand on the left is smaller than right operand

>=

check left operand is greater than or equal to right operand

<=

Check if operand on left is smaller than or equal to right operand

Example:

public class Demo {
   public static void main(String[] args) {
        int x = 10;
        int y = 20;
        System.out.println("x == y? " + (x == y));
        System.out.println("x != y? " + (x != y));
        System.out.println("x > y? " + (x > y));
        System.out.println("x >= y? " + (x >= y));
        System.out.println("x < y? " + (x < y));
        System.out.println("x <= y? " + (x <= y));
     }
}
Try it Yourself

Sove this!!

10760.what is the output of the program ?
public class Equals {
   public static void main(String [] args){
      int x = 100;
      double y = 100.1;
      boolean b = (x == y);
      System.out.println(b);
   }
}
true
false
none
0
10761.
public class Relational_operator {
   public static void main(String args[]){
      int var1 = 5; 
      int var2 = 6;
      System.out.print(var1 > var2);
   } 
}
true
false
none
0

Bitwise Operators:

These operators perform bitwise and bit shift operations on only integral types, not float types.

Bitwise operator works on bits and performs bit-by-bit operation

operator

description

&

Bitwise AND

|

Bitwise OR

^

Bitwise exclusive OR

<<

left shift

>>

right shift

Example:

public class Demo {
    public static void main(String[] args) {
        int a =60; /* 60 = 0011 1100 */
        int b =13; /* 13 = 0000 1101 */
        System.out.println("a & b = "+ (a & b ));
        System.out.println("a | b = "+ (a | b ));
        System.out.println("a ^ b = "+ (a ^ b ));
        System.out.println("~a = "+ (~a) );
        System.out.println("a << 2 = "+ (a <<2 ));
        System.out.println("a >> 2 = "+ (a >>2 ));
        System.out.println("a >>> 2 = "+ (a >>>2) );
     }
}
Try it Yourself

Sove this!!

10762.What is the output of the following program?
public class Operators1 {
    public static void main(String[] args){
        byte a=-20;
        System.out.println(~a);
    }
}
19
20
21
22

Logical Operators

Logical operators are used to combine one or more relational expressions that results in formation of complex expressions known as logical expressions.

Like relational operators, the logical operators evaluate the result of logical expression in terms of Boolean values that can only be true or false according to the result of the logical expression.

operator

description

example

&&

Logical AND

(a && b) is false

||

Logical OR

(a || b) is true

!

Logical NOT

(!a) is false

Example:

public class Test{
   public static void main(String args[]){
       boolean a =true;
       boolean b =false;
       System.out.println("a && b = "+(a&&b));
       System.out.println("a || b = "+(a||b));
       System.out.println("!(a && b) = "+!(a && b));
    }
}

Try it Yourself

Solve this!!

10763.
public class Test{
   public static void main(String args[]){
      int a=10,b=30,c=20;
      System.out.println((a<b)&&(b>c));
      System.out.println((a>b)&&(b>c));
   }
}
true false
false true
false
true
10764.
public class Test{
   public static void main(String args[]){
       int a=10,b=30,c=20;
       System.out.println((a<b)||(b>c));
       System.out.println((a>b)||(b>c));
    }
}
true false
true true
false false
false

Assignment Operators

Assignment operators are used to assign value to a variable.

operator

description

example

=

assigns values from right side operands to left side operand

a=b

+=

adds right operand to the left operand and assign the result to left

a+=b is same as a=a+b

-=

subtracts right operand from the left operand and assign the result to left operand

a-=b is same as a=a-b

*=

mutiply left operand with the right operand and assign the result to left operand

a*=b is same as a=a*b

/=

divides left operand with the right operand and assign the result to left operand

a/=b is same as a=a/b

%=

calculate modulus using two operands and assign the result to left operand

a%=b is same as a=a%b

Example:

public class Demo {
    public static void main(String[] args) {
        int a =10;
        int b =20;
        int c =15;
        System.out.println("c = a + b is "+ (c = a + b ));
        System.out.println("c += a is "+ (c += a) );
        System.out.println("c -= a is "+ (c -= a) );
        System.out.println("c *= a is "+ (c *= a) );
        System.out.println("c /= a is "+ (c /= a) );
        System.out.println("c %= a is "+ (c %= a) );
     }
}
Try it Yourself

Solve this!!

10765.
public class Demo{
   public static void main(String args[]){
       int x, y, z;
       x = 0;
       y = 1;
       x = y = z = 8;
       System.out.println(y);
    }
}
8
9
10
11

Conditional Operator (?:)

Conditional operator is also known as the ternary operator.

This operator consists of three operands and is used to evaluate Boolean expressions.

It is to decide which value should be assigned to the variable.

Syntax

variable x =(expression)? value if true: value if false

Example:

public class Demo {
    public static void main(String[] args) {
        int a =10, b;
        System.out.println("Value of b is : "+ (b =(a ==1)?20:30));
    }
}
Try it Yourself

Sove this!!

10766.What is the output of the program ?
public class Test {
   public static void main(String [] args){
       int x=20;
       String sup = (x < 15) ? "small" : "tiny";
       System.out.println(sup);
    }
}
tiny
small
small tiny
error
Share with Friends