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.
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.
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 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;
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
z = 88;
x = ++z; here, z is incremented first, it becomes 89 and assigned to 89 to x;
z = z +1 ;
x = z;
z = 100;
x = z++; here, x is assigned value of z, that is 100, then only z is incremented
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 can be applied to integer types only. Bitwise operators act upon the individual bits of their operands.
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 |
43 >> 2 results in 10
00101011 >> 2 results in 00001010 (two zeros are added to the left and two rightmost bits are removed)
43 << 2 results in 10
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); } }
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 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.
int x = 33, y = 44;
boolean flag = x == y; //returns false
int x = 33, y = 44;
boolean flag = x != y; //returns true
int x = 33, y = 44;
boolean flag = x > y; //returns false
int x = 33, y = 44;
boolean flag = x < y; //returns true
int x = 33, y = 44;
boolean flag = x >= y; //returns false
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 } }
Relational Operators... 88 == 99 = false 88 > 99 = false 88 < 99 = true 88 != 99 = true
Logical operators only operate on Boolean operands and returns Boolean value.
int x = 4,y= 5,m=3,n=5;
boolean flag = (x == y) && (n>m); //false && true --> false
int x = 4,y= 5,m=3,n=5;
boolean flag = (x == y) || (n>m); //false && true --> 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 } }
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
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;
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); } }
K :0