Assignment Operator
Assignment operators are used in Java to assign values to variables. For example,
int age;
age = 5;
The assignment operator assigns the value on its right to the variable on its left. Here, 5 is assigned
to the variable age using = operator.
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction,
multiplication etc.
Java Arithmetic Operators
Operator Meaning
+ Addition (also used for string concatenation)
- Subtraction Operator
* Multiplication Operator
/ Division Operator
% Remainder Operator
Unary Operators
Unary operator performs operation on only one operand.
Operator Meaning
+ Unary plus (not necessary to use since numbers are positive without using it)
- Unary minus; inverts the sign of an expression
++ Increment operator; increments value by 1
-- decrement operator; decrements value by 1
! Logical complement operator; inverts the value of a boolean
Equality and Relational Operators
The equality and relational operators determines the relationship between two operands. It checks
if an operand is greater than, less than, equal to, not equal to and so on. Depending on the
relationship, it results to either true or false.
Java Equality and Relational Operators
Operator Description Example
== equal to 5 == 3 is evaluated to false
!= not equal to 5 != 3 is evaluated to true
> greater than 5 > 3 is evaluated to true
< less than 5 < 3 is evaluated to false
>= greater than or equal to 5 >= 5 is evaluated to true
<= less then or equal to 5 <= 5 is evaluated to true
Logical operators in Java
Operator Name ` Description
&& (Logical AND) Returns the value if both the conditions are true otherwise
returns zero.
II (Logical OR) Returns the value if even one condition is true
! returns true only if the variable given is not present
Ternary Operator
The conditional operator or ternary operator ?: is shorthand for if-then-else statement. The syntax
of conditional operator is:
variable = Expression ? expression1 : expression2
Here's how it works.
If the Expression is true, expression1 is assigned to variable.
If the Expression is false, expression2 is assigned to variable.