Operators In Java
A programmer generally wants to do some operations in a program. To do so programmer needs some operators to carry out those operations in the program. An operator is a symbol that performs an operation. Operator acts on some variables, called operands to get the desired result.
Types Of Operators
- Arithmetic Operators
- Assignment Operators
- Relational Operators
- Logical Operators
- Boolean Operators
Arithmetic Operators
These are used to perform fundamental arithmetic operations like addition, subtraction, multiplication, etc. There are five types of arithmetic operations in java and since these operators acts on two operands at a time they are called as binary operators
.
Operator | Meaning | Example |
---|---|---|
+ | Addition Operator | a + b |
- | Subtraction Operator | a - b |
* | Multiplication Operator | a * b |
/ | Division Operator | a / b |
% | Modulus Operator | a % b |
Assignment Operators (=)
These operators are used to store some value into a variable. It is used in 3 ways
- It is used to store value into a variable
int x = 5;
- It is used to store value of a variable into another variable
int x = y; // here the value of y is stored into x
- It is used to store the value of an expression into a variable
int x = y + z - 3; // here the expression y+z-4 is evaluated and stored into x
Compact Notation
While using assignment operator (=), sometimes we may have to use same variable at both the sides of the operator. In such cases we can eliminate repetition of the variable and use compact notation or shortcut notation as shown below
Expanded Notation | Compact Notation | Name of Operator |
---|---|---|
x = x + 5 | x + = 5 | += is called additional assignment operator |
x = x * 5 | x * = 5 | *= is called multiplication assignment operator |
x = x - 5 | x - = 5 | -= is called subtraction assignment operator |
x = x / 5 | x / = 5 | /= is called division assignment operator |
x = x % 5 | x % = 5 | %= is called modulus assignment operator |
Relational Operators
These are used for the purpose of comparing. There are 6 types of relational operators
>
greater than operator<
less than operator==
equal to operator>=
greater than or equal to operator<=
less than or equal to operator!=
not equal to operator
The main use of relational operators is in the construction of conditions in statements. Arithmetic operators are for storing values into the variables and relational operators are used for comparing the two quantities.
Logical Operators
Logical operators are used to construct compound conditions. A compound condition is a combination of several simple conditions. Logical operators are of 3 types
&& (and) Operator
Example:
if(x < y && y > z) System.out.println("Yes");
In the above case if both the cases are true then only Yes
will be displayed as output.
|| (or) Operator
Example:
if(x == 1 || y == 1) System.out.println("Yes");
In the above case if either of the x
or y
value becomes equal to 1, Yes
will be displayed as output.
! (not) Operator
Example:
if(!(str1.equals(str2))) System.out.println("Not equal");
In the above case if string 1 str1
is not equal to string 2 str2
then only Not equal
will be displayed as output.
Boolean Operators
These operators act on boolean variables and produce boolean type results. There are 3 boolean operators
&
boolean and operator|
boolean or operator!
boolean not operator
Example:
boolean a,b // declare two boolean type variables
a = true; // store boolean value true into a
b = false; // store boolean value false into b
a & b // returns false and `&` gives true only if both are true
a | b // returns true and `|` gives true if any one is true
b | b // returns false since both are false
a & a // returns true since both are true
!a // gives false
!b //gives true
Boolean !
operator converts true to false and false to true.
Boolean |
operator returns true if any one of the variable is true.
Boolean &
operator returns true if both the variables are true.