Java Operators
Java Operators are used to perform mathematical or logical operations.
There are following types of Operators in Java :
- Unary Operators
- Arithmetic Operators
- Relational Operators
- Bitwise Operators
- Logical Operators
- Ternary Operators
- Assignment Operators
- Instanceof Operators
1. Unary Operators
Unary operators need only one operand. They are used to increment or decrement a value.
There are two types of Unary Operators :
- Postfix
- Prefix
Postfix : In Postfix value is firstly used to compute the result then increment or decrement the value by 1.
Prefix : In Prefix value is firstly increment or decrement then compute the result value by 1.
Example :public class Test { public static void main(String[] args) { int i = 10; // postfix i++; System.out.println(i);// 11 // prefix ++i; System.out.println(i);// 12 // postfix i--; System.out.println(i);// 11 // prefix --i; System.out.println(i);// 10 } }
2. Arithmetic Operators
Arithmetic Operators are used to perform mathematical operations.
Arithmetic operators like :
- +
- -
- *
- /
- %
public class Test { public static void main(String[] args) { int i = 10; int j = 5; // It is used to perform addition System.out.println(i + j);// 15 // It is used to perform subtraction System.out.println(i - j);// 5 // It is used to perform multiplication System.out.println(i * j);// 50 // It is used to perform division System.out.println(i / j);// 2 // It is used to get reminder System.out.println(i % j);// 0 } }
3. Relational Operators
Relational Operators are used to perform comparison between two vriables or it is used to compare two values.
It always return true or false based on condition.
Relational operators like :
- < less than
- > greater than
- <= less than or equal to
- >= greater than or equal to
- == equal to
- != not equal to
public class Test { public static void main(String[] args) { int i = 10; int j = 5; // true if i is less than j System.out.println(i < j);// false // true if i is greater than j System.out.println(i > j);// true // true if i is less than or equal to j System.out.println(i <= j);// false // true if i is greater than or equal to j System.out.println(i >= j);// true // true if i is equal to j System.out.println(i == j);// false // true if i is not equal to j System.out.println(i != j);// true } }
4. Bitwise Operators
Bitwise Operators are used to perform operation on bits (binary) numbers and also can apply with boolean.
In this case both expression can cheack either one condition is true or false.
Bitwise operators like :
- & Bitwise AND
- | Bitwise OR
- ^ Bitwise XOR
- ~ Bitwise NOT
public class Test { public static void main(String[] args) { int i = 10; int j = 5; //10 in bits or binary // 1010 //5 in bits or binary // 0101 //&(AND) | |(OR) rules // 0 & 1 =0 | 0 | 1 =1 // 0 & 0 =0 | 0 | 0 =0 // 1 & 0 =0 | 1 | 0 =1 // 1 & 1 =1 | 1 | 1 =1 //in & both are true then true other wise false //in | both are false then false other wise true // 1010 // 0101 // 0000 System.out.println(i&j);//0; // 1010 // 0101 // 1111 // 2^3+2^2+2^1+2^0 // 8+4+2+1=15 System.out.println(i|j);//15; System.out.println(i^j);//15 System.out.println(~i);//-11; System.out.println(~j);//-6; } }
5. Logical Operators
Logical operators are used to determine the logic between variables or values.
Logical Operators are used with boolean not for bits.
Logical Operators like :
- && Logical AND
- || Logical OR
- ! Logical NOT
public class Test { public static void main(String[] args) { int i = 10; int j = 5; System.out.println(i > j && i < j);// false System.out.println(i > j || i > j);// true System.out.println(!(i > j || i > j));// false } }
6. Ternary Operators
Ternary operator is a shorthand version of if-else statement.
(condition)?"true statement":"false statement";
Example :public class Test { public static void main(String[] args) { String msg = (10 > 5) ? "right" : "wrong"; System.out.println(msg);// right int result1 = (10 > 5) ? 10 : 5; System.out.println(result1);// 10 float result2 = (10 > 5) ? 10.0f : 5.0f; System.out.println(result2);// 10.0 boolean result3 = (10 > 5) ? true : false; System.out.println(result3);// true // you can have a nested condition int result4 = (10 > 5) ? (10 != 0) ? 10 : 0 : 5; System.out.println(result4);// 10 } }
7. Assignment Operators
Assignment Operators are used to assign a value to a variable.
Assignment Operators Like :
- =
- +=
- -=
- *=
- /=
- %=
public class Test { public static void main(String[] args) { int i = 10; int j = 5; int k = i + j; System.out.println(k); // 15 i += j; // or i=i+j; System.out.println(i); // 15 i -= j; // or i=i-j; System.out.println(i); // 10 i *= j; // or i=i*j; System.out.println(i); // 50 i /= j; // or i=i/j; System.out.println(i); // 10 i %= j; // or i=i%j; System.out.println(i); // 0 } }
8. Instanceof Operators
Instanceof operator in java is used to check whether the object is an instance of the specified type or not.
In java instanceof is a predefine keyword.
Return true if object is belongs to this type (class or interface).
Example :class Animal { } class Dog extends Animal { } class Lion extends Animal { } public class Test { public static void main(String[] args) { Animal animal = new Dog(); System.out.println(animal instanceof Dog); // true animal = new Lion(); System.out.println(animal instanceof Lion); // true } }
No comments:
Post a Comment