You are on page 1of 45

CSE310: Programming in Java

Topic: Operators in Java


Outlines [Expected Time: 1 Hours]

• Introduction
• Assignment Operator
• Arithmetic Operator
• Relational Operator
• Bitwise Operator
• Conditional Operator
• Type Comparison Operator
• Unary Operator
Introduction

 Operators are special symbols that perform specific


operations on one, two, or three operands, and then
return a result.

 Operator in java is a symbol that is used to perform operations.


For example: +, -, *, / etc.
 There are many types of operators in java which are
given below:

• 1. Unary Operator,
• 2. Arithmetic Operator,
• 3. Shift Operator,
• 4. Relational Operator,
• 5. Bitwise Operator,
• 6. Logical Operator,
• 7.Ternary Operator and
• 8. Assignment Operator.
Unary Operators
 The unary operators require only one operand.
Operator Description

+ Unary plus operator; indicates positive value

- Unary minus operator; negates an expression

++ Increment operator; increments a value by 1

-- Decrement operator; decrements a value by 1

Logical complement operator; inverts the value of a


! boolean
Unary operators are used to perform various
operations i.e.:

• incrementing/decrementing a value by one


• negating an expression
• inverting the value of a boolean
Java Unary Operator Example: ++ and --
class OperatorExample

 public static void main(String args[])

 int x=10; 
 System.out.println(x++);//10 (11) 
 System.out.println(++x);//12  Output:
 System.out.println(x--);//12 (11)  10
 System.out.println(--x);//10  } 12
}  12 10
Java Unary Operator Example 2: ++ and --
class OperatorExample1
{  
public static void main(String args[])
{  
int a=10;  
int b=10;  
System.out.println(a++ + ++a);//10+12=22  
System.out.println(b++ + b++);//10+11=21 
} Output:
}   22
21
Logical Operators
• The following table shows the effect of each logical
operation:

A B A|B A&B A^B ~A

False False False False False True

True False True False True False

False True True False True True

True True True True False False


Bitwise Logical NOT
• Also called the bitwise complement, the unary
NOT operator, ~, inverts all of the bits of its
operand. For example, the number 42, which
has the following bit pattern:
00101010 becomes
11010101
after the NOT operator is applied.
Bitwise Logical AND
• The AND operator, &, produces a 1 bit if both
operands are also 1. A zero is produced in all
other cases. Here is an example:
00101010 42
&
00001111 15
=
00001010 10
Bitwise Logical OR
• The OR operator, |, combines bits such that if
either of the bits in the operands is a 1, then
the resultant bit is a 1, as shown here:
00101010 42
|
00001111 15
=
00101111 47
Bitwise Logical XOR
• The XOR operator, ^, combines bits such that if exactly one
operand is 1, then the result is 1. Otherwise, the result is zero. The
following example shows the effect of the ^.

• Notice how the bit pattern of 42 is inverted wherever the second


operand has a 1 bit. Wherever the second operand has a 0 bit, the
first operand is unchanged. You will find this property useful when
performing some types of bit manipulations.
00101010 42
^
00001111 15
=
00100101 37
Java Unary Operator Example: ~ and !
class OperatorExample2
{  
public static void main(String args[])
{  
int a=10;  
int b=-10;  
boolean c=true;  
boolean d=false;  
System.out.println(~a);
// -11 (minus of total positive value which starts from 0)  
System.out.println(~b);
// 9 (positive of total minus, positive starts from 0)  
System.out.println(!c);// false (opposite of boolean value)  
System.out.println(!d);// true  
} }  
Arithmetic Operators
 Java provides operators that perform addition, subtraction,
multiplication, and division.
Operator Description
Additive operator (also used for String
+ concatenation)

- Subtraction operator

* Multiplication operator

/ Division operator

% Remainder operator
Java Arithmetic Operator Example
• class OperatorExample
• {  
• public static void main(String args[])
• {  
• int a=10;  
• int b=5;  
• System.out.println(a+b); //15  
• System.out.println(a-b); //5  
• System.out.println(a*b); //50  
• System.out.println(a/b); //2  
• System.out.println(a%b); //0  
• }
• }  
Java Arithmetic Operator Example:
Expression
• class OperatorExample
• {  
• public static void main(String args[])
• {  
• System.out.println(10*10/5+3-1*4/2);  
• }
• }  Output:
• 21
Shift Operators
• Java Left Shift Operator<<

• The Java left shift operator << is used to


shift all of the bits in a value to the left side
of a specified number of times.
Java Left Shift Operator Example
• class OperatorExample2
• {  
• public static void main(String args[])
• { 
•  System.out.println(10<<2);
• //10*2^2=10*4=40  
• System.out.println(10<<3);
• //10*2^3=10*8=80 
•  System.out.println(20<<2);
• //20*2^2=20*4=80  
• System.out.println(15<<4);
• //15*2^4=15*16=240  
• }
• } 
Java Right Shift Operator

• The Java right shift operator >> is used to


move left operands value to right by the
number of bits specified by the right
operand.
Java Right Shift Operator Example
• class OperatorExample3
• { 
•  public static void main(String args[])
• {  
• System.out.println(10>>2);//10/2^2=10/4=2
System.out.println(20>>2);//20/2^2=20/4=5
System.out.println(20>>3);//20/2^3=20/8=2
 }
• }
The Unsigned Right Shift
• In these cases, to shift a zero into the high-order bit no
matter what its initial value was. This is known as an
unsigned shift.

• To accomplish this, we will use Java’s unsigned, shift-right


operator, >>>, which always shifts zeros into the high-order
bit.

• Example:
– 11111111 11111111 11111111 11111111 –1 in
binary as an int
– >>>24
– 00000000 00000000 00000000 11111111 255 in
binary as an int
Java AND Operator

• Logical && and Bitwise &


• The logical && operator doesn't check second
condition if first condition is false. It checks
second condition only if first one is true.

• The bitwise & operator always checks both


conditions whether first condition is true or
false.
Example: Logical && and Bitwise &
class OperatorExample
{  
public static void main(String args[])
{  
int a=10;  
int b=5;  
int c=20;  
System.out.println(a<b&&a<c); //false && true = false  
System.out.println(a<b&a<c); //false & true = false  
}
}  
Example: Logical && vs Bitwise &
class OperatorExample
{  
public static void main(String args[])
{  
int a=10;  
int b=5;  
int c=20;  
System.out.println(a<b&&a++<c); //false && true = false  
System.out.println(a); //10 because second condition is not checked 
System.out.println(a<b&a++<c); //false && true = false  
System.out.println(a); //11 because second condition is checked  
}
}  
Java OR Operator

• Logical || and Bitwise |


• The logical || operator doesn't check second
condition if first condition is true. It checks
second condition only if first one is false.

• The bitwise | operator always checks both


conditions whether first condition is true or
false.
Example: Logical || and Bitwise |
class OperatorExample
{  
public static void main(String args[])
{  
int a=10;  
int b=5;  
int c=20;  
System.out.println(a>b||a<c); //true || true = true  
System.out.println(a>b|a<c); //true | true = true  
//|| vs |  
System.out.println(a>b||a++<c); //true || true = true  
System.out.println(a); //10 because second condition is not checked  
System.out.println(a>b|a++<c); //true | true = true  
System.out.println(a); //11 because second condition is checked  
}
}  
Java Ternary Operator

• Java Ternary operator is used as one liner


replacement for if-then-else statement and
used a lot in java programming.

• It is the only conditional operator which takes


three operands.
Java Ternary Operator Example1
class OperatorExample
{  
public static void main(String args[])
{  
int a=2;  
int b=5;  
int min=(a<b)?a:b;  
System.out.println(min);   Output:
} 2
}  
Java Ternary Operator Example2
class OperatorExample
{  
public static void main(String args[])
{  
int a=10;  
int b=5;  
int min=(a<b)?a:b;  
System.out.println(min);   Output:
} 5
}  
Java Assignment Operator

• Java assignment operator is one of the most


common operator.

• It is used to assign the value on its right to the


operand on its left.
Java Assignment Operator Example1
class OperatorExample
{  
public static void main(String args[])
{  
int a=10;  
int b=20;  
a+=4; //a=a+4 (a=10+4)  
b-=4; //b=b-4 (b=20-4)  
System.out.println(a);  
System.out.println(b);  
}
}  
Java Assignment Operator Example2
class OperatorExample
{  
public static void main(String[] args)
{  
int a=10; 
 a+=3; //10+3  
System.out.println(a); 
 a-=4; //13-4 
 System.out.println(a);  
a*=2; //9*2 
 System.out.println(a);  
a/=2; //18/2  
System.out.println(a);  
}

The following statement is correct:
• System.out.println(x = 1);
• which is equivalent to
• x = 1;
• System.out.println(x);
If a value is assigned to multiple variables, you can use
this syntax:
• i = j = k = 1;
• which is equivalent to
• k = 1;
• j = k;
• i = j;
• In an assignment statement, the data type of
the variable on the left must be compatible
with the data type of the value on the right.

• For example, int x = 1.0 would be illegal.

• We cannot assign a double value (1.0) to


• an int variable without using type casting.
Relational Operators

 Relational operators determine if one operand is


greater than, less than, equal to, or not equal to
another operand.

 It always returns boolean value i.e true or false.


Relational Operators
Operator Description

== equal to

!= not equal to

< less than

> greater than

<= less than or equal to

>= greater than or equal to


Type Comparison Operators

 The instanceof operator is used to compare an object


to a specified type i.e. class or interface.

 It can be used to test if an object is an instance of a


class or subclass, or an instance of a class that
implements a particular interface.
Example
class Parent{}
class Child extends Parent{}
class TestInstanceOf {
public static void main(String [] rk){
Parent p = new Parent();
Child c = new Child();
System.out.println(p instanceof Child);
System.out.println(p instanceof Parent);
System.out.println(c instanceof Parent); }
}
Operator Precedence
Highest
() [] .
++ -- %
+ -
>> >>> <<
> >= < <=
== !=
&
^
|
&&
||
?:
= Op=
Lowest
Brainstorming 1
What will be the output of the following code snippets?

 byte b = 30; System.out.println(~b);

 byte b = -53; System.out.println(~b);

 System.out.println(34<<3);

 System.out.println(-34>>3);

 System.out.println(-34>>>3);
Brainstorming 2

int x = 10, y = 5;
while(x- - > 7 || + + y < 8 )
System.out.print(x);
System.out.print(y);

A. 95
B. 67
C. 78
D. 48
E. 77
F. N.O.T
Brainstorming 3
System.out.print(2>1||4>3?false:true);
class X{}
class Y extends X{}
class Z extends Y{}

X x1 = new Y();
Y y1 = new Z();
Y y2 = new Y();
System.out.println( x1 instanceof X);
System.out.println( x1 instanceof Z);
System.out.println( y1 instanceof Z);
System.out.println( y2 instanceof X);
Brainstorming 4

System.out.print(2>1||4>3?false:true);

You might also like