0% found this document useful (0 votes)
4 views

Topic 4_Operators in Java (1) (1)

The document provides an overview of Java operators, including their categories such as arithmetic, assignment, relational, logical, bit-wise, and shift operators, along with their precedence and associativity. It explains how each operator functions with examples, detailing how they perform operations on values and variables. Additionally, it covers the increment and decrement operators, highlighting their unary nature and impact on operand values.

Uploaded by

joyikehi7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Topic 4_Operators in Java (1) (1)

The document provides an overview of Java operators, including their categories such as arithmetic, assignment, relational, logical, bit-wise, and shift operators, along with their precedence and associativity. It explains how each operator functions with examples, detailing how they perform operations on values and variables. Additionally, it covers the increment and decrement operators, highlighting their unary nature and impact on operand values.

Uploaded by

joyikehi7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

JAVA OPERATORS

JAVA OPERATORS Operator Precedence determines the grouping of terms in an


expression. This affects how an expression is evaluated.
An operator is a symbol that Certain operators have higher precedence than others
perform operations on a value or
Category Operator Associativity
a variable.
Postfix () [] -> . ++ - - Left to right

1. Arithmetic Operators Unary + - ! ~ ++ - - (type)* &sizeof Right to left

Multiplicative */% Left to right


2. Assignment Operators Additive +- Left to right

Shift <<>> Left to right


3. Relational Operators
Relational <<= >>= Left to right

4. Logical Operators Equality == != Left to right

Bitwise AND & Left to right

5. Unary Operators Bitwise XOR ^ Left to right

Bitwise OR | Left to right


6. Bit-Wise Operators Logical AND && Left to right

Logical OR || Left to right

Conditional ?: Right to left

Assignment = += -= *= /= %=>>= <<= &= ^= Right to left


|=

Comma , Left to right


Java Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition,
subtraction, multiplication, division etc. on numerical values (constants and
variables).

Operator Description Example

Addition (+) Adds two operands x=y+z


Subtraction (-) Subtracts one operand from x=y-z
another

Multiplication Multiplies two operands x=y*z


(*)
Division (/) Divides two operands x=y/z
Modulus (%) Calculates the modulus x=y%z
Java Increment and Decrement Operators
Increment ++ and Decrement -- Operators are used to change the value of an
operand (constant or variable) by 1.

Increment ++ increases the value by 1 whereas decrement -- decreases the


value by 1.

These two operators are unary operators, meaning they only operate on a single
operand.
For example:

Operator Description Example x = 5;


(++) Increments a variable ++x y = ++ x;

(--) Decrements a variable --x i.e

x = 5;
x = x + 1;
y = x;
Java Assignment Operators
An assignment operator is used for assigning a value to a variable. The most
common assignment operator is =.

Operator Example Same as


= a=b a=b
+= a+=b a = a+b
-= a-=b a = a-b
*= a*=b a = a*b
/= a/=b a = a/b
%= a%=b a = a%b
Java Relational Operators
Relational Operators are used to compare two operands. When two operands are
compared by using these operators, the result is a logical value either TRUE or
FALSE. The following table lists the various relational operators.
Operator Description Example Explanation
(<) Evaluates whether the left operands Returns true if the x is less
Less than is less than the right operand x<y than y and false otherwise
(>) Evaluates whether the left operands Returns true if the x is
Greater than is greater than the right operand x>y greater than y and false
otherwise
Evaluates whether the left operands Returns true if the x is less
(<=) is less than or equal to the right x <= y than or equal to y and false
Less than or Equal to operand otherwise

(>=) Evaluates whether the left operands Returns true if the x is


Greater than or Equal is greater than or equal to the right x >= y greater than or equal to y
to operand and false otherwise
(==) Evaluates whether the operands are Returns true if the values are
Equal to equal x == y equal and false otherwise
(!=) Evaluates whether the operands are Returns true if the values are
Not Equal to not equal x != y not equal and false otherwise
Java Logical Operators
Logical Operators are used to combine the results of expressions containing
relational operators.
Operator Description Example
Logical AND (&&) Evaluates whether the compound Expression is
true when two expressions are true
x < 5 && y > 10
Logical OR (||) Evaluates whether the compound Expression is x < 5 || y > 10
true if either expressions are true
Logical NOT (!) Evaluates whether a single Expression is true if
the expression is false or false if expression is
! (x < 5 )
true

Value of the combined


The truth table explains Expression 1 Expression 2
Expression
the working of the AND OR
Logical AND or Logical
True True True True
OR operators
True False False True
False True False True
False False False False
Java Bit-Wise Operators
Bit-wise Operators operates on the individual bits of their operands. When you
use a bit-wise operator, the operands are first converted into their binary
equivalents and then the bits-wise operator operates on the bits.

Operator Description Example


Bit-wise AND (&) Evaluates to 1 if both bits, x and y are 1. If
either or the bits are 0, the result is 0.
x&y
Bit-wise OR (|) Evaluates to 0 if both bits, x and y are 0. If
either or the bits are 1, the result is 1.
x |y
Bit-wise NOT (~) Converts all the 1s into 0s and all the 0s
into 1s.
~x
Bit-wise XOR (^) Evaluates to 1 if bits have different values
and 0 if both the bits have same values.
x^y
Example

Bit-wise AND Operator Bit-wise NOT Operator

0 0 0 0 0 0 1 0 2 0 0 0 0 0 0 1 0 2

0 0 0 0 0 0 1 1 3 1 1 1 1 1 1 0 1 ~2 = -3

0 0 0 0 0 0 1 0 2&3=2
Bit-wise XOR Operator
Bit-wise OR Operator
0 0 0 0 0 0 1 0 2
0 0 0 0 0 0 1 0 2
0 0 0 0 0 0 1 1 3
0 0 0 0 0 0 1 1 3

0 0 0 0 0 0 0 1 2^3=1
0 0 0 0 0 0 1 1 2|3=2
Java Shift Operators
Shift Operators operates on the individual
bits of their operands. It shifts the bits of its
operand either to the left or right.
Right Shift Operator
Operator Description Example
>> Shifts the bits of the x 0 0 0 0 0 1 0 1 5
operand to the right by
(Right Shift) x >> y
the number of position 0 0 0 5 >> 2 = 1
0 0 0 0 1
specified y
<< Shifts the bits of the x
operand to the left by the
(Left Shift) x << y
number of position 0 0 0 0 0 1 0 0 4
specified y

0 0 0 1 0 0 0 0 4 << 2 = 16

You might also like