Operators in Java
Operator is a symbol which is used to perform operation.example: +, -, *, / etc.
There are many types of operators in Java which are given below:
-
Arithmetic Operator: arithmetic operators are used to perform arithmetic operations such as addition,substraction,multiplication,division etc.
Operator | Name | Purpose | Example |
+ | Addition | adding two numbers | a=20;b=10;c=a+b; |
- | Substraction | substracting two numbers | a=20;b=10;c=a-b; |
* | Multiplication | multiplying two numbers | a=20;b=10;c=a*b; |
/ | division | dividing two numbers | a=20;b=10;c=a/b; |
% | modulo division | finding remainder of two numbers | a=20;b=10;c=a%b; |
-
Relational Operator: Relational operators are used are used for comparison between two values or expressions
Operator | Name | Purpose | Example |
< | less than | it is used to check whether the first operand is lesser than the second operand. | operand1<operand2 |
> | greater than | it is used to check whether the first operand is greater than the second operand. | operand1>operand2 |
<= | Less than Equal to | it is used to checks whether the first operand is less than or equal to the second operand. | operand1<=operand2 |
>= | Greater than Equal to | it is used to checks whether the first operand is greater than or equal to the second operand. | operand1>=operand2 |
== | Equal to | equal to operator (==) is used to check whether the two given operands are equal or not. | operand1==operand2 |
!= | NotEqual to | Not Equal to operator (!=) is used to check whether the two given operands are equal or not. | operand1!=operand2 |
-
Assignment Operator:Assignment operators are used to assigning value to variable.= is a assignment operator.
-
Logical Operator:Logical operators are used Logical operators are used to perform logical “AND”, “OR” and “NOT” operations.
Operator | Name | Purpose | Example |
< | less than | it is used to check whether the first operand is lesser than the second operand. | operand1<operand2 |
&& | Logical AND | if both operands are true execute otherwise don't execute. | operand1&&operand2 |
|| | Logical OR | if one of operand is true to execute else don’t | operand1&&operand2 |
! | LogicalNOT | if operand is true it reurns false.and vice versa. | !operand |
-
Increment/Decrement Operators: Increment operators are used to add one value to variable value.decrement operators are used to substract one value from the variable value.
Operator | Name | Purpose | Example |
< | less than | it is used to check whether the first operand is lesser than the second operand. | operand1<operand2 |
++ | Increment | it is used to adding one value to a variable. |
++a |
-- | decrement | it is used to checks whether the first operand is less than or equal to the second operand. | operand1<=operand2 |