Java Operators
Java Operators
JAVA OPERATORS
Operators are used to perform operations on variables and values.
Java divides the operators into the following groups:
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Bitwise operators
ARITHMETIC OPERATORS
• In the example below, we use the + operator to add together two values:
int x = 100 + 50;
• Although the + operator is often used to add together two values, like in the example
above, it can also be used to add together a variable and a value, or a variable and
another variable:
int sum1 = 100 + 50; // 150 (100 + 50)
int sum2 = sum1 + 250; // 400 (150 + 250)
int sum3 = sum2 + sum2; // 800 (400 + 400)
JAVA ASSIGNMENT OPERATORS
• Assignment operators are used to assign values to variables.
• In the example below, we use the assignment operator (=) to assign the
value 10 to a variable called x:
int x = 10;
• The addition assignment operator (+=) adds a value to a variable:
int x = 10;
x += 5;
A LIST OF ALL
ASSIGNMENT
OPERATORS:
ASSIGNMENT
OPERATORS:
SAMPLE
PROGRAM
JAVA
COMPARISON/RELATIONAL
OPERATORS
• Comparison operators
are used to compare two
values:
EXAMPLE-
COMPARISON/RELATIONAL
OPERATORS
• Assume variable A
holds 10 and variable B
holds 20, then:
EXAMPLE-
COMPARISON/RELATIONAL
OPERATORS
• Assume variable A
holds 10 and variable B
holds 20, then:
JAVA LOGICAL
OPERATORS
A !A
True False
False True
EXAMPLE-JAVA • Assume a=true and b=false
LOGICAL
OPERATORS
JAVA BITWISE OPERATORS
• Bitwise operators are used to perform binary logic with the bits of an integer
or long integer.
• Bitwise operators are used to perform manipulation of individual bits of a
number.
• They can be used with any of the integral types (char, short, int, etc.).
JAVA BITWISE OPERATORS
JAVA BITWISE
OPERATORS
• Bitwise OR (|) –
This operator is binary
operator, denoted by ‘|’.
It returns bit by bit OR
of input values, i.e, if
either of the bits is 1, it
gives 1, else it gives 0.
For example:
BITWISE OR (|)
JAVA BITWISE
OPERATORS
• Bitwise AND (&) –
This operator is binary
operator, denoted by ‘&’. It
returns bit by bit AND of
input values, i.e, if both bits
are 1, it gives 1, else it gives
0.
• For example:
BITWISE
AND (&)
JAVA BITWISE
OPERATORS
• Bitwise XOR (^) –
This operator is binary
operator, denoted by ‘^’. It
returns bit by bit XOR of
input values, i.e, if
corresponding bits are
different, it gives 1, else it
gives 0.
For example,
BITWISE XOR
JAVA BITWISE
OPERATORS
• Bitwise Complement (~) –
This operator is unary
operator, denoted by ‘~’. It
returns the one’s compliment
representation of the input
value, i.e, with all bits
inversed, means it makes
every 0 to 1, and every 1 to 0.
For example,
BITWISE NOT (~)
JAVA
BITWISE
OPERATORS
PROGRAM OUTPUT:
JAVA BITWISE OPERATORS