Operators
StevenSwiniarski474 total contributions
Published May 6, 2021Updated May 10, 2022
Contribute to Docs
Operators are used to perform various operations on variables and values of various data types.
Arithmetic Operators
Basic math operations can be applied to int
, double
, and float
data types:
+
addition-
subtraction*
multiplication/
division%
modulo (yields the remainder)
These operations are not supported for other data types.
int a = 20;int b = 10;int result;result = a + b; // 30result = a - b; // 10result = a * b; // 200result = a / b; // 2result = a % b; // 0
Comparison Operators
Comparison operators can be used to compare two values:
>
greater than<
less than>=
greater than or equal to<=
less than or equal to==
equal to!=
not equal to
They are supported for primitive data types and the result of a comparison is a boolean value true
or false
:
int a = 5;int b = 3;boolean result = a > b;// Result now holds the boolean value true
There is also a type comparison operator, instanceof
. It tests if a given object is an instance of a particular class, subclass, or interface. It will return false
if the object being compared has a null
value.
class Example {public static void main(String args[]) {Example test = new Example();System.out.println(test instanceof Example);// Outputs true}}
Bitwise Operators
Bitwise operators are used to manipulate individual bits of a number.
&
Bitwise AND|
Bitwise OR^
Bitwise XOR~
Bitwise complement<<
Signed right shift operator>>
Signed left shift operator>>>
Unsigned right shift operator<<<
Unsigned left shift operator
They can only be operated on data of int
, char
, byte
, long
, and short
.
int a = 5;int b = 3;int bitwiseAnd = a & b;// 1int bitwiseOr = a | b;// 7int bitwiseXor = a ^ b;// 6int bitwiseComplement = ~a;// -6int bitwiseRightShift = a >> b;// 0int bitwiseLeftShift = a << b;// 40
All contributors
- StevenSwiniarski474 total contributions
- Anonymous contributorAnonymous contributor2 total contributions
- christian.dinh2476 total contributions
- Anonymous contributorAnonymous contributor3071 total contributions
- StevenSwiniarski
- Anonymous contributor
- christian.dinh
- Anonymous contributor
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.