object oriented programming in javaTERNARY OPERATORS.pptx
riazahamed37
10 views
14 slides
Jul 16, 2024
Slide 1 of 14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
About This Presentation
discusses ternary operators in java
Size: 106.39 KB
Language: en
Added: Jul 16, 2024
Slides: 14 pages
Slide Content
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 1 1 1 1 1 1 1 1 1 1 1 1
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).
Shift Operators - Unsigned 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