1 of 21

Java Programming - Operators

2 of 21

Operators

2 november

Operators are used to perform operations on variables and values.

  • Arithmetic Operators
  • Assignment Operator
  • Bitwise Operators
  • Relational Operators
  • Boolean Logical Operators
  • Ternary (?) Operator

3 of 21

Arithmetic Operators

2 november

  • Arithmetic operators are used in mathematical expressions in the same way

that they are used in algebra.

4 of 21

Assignment Operator

2 november

  • The assignment operator is the single equal sign, =.
  • The assignment operator works in Java much as it does in any other computer

Language.

var = expression;

int x, y, z;

x = y = z = 100; // set x, y, and z to 100

5 of 21

Bitwise Operators

2 november

  • Java defines several bitwise operators that can be applied to the integer types:

long, int, short, char, and byte. These operators act upon the individual bits of

their operands.

6 of 21

Relational Operators

2 november

  • The relational operators determine the relationship that one operand has to the

other

7 of 21

Boolean Logical Operators

2 november

  • The Boolean logical operators operate only on boolean operands.
  • All of the binary logical operators combine two boolean values to form a

resultant boolean value.

8 of 21

The ? Operator

Output

2 november

  • Java includes a special ternary (three-way) operator that can replace certain

types of if-then-else statements.

Syntax:

expression1 ? expression2 : expression3 ;

Example:

x = a < 1 ? -1 : 2;

9 of 21

Operator Precedence

2 november

  • Operators in the same row are equal in precedence
  • Order of precedence for Java operators, from highest to lowest

10 of 21

Using Parentheses

2 november

  • Parentheses raise the precedence of the operations that are inside them.

a >> b + 3

  • a >> (b + 3) // This expression first adds 3 to b and then shifts a right by that result.

  • (a >> b) + 3 // This expression first shift a right by b positions and then add 3 to that result

11 of 21

Arithmetic Operators

12 of 21

The Modulus Operators

Output

13 of 21

Arithmetic Compound Assignment Operators

Output

  • Java provides special operators that can be used to combine an arithmetic

operation with an assignment.

a = a + 4; ----> a += 4;

a = 6

b = 8

c = 3

14 of 21

Increment and Decrement Operators

Output

  • The increment operator increases its operand by one. The decrement

operator decreases its operand by one.

x = x + 1; ----> x++;

x = x - 1; -----> x--;

y = ++x; ----> x = x + 1;

y = x;

y = x++; -----> y = x;

x = x + 1;

15 of 21

The Bitwise Logical Operators

Output

  • The bitwise logical operators are &, |, ^, and ~. Bitwise operators are applied to each individual bit within each operand

16 of 21

The Left Shift Operators

Output

  • The left shift operator, <<, shifts all of the bits in a value to the left a specified

number of times. It has this general form:

value << num

17 of 21

The Right Shift Operators

Output

Output

Signed right shift operator, >>, shifts all of the bits in a value to the right a specified number of times. Its general form is shown here:

value >> num

-2

2

>>> (Unsigned right shift) In Java, the operator ‘>>>’ is unsigned right shift operator. It always fills 0 irrespective of the sign of the number.

7

3

1

18 of 21

Bitwise Operator Compound Assignments

Output

a = 3

b = 1

c = 6

19 of 21

Relational Operators

The relational operators determine the relationship that one operand has to the other.

class Main {

public static void main(String[] args) {

int a = 7, b = 11;

System.out.println("a is " + a + " and b is " + b);

// == operator

System.out.println(a == b); // false

// != operator

System.out.println(a != b); // true

// > operator

System.out.println(a > b); // false

// < operator

System.out.println(a < b); // true

// >= operator

System.out.println(a >= b); // false

// <= operator

System.out.println(a <= b); // true

}}

20 of 21

Boolean Logical Operators

Output

The relational operators determine the relationship that one operand has to the other.

21 of 21

Thank You