Operator performs operations. In java, there are 7 types of operators:
Arithmetic Operators
These operators are used in mathematics calculations. Arithmetic operands are:
Operator | Description |
---|---|
+ | addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus (Reminder) |
++ | Increase by 1 |
-- | Decrease by 1 |
Example:
- package flowkl;
-
- public class Tutorials
- {
- public static void main(String args[])
- {
- //Arithmetic operators
- int a =5;
- int b = 6;
- // c= 11
- int c = a + b;
- // print 11
- System.out.println(c);
- // print 11 - 5
- System.out.println(c-a);
- // print 11/5
- System.out.println(c/a);
- // print 11 % 5
- System.out.println(c%a);
- // print 5
- System.out.println(a);
- // increase a by 1
- a++;
- // print a again, a = 6
- System.out.println(a);
- // decrease a
- a--;
- // print a again, a = 5
- System.out.println(a);
- }
- }
Output:
- 11
- 6
- 2
- 1
- 5
- 6
- 5
Relational Operators
Relational operators are used to compare one operand to another. These compares two operands and return true if comparison is right otherwise false.
Operators | Descriptors |
---|---|
== | both are equal |
!= | both are not equal |
< | left is smaller |
> | right is smaller |
<= | left is smaller or equal |
>= | right is smaller or equal |
Example:
- package flowkl;
-
- public class Tutorials
- {
- public static void main(String args[])
- {
- System.out.println(8==9);
- System.out.println(8!=9);
- System.out.println(8<9);
- System.out.println(8>9);
- System.out.println(8<=9);
- System.out.println(8>=9);
- }
- }
Output:
- false
- true
- true
- false
- true
- false
Bitwise Operators:
Bitwise operators performs operations on individual bits in an integer.
int a = 20 ( in binary, a = 10100) and b = 50 ( in binary, 110010). These operators will change these numbers in binary and will operate functions.
Operator | Description |
---|---|
& | And Operator |
| | Or Operator |
^ | XOR operator |
<< | Left operator |
>> | Right operator |
>>> | Shift right zero fill operator |
Example:
- package flowkl;
-
- public class Tutorials
- {
- private void method1(String name)
- {
- System.out.printf("name is %s \n",name);
- }
-
- private void method2(String name, int age)
- {
- System.out.printf("name is %s and age is %d \n",name,age);
- }
-
- public static void main(String args[])
- {
- Tutorials object = new Tutorials();
- object.method1("harish");
- object.method2("harish", 20);
- }
- }
Output:
- 16
- 54
- 38
- 80
- 5
- 5
Logical Operators
These are boolean operators return true or false. These are same as logical gates.
Operator | Description |
---|---|
&& | Logical AND operator |
|| | Logical OR operator |
! | Logical NOT operator |
Example: 3 && 4 is true and 3 && 0 is false. !0 is true and !3 is false. Try to print all operators.
Assignment Operators
Operators | Example |
---|---|
= | a = 10 |
+= | a+=4 is equivalent to a = a + 4 |
-= | a-=4 is equivalent to a = a - 4 |
/= | a/=4 is equivalent to a = a / 4 |
%= | a%=4 is equivalent to a = a %+ 4 |
<<= | a<<=4 is equivalent to a = a << 4 |
>>= | a>>=4 is equivalent to a = a >> 4 |
&= | a&=4 is equivalent to a = a & 4 |
^= | a^+=4 is equivalent to a = a ^ 4 |
|= | a|=4 is equivalent to a = a | 4 |
Misc Operators:
This operator check a condition if condition is true then it will do first work otherwise it will do alternative work.
Syntax is : variable x =(condition)? if_true_work: if_false_work
Example:
- package flowkl;
-
- public class Tutorials
- {
- public static void main(String args[])
- {
- int b = 3;
- // condition is b == 3 or not
- // if b == 3 then a = 4
- // otherwise a = 9
- int a =(b == 3)? 4 : 9;
- System.out.print(a);
- }
- }