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
• Arithmetic operators are utilized in mathematical expressions
similarly that they are utilized in mathematics.
ARITHMETIC
OPERATORS
EXAMPLE –USING ARITHMETIC OPERATORS
EXAMPLE –USING 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
• Logical operators are
used to determine the
logic between variables
or values.
• Assume variable A
holds true and variable
B holds false then:
TRUTH TABLE-
LOGICAL AND
(&&) A B A&&B
True True True
True False False
False True False
False False False
TRUTH TABLE-
LOGICAL OR (||)
A B A||B
True True True
True False True
False True True
False False False
TRUTH TABLE-
LOGICAL NOT (!)
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
• Signed Right shift operator (>>) –
Shifts the bits of the number to the right and fills 0 on voids left as a result. The
leftmost bit depends on the sign of initial number. Similar effect as of dividing
the number with some power of two.
JAVA BITWISE OPERATORS
• Left shift operator (<<) –
Shifts the bits of the number to the left and fills 0 on voids left as a result.
Similar effect as of multiplying the number with some power of two.
JAVA
BITWISE
OPERATORS
OPERATOR PRECEDENCE
• All operators inside the parenthesis are evaluated first.
• *, / and % are at the same level of precedence and are evaluated next.
• + and - have the same level and are evaluated last.
• When operators are at the same level, we use left to right associativity.
• 3*7-6+2/4+6 means (((3*7)-6) + ((2*5)/4)) + 6.
OPERATOR
PRECEDENCE
• Association – when selecting
between two operators of the
same priority they operate
according to their
associativity which is:
• Right to left – unary
operators and assignment
operators are right to left
• Left to right – all other
operators
EXPRESSIONS
• These are the combinations of operators and operands.
• If all operands are integers this is called an integer expression and it yields an integer result.
• For example 2+3*5.
• And if all operands are floating point this is called floating-point expression and the result is floating
point numbers.
• Mixed expressions has operands of different data types. The result would be of the highest size
data type.
• For example, 2 + 3.5.