Online-Academy

Look, Read, Understand, Apply

Menu

Operators

Operators

There are several operators in Java, and are divided into four groups: arithmetic, bitwise, relational, and logical.

Unary Operator:Operator which takes only one operand is called Unary operator; minus (-), increment (++) and decrement (--) are Unary operators.

Binary Operator: Operator which takes two operands is called binary operator; addition (+), multiplication (*), subtraction (-), greater than (>), less than (<), and (&&) or (||), bitwise and (&) etc. are binary operators.

The basic Arithmetic Operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, division, multiplication, getting remainder. In addition to these basic operations, operators for increment, decrement are also defined. These operators work on numeric data type only. Numbers are negated with unary minus operator.

  • Addition (+): is a binary operator for adding two numbers.

    int a = 44, b = 345, c;

    c = a + b;

  • Subtraction (-) : is a binary operator for subtracting a number from another number.

    int a = 23, b = 12, c;

    c = a - b;

    Minus is used to negate a number also, it takes only one value while negating, it is unary operator in this case.

    int x = -44;

  • Multiplication (*): is used to multiple a number by another number;

    float x = 2.0, y = 33.2, z;

    z = x * y;

  • Division (/): is used to divide a number by another number.

    double x = 44, y = 22, z;

    z = x / y;

  • Modulus (%): is used to get remainder when a number is divided by another number.

    int x = 33, y = 7, z;

    z = x % y; value of z will be 5.

    z = 102 % y;

Arithmetic Compound Assignment Operators

Arithmetic operator and assignment (=) operators are combined to for compound assignment operator. For example,

int x = 4, y = 5; x += y;

x+= y; is equivalent to x = x + y;

int x = 4, y = 5; x *= y;

x *= y; is equivalent to x = x * y;

int x = 4, y = 5; x %= y;

x%= y; is equivalent to x = x % y;

int x = 4, y = 5; x -= y;

x-= y; is equivalent to x = x - y;

Increment and Decrement operators

The ++ and -- are increment and decrement operators. The ++ operator increments its operand by one, and -- operator decrements its operand by one.

a = a + 1; is equivalent to a++;

x = x - 1; can be written as z--;

Increment and Decrement operator can precede its operand (prefix form) or succeed its operand (postfix form).

++x; //prefix form

y--; //postfix form

Prefix notation

z = 88;

x = ++z; here, z is incremented first, it becomes 89 and assigned to 89 to x;

Prefix notation is equivalent to following expressions

z = z +1 ;

x = z;

Postfix notation

z = 100;

x = z++; here, x is assigned value of z, that is 100, then only z is incremented

Postfix notation is equivalent to following expressions.

x = z;

z++;

class arithmetica{
	public static void main(String[] args){
		int a, b = 33, c; 
		a = 100; 
		c = a + b; //adding two operands of type integer
		System.out.println(a+" + "+ b + " = "+c);  // 100 + 33 = 133
		c = a - b; //substracting b from a
		System.out.println(a+" - "+ b + " = "+c);  // 100 - 33 = 67
		c = a * b; //multiplying two operands
		System.out.println(a+" * "+ b + " = "+c);  // 100 * 33 = 3300
		c = a / b; //dividing a by b
		System.out.println(a+" / "+ b + " = "+c);  // 100 / 33 = 3
		c = a % b; //dividing a by b and getting remainder - modulus operator
		System.out.println(a+" % "+ b + " = "+c);  // 100 % 33 = 1
		double x = 3.33, y = 4.4, z; 
		z = x + y;// adding two operands of type double
		System.out.println(x+" + "+ y + " = "+z);  // 3.33 + 4.4 = 7.73
		z = x % y;// adding two operands of type double
		System.out.println(x+" % "+ y + " = "+z); //3.33 % 4.4 = 3.33
		int m = 4; 
		c = -m;// negating m and assigning to a
		System.out.println(m+" : " +c); // 4 : -4		
		//compound operators
		a += b; // a = a + b; here a = 100 and b = 33; a = 100 + 33 = 133 
		System.out.println("a : "+a);
		a *= b; // a = a * b; 
		System.out.println("a : "+a);
		System.out.println("Value of b before ++: "+b);
		//++, -- are uniary operators, they work on single operand and increases or decreases a value by 1. 
		++b; //increament b by 1; 
		System.out.println("Value of b after ++: "+b);
		b = 78;
		System.out.println("Value of b before --: "+b);
		--b; //decreament b by 1; 
		System.out.println("Value of b after ++: "+b);
		a = 2; b = 4; c = 49; 
		int d = 7, e = 88;
		int exp = a + b * c / d - e;
		System.out.println("Value of exp = "+exp);
	}
}

Bitwise Operators

Bitwise operators can be applied to integer types only. Bitwise operators act upon the individual bits of their operands.

  • Bitwise unary NOT (~): This operator inverts each bit of the operand.

    For example, when bitwise NOT operator is applied to the number 45, having bit pattern:

    00101101 becomes 11010010

  • Bitwise AND (&): is a binary operator, acts bit by bit of the operands and results in 1 bit if both operands are also 1 otherwise 0 is produced

    Bitwise AND (&)Decimalbit pattern
    0010110145
    &0010101143
    0010100141

  • Bitwise OR : is a binary operator, acts bit by bit of the operands and results in 1 bit if any one of the two operands is 1.

    Bitwise OR (|)Decimalbit pattern
    0010110145
    |0010101143
    0010111147

  • Bitwise XOR (^): if any one of the bits is 1 of any one of the two operands, then it produces 1.

    Bitwise XOR (^)Decimalbit pattern 0010110145 ^0010101143 000001106

  • Right Shift (>>): This operator shifts all of the bits in an operand to the right a specified number of times. Syntax: operand >> num;

    43 >> 2 results in 10

    OR

    00101011 >> 2 results in 00001010 (two zeros are added to the left and two rightmost bits are removed)

  • Left Shift (<<):
  • This operator shifts all of the bits in an operand to the left a specified number of times. Syntax: operand << num;

    43 << 2 results in 10

    OR

    00101011 << 2 results in 10101100 (all the bits are shifted two position right and two zeros are added to the end)

class bitwise_operator{
	public static void main(String[] arggg){
		int a=88, b=99, c;
		System.out.println("Bitwise Operators...");
		c = a >> 2;
		System.out.println("if "+a+" is shifted right by 2 then it will be: "+c);
		c = b <<3; 
		System.out.println("if "+b+" is shifted left by 3 then it will be: "+c);
		c = a | b;
		System.out.println(a+" | "+b+" results in: "+c);
		c = a & b; 
		System.out.println(a+" & "+b+" results in: "+c);
	}
}

Output:

Bitwise Operators...
if 88 is shifted right by 2 then it will be: 22
if 99 is shifted left by 3 then it will be: 792
88 | 99 results in: 123
88 & 99 results in: 64

Relational Operators

Relational operators are used to determine relationships like equality and ordering between the operands. The outcome of the relational operators is Boolean value, either true or false.

  • Equal to (==):

    int x = 33, y = 44;

    boolean flag = x == y; //returns false

  • Not Equal to (!=):

    int x = 33, y = 44;

    boolean flag = x != y; //returns true

  • Greater than (>):

    int x = 33, y = 44;

    boolean flag = x > y; //returns false

  • Less than (<):

    int x = 33, y = 44;

    boolean flag = x < y; //returns true

  • Great than Equal to (>=):

    int x = 33, y = 44;

    boolean flag = x >= y; //returns false

  • Less than Equal to (<=):

    int x = 33, y = 44;

    boolean flag = x <= y; //returns true

class relational_operators{
	public static void main(String[] argss){
		int a=88, b=99, c;
		boolean flag; 
		
		System.out.println("Relational Operators...");
		flag = (a==b);
		System.out.println(a+" == " + b+" = "+flag);// a == b = false
		System.out.println(a+" > " + b+" = "+(a > b));// a == b = false
		System.out.println(a+" < " + b+" = "+(a < b));// a == b = true
		System.out.println(a+" != " + b+" = "+(a!=b));// a == b = true
	}
}

Output

Relational Operators...
88 == 99 = false
88 > 99 = false
88 < 99 = true
88 != 99 = true

Logical Operators

Logical operators only operate on Boolean operands and returns Boolean value.

  • AND: It operates on two operands and returns true if both operands are true otherwise false.

    int x = 4,y= 5,m=3,n=5;

    boolean flag = (x == y) && (n>m); //false && true --> false

  • OR: It operates on two operands and returns either true if any one of the operands is true.

    int x = 4,y= 5,m=3,n=5;

    boolean flag = (x == y) || (n>m); //false && true --> true

  • NOT: It operates on one operand and makes true to false or false to true.

    int x = 4,y= 5,m=3,n=5;

    boolean flag = !(x == y); //!false --> true

class logical_operator{
	public static void main(String[] args){
		System.out.println("Logical Operators...");
		int a = 33, b = 55;
		boolean flag = a == b;
		System.out.println(a+" == "+b+" results in : "+flag);
		System.out.println("here fase AND true is fals,"+((3 == 4) && (4 == 5)));// false
		System.out.println("here true AND true is true, "+((4 == 4) && (5 == 5)));// true
		System.out.println("here false OR false is false, "+ ((3 == 4) || (4 == 5)));// false
		System.out.println("here false OR true is true, "+((4 == 41) || (5 == 5)));// true
		System.out.println("! --> not, it make true to false and false to true, "+!(4 == 41));// true
	}
}

Output

Logical Operators...
33 == 55 results in : false
here fase AND true is fals,false
here true AND true is true, true
here false OR false is false, false
here false OR true is true, true
! --> not, it make true to false and false to true, true

Assignment Operator

A single equal (=) operator, assignment operator is used to assign value to the variable and can be chained.

int x = 33, y, q,p;

y = q = p = x;

Ternary Operator (?)

Is used to check conditions. The general syntax is

expression1? expression2 : expression2

expression1 is an expression that evaluates to a boolean value, if expression1 is true then expression2 is executed otherwise expression3 is executed. Both expression2 and expression3 should return value of the same type except void.

int z=44, a = 44;

int k = (z == a) ? 1: 0;

z is equal to a, so it evaluates to true, and 1 is returned and assigned to k.

class ternary_operator{
	public static void main(String[] args){
		int d = 77, e = 55;
			int k = (d == e)?1: 0;
		System.out.println("K :"+k);
	}
}

Output

K :0