Operators in Java
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;
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;
float x = 2.0, y = 33.2, z;
z = x * y;
double x = 44, y = 22, z;
z = x / y;
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 integerSystem.out.println(a+" + "+ b + " = "+c); // 100 + 33 = 133c = a - b; //substracting b from aSystem.out.println(a+" - "+ b + " = "+c); // 100 - 33 = 67c = a * b; //multiplying two operandsSystem.out.println(a+" * "+ b + " = "+c); // 100 * 33 = 3300c = a / b; //dividing a by bSystem.out.println(a+" / "+ b + " = "+c); // 100 / 33 = 3c = a % b; //dividing a by b and getting remainder - modulus operatorSystem.out.println(a+" % "+ b + " = "+c); // 100 % 33 = 1double x = 3.33, y = 4.4, z;z = x + y;// adding two operands of type doubleSystem.out.println(x+" + "+ y + " = "+z); // 3.33 + 4.4 = 7.73z = x % y;// adding two operands of type doubleSystem.out.println(x+" % "+ y + " = "+z); //3.33 % 4.4 = 3.33int m = 4;c = -m;// negating m and assigning to aSystem.out.println(m+" : " +c); // 4 : -4//compound operatorsa += b; // a = a + b; here a = 100 and b = 33; a = 100 + 33 = 133System.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 (&) | Decimal | bit pattern |
---|---|---|
00101101 | 45 | |
& | 00101011 | 43 |
00101001 | 41 |
Bitwise OR (|) | Decimal | bit pattern |
---|---|---|
00101101 | 45 | |
| | 00101011 | 43 |
00101111 | 47 |
Bitwise XOR (^)Decimalbit pattern 0010110145 ^0010101143 000001106
43 >> 2 results in 10
OR
00101011 >> 2 results in 00001010 (two zeros are added to the left and two rightmost bits are removed)
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: 22if 99 is shifted left by 3 then it will be: 79288 | 99 results in: 12388 & 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 = falseSystem.out.println(a+" > " + b+" = "+(a > b));// a == b = falseSystem.out.println(a+" < " + b+" = "+(a < b));// a == b = trueSystem.out.println(a+" != " + b+" = "+(a!=b));// a == b = true}}
Output
Relational Operators...88 == 99 = false88 > 99 = false88 < 99 = true88 != 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)));// falseSystem.out.println("here true AND true is true, "+((4 == 4) && (5 == 5)));// trueSystem.out.println("here false OR false is false, "+ ((3 == 4) || (4 == 5)));// falseSystem.out.println("here false OR true is true, "+((4 == 41) || (5 == 5)));// trueSystem.out.println("! --> not, it make true to false and false to true, "+!(4 == 41));// true}}
Output
Logical Operators...33 == 55 results in : falsehere fase AND true is fals,falsehere true AND true is true, truehere false OR false is false, falsehere 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
Loops in Java
Arrays in Java