OOPS WITH
JAVA
OPERATORS
Ternary Operator
The ?: Operator
Java supports ternary operator which sometimes can be used as an
alternative for if-then-else statement.
The general form is –
var = expression1 ? expression2 : expression3;
Here, expression1 is evaluated first and it must return Boolean type. If
it results true, then value of expression2 is assigned to var, otherwise
value of expression3 is assigned to var.
For example,
int a=10, b=20, c ;
……….
c= (a>b)?a:b; //c will be assigned with biggest among a and b
Bitwise Operators
These Operators are used to perform
manipulation of
individual bits of a number. They can be used with
any
of the integer types.
Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise X-OR
~ Bitwise Complement
Table for Bitwise
Operators
A B A&B A|B A^B ~A
0 0 0 0 0 1
0 1 0 1 1 1
1 0 0 1 1 0
1 1 1 1 0 0
Bitwise AND Example
Bitwise AND
int x=5, y=6,z;
z= x & y;
128 64 32 16 8 4 2 1
x 0 0 0 0 0 1 0 1
y 0 0 0 0 0 1 1 0
x & y 0 0 0 0 0 1 0 0
Answer: z=4
Bitwise OR Example
Bitwise OR
int x=5, y=6,z;
z= x | y;
128 64 32 16 8 4 2 1
x 0 0 0 0 0 1 0 1
y 0 0 0 0 0 1 1 0
x | y 0 0 0 0 0 1 1 1
Z =7
Bitwise X-OR Example
Bitwise X OR
int x=5, y=6,z;
z= x ^ y;
128 64 32 16 8 4 2 1
x 0 0 0 0 0 1 0 1
y 0 0 0 0 0 1 1 0
x ^ y 0 0 0 0 0 0 1 1
Z=3
Bitwise NOT
A unary NOT operator ~, also called as bitwise
complement inverts all the bits of the operand.
For example, the number 42, which has the
following bit pattern:
128 64 32 16 8 4 2 1
00101010 becomes 11010101 after the NOT
operator is applied.
Shift Operators
These Operators are used to shift the bits of a
number left or right .
Operator Description
<< Left Shift Operator
>> Signed right shift Operator
>>> Unsigned right shift
Operator
Shift Operators - Left Shift
The left shift operator, <<, shifts all of the bits in
a value to the left by a specified number of times.
It has this general form:
value << num
Example: If x=10, then calculate x<<2 value.
Shifting the value of x towards the left two positions
will make the leftmost 2 bits to be lost. The value of
x is 10. The binary representation of 10 is 00001010.
After shifting the bits to the left the binary number
00001010 (in decimal 10) becomes 00101000 (in
decimal 40).
Shift Operators - Right Shift
The right shift operator, >> shifts all of the bits in
a value to the right by a specified number of
times. It has this general form:
value >> num
Example: If x=10, then calculate x>>2 value.
Shifting the value of x towards the right two positions
will make the rightmost 2 bits to be lost.
The value of x is 10. The binary representation
of 10 is 00001010. After shifting the bits to the right
the binary number 00001010 (in decimal 10)
becomes 00000010 (in decimal 2).
Right Shift
The unsigned right-shift operator is a special
type of right-shift operator that doesn't use the
sign bit for filling the trailing position. The
unsigned right-shift operator always fills the
trialing position by 0.
Instance of operator
Instance of operator is used for type checking. It
can be used for type checking. It can be used to
test if an object of a class, a subclass or an
Interface. It is used to test whether the object is
an instance of the specified class or subclass or
an interface.
Example Program for instance of
Operator
class Instance
{
public static void main(String args[])
{
Instance s =new Instance();
System.out.println(s instanceof
Instance);
}
}
Output: true

object oriented programming in javaTERNARY OPERATORS.pptx

  • 1.
  • 2.
    Ternary Operator The ?:Operator Java supports ternary operator which sometimes can be used as an alternative for if-then-else statement. The general form is – var = expression1 ? expression2 : expression3; Here, expression1 is evaluated first and it must return Boolean type. If it results true, then value of expression2 is assigned to var, otherwise value of expression3 is assigned to var. For example, int a=10, b=20, c ; ………. c= (a>b)?a:b; //c will be assigned with biggest among a and b
  • 3.
    Bitwise Operators These Operatorsare used to perform manipulation of individual bits of a number. They can be used with any of the integer types. Operator Description & Bitwise AND | Bitwise OR ^ Bitwise X-OR ~ Bitwise Complement
  • 4.
    Table for Bitwise Operators AB A&B A|B A^B ~A 0 0 0 0 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 0
  • 5.
    Bitwise AND Example BitwiseAND int x=5, y=6,z; z= x & y; 128 64 32 16 8 4 2 1 x 0 0 0 0 0 1 0 1 y 0 0 0 0 0 1 1 0 x & y 0 0 0 0 0 1 0 0 Answer: z=4
  • 6.
    Bitwise OR Example BitwiseOR int x=5, y=6,z; z= x | y; 128 64 32 16 8 4 2 1 x 0 0 0 0 0 1 0 1 y 0 0 0 0 0 1 1 0 x | y 0 0 0 0 0 1 1 1 Z =7
  • 7.
    Bitwise X-OR Example BitwiseX OR int x=5, y=6,z; z= x ^ y; 128 64 32 16 8 4 2 1 x 0 0 0 0 0 1 0 1 y 0 0 0 0 0 1 1 0 x ^ y 0 0 0 0 0 0 1 1 Z=3
  • 8.
    Bitwise NOT A unaryNOT operator ~, also called as bitwise complement inverts all the bits of the operand. For example, the number 42, which has the following bit pattern: 128 64 32 16 8 4 2 1 00101010 becomes 11010101 after the NOT operator is applied.
  • 9.
    Shift Operators These Operatorsare used to shift the bits of a number left or right . Operator Description << Left Shift Operator >> Signed right shift Operator >>> Unsigned right shift Operator
  • 10.
    Shift Operators -Left Shift The left shift operator, <<, shifts all of the bits in a value to the left by a specified number of times. It has this general form: value << num Example: If x=10, then calculate x<<2 value. Shifting the value of x towards the left two positions will make the leftmost 2 bits to be lost. The value of x is 10. The binary representation of 10 is 00001010. After shifting the bits to the left the binary number 00001010 (in decimal 10) becomes 00101000 (in decimal 40).
  • 11.
    Shift Operators -Right Shift The right shift operator, >> shifts all of the bits in a value to the right by a specified number of times. It has this general form: value >> num Example: If x=10, then calculate x>>2 value. Shifting the value of x towards the right two positions will make the rightmost 2 bits to be lost. The value of x is 10. The binary representation of 10 is 00001010. After shifting the bits to the right the binary number 00001010 (in decimal 10) becomes 00000010 (in decimal 2).
  • 12.
    Right Shift The unsignedright-shift operator is a special type of right-shift operator that doesn't use the sign bit for filling the trailing position. The unsigned right-shift operator always fills the trialing position by 0.
  • 13.
    Instance of operator Instanceof operator is used for type checking. It can be used for type checking. It can be used to test if an object of a class, a subclass or an Interface. It is used to test whether the object is an instance of the specified class or subclass or an interface.
  • 14.
    Example Program forinstance of Operator class Instance { public static void main(String args[]) { Instance s =new Instance(); System.out.println(s instanceof Instance); } } Output: true