Java Operators
we use the + operator to add together two values:
it can also be used to add together a variable and a value, or a variable and another variable:
Java Operators
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Bitwise operators
Arithmetic operators
a + b = 17
a - b = 7
a * b = 60
a / b = 2
a % b = 2
Assignment operators
Assignment operators are used in Java to assign values to variables. For example,
int age;
age = 5;
Here, = is the assignment operator. It assigns the value on its right to the variable on its left. That is, 5 is assigned to the variable age.
EXAMPLE
output:
var using =: 4
var using +=: 8
var using *=: 32
Comparison operators
Relational operators are used to check the relationship between two operands. For example,
// check is a is less than b
a < b;
Here, > operator is the relational operator. It checks if a is less than b or not.
It returns either true
or false
.
Note: Relational operators are used in decision making and loops.
Logical operators
Logical operators are used to check whether an expression is true
or false
. They are used in decision making.
Working of Program
(5 > 3) && (8 > 5)
returnstrue
because both(5 > 3)
and(8 > 5)
aretrue
.(5 > 3) && (8 < 5)
returnsfalse
because the expression(8 < 5)
isfalse
.(5 < 3) || (8 > 5)
returnstrue
because the expression(8 > 5)
istrue
.(5 > 3) && (8 > 5)
returnstrue
because the expression(5 > 3)
istrue
.(5 > 3) && (8 > 5)
returnsfalse
because both(5 < 3)
and(8 < 5)
arefalse
.!(5 == 3)
returns true because5 == 3
isfalse
.!(5 > 3)
returns false because5 > 3
istrue
.
Bitwise operators
Bitwise operators in Java are used to perform operations on individual bits.