Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Successfully reported this slideshow.

Like this presentation? Why not share!

- Bitwise Operators in JAVA by Ali Asghar Manjotho 652 views
- OCA JAVA - 3 Programming with Java... by Fernando Gil 1035 views
- 15 bitwise operators by Ravindra Rathore 3416 views
- Java Datatypes by Mayank Aggarwal 606 views
- Operators in java by Ravi_Kant_Sahu 3360 views
- Java Basics by Dhanunjai Bandlamudi 2475 views

1,623 views

Published on

No Downloads

Total views

1,623

On SlideShare

0

From Embeds

0

Number of Embeds

2

Shares

0

Downloads

0

Comments

0

Likes

5

No embeds

No notes for slide

- 1. Java Operators
- 2. Java provides a rich set of operators to manipulatevariables. We can divide all the Java operators into thefollowing groups:• Assignment Operator•Arithmetic Operators•Unary Operators• Equality and Relational Operators•Conditional (Logical) Operators•Bitwise and Bit Shift Operators
- 3. (1) Assignment Operator Simple Assignment Operator Syntax of using the assignment operator is: <variable> = <expression>; Compound Assignment Operator Syntax of using the compound assignment operator is: operand operation= operand
- 4. Compound assignment operators :Operator Example Equivalent Expression+= x += y; x = (x + y);-= x -= y; x = (x - y);*= x *= y; x = (x * y);/= x /= y; x = (x / y);%= x %= y; x = (x % y);&= x &= y; x = (x & y);|= x != y; x = (x ! y);^= x ^= y; x = (x ^ y);<<= x <<= y; x = (x << y);>>= x >>= y; x = (x >> y);>>>= x >>>= y; x = (x >>> y);
- 5. (2) Arithmetic Operators The symbols of arithmetic operators are given in a table:Symbol Name of the Operator Example+ Additive Operator n = n + 1;- Subtraction Operator n = n - 1;* Multiplication Operator n = n * 1;/ Division Operator n = n / 1;% Remainder Operator n = n % 1; The "+" operator can also be used to concatenate (to join) the two stringstogether. For example:String str1 = "Concatenation of the first";String str2 = "and second String";String result = str1 + str2;
- 6. (3) Unary OperatorsThere are different types of unary operators : + Unary plus operator indicates positive value (however, numbersare positive without this)Ex : int number = +1; - Unary minus operator negates an expressionEx : number = - number; ++ Increment operator increments a value by 1Ex : number = ++ number; -- Decrement operator decrements a value by 1Ex : number = -- number; ! Logical compliment operator inverts a boolean value
- 7. (4) Equality and Relational OperatorsSymbol Name of the Operator Example== Equal to a==b!= Not equal to a!=b> Greater than a>b< Less than a<b>= Greater than or equal to a>=b<= Less than or equal to a>=b
- 8. (5) Conditional (Logical) OperatorsSymbol Name of the Operator& AND&& Conditional-AND| OR|| Conditional-OR! NOT?: Ternary (shorthand for if-then-else statement)
- 9. ternary ("?:") operatorJava supports another conditional operator that is known as the ternary operator"?:" and basically is used for an if-then-else as shorthand asboolean expression ? operand1 : operand2; If we analyze this diagram then we find that, operand1 is returned, if the expression is true; otherwise operand2 is returned in case of false expression.
- 10. Lets have an example implementing some Logical operators:class ConditionalOperator{ public static void main(String[] args){ int x = 5; int y = 10, result=0; boolean bl = true; if((x == 5) && (x < y)) System.out.println("value of x is "+x); if((x == y) || (y > 1)) System.out.println("value of y is greater than the value of x"); result = bl ? x : y; System.out.println("The returned value is "+result); }}
- 11. Outputvalue of x is 5 is 5value of y is greater than the value of x lue of y is greater than the value of xThe returned value is 5
- 12. (6) Bitwise and Bit Shift Operators. There are different types of bitwise and bit shift operators available inthe Java language summarized in the table.Symbol Name of the Operator Example~ Unary bitwise complement ~op2& Bitwise AND op1 & op2| Bitwise inclusive OR op1 | op2^ Bitwise exclusive OR op1 ^ op2<< Signed left shift op1 << op2>> Signed right sift op1 >> op2>>> Unsigned right shift op1 >>> op2
- 13. I. Unary Bitwise Complement ("~") :Lets use the table to understand bitwise complementoperation :Operand Result 0 1 1 0 1 0 0 1II. Bitwise AND (&) :Lets understand the AND operations using truth table:(AND) A B Result 0 0 0 1 0 0 0 1 0 1 1 1
- 14. III. Bitwise inclusive OR ( | ) :Lets understand the inclusive OR operations using truthtable: (OR) A B Result 0 0 0 1 0 1 0 1 1 1 1 1IV. Bitwise exclusive OR (^) :Lets understand the exclusive OR operations using truthtable: A B Result 0 0 0 1 0 1 0 1 1 1 1 0
- 15. Bit Shifts Operators:I. Signed Left Shift ("<<") :This diagram shows that, all bits of the upper position were shifted to the left bythe distance of 1; and the Zero was shifted to the right most position. Thus theresult is returned as 11100.Another expression "2<<2"; shifts all bits of the number 2 to the left placing azero to the right for each blank place. Thus the value 0010 becomes 1000 or 8 indecimal.
- 16. II. Signed Right Shift (">>") :This diagram shows that, all bits of the upper position were shifted to the rightdistance specified by 1; Since the sign bit of this number indicates it as a positivenumber so the 0 is shifted to the right most position. Thus the result is returned as00011 or 3 in decimal.Another expression "2>>2"; shifts all bits of the number 2 to the right placing azero to the left for each blank place. Thus the value 0010 becomes 0000 or 0 indecimal.
- 17. III. Unsigned Right Shift (">>>") :For example, the expression "14>>>2"; shifts all bits of thenumber 14 to the right placing a zero to the left for each blankplace Thus the value 1110 becomes 0011 or 3 in decimal.
- 18. Operator PrecedenceOperators Precedencearray index & parentheses [] ( )access object .postfix expr++ expr--unary ++expr --expr +expr -expr ~ !multiplicative * / %additive + -bit shift << >> >>>relational < > <= >=equality == !=bitwise AND &bitwise exclusive OR ^bitwise inclusive OR |logical AND &&logical OR ||ternary ?:assignment = += -= *= /= %= &= ^= |= <<= >>= >> >=
- 19. Lets see an example that evaluates an arithmeticexpression according to the precedence order.class PrecedenceDemo{ public static void main(String[] args){ int a = 6; int b = 5; int c = 10; float rs = 0; rs = a + (++b)* ((c / a)* b); System.out.println("The result is:" + rs); }}
- 20. The expression "a+(++b)*((c/a)*b)" is evaluated from rightto left. Its evaluation order depends upon the precedence order ofthe operators. It is shown below:(++b) a + (++b)*((c/a)*b)(c/a) a+ (++b)*((c/a)*b)(c/a)*b a + (++b)*((c/a)* b)(++b)*((c/a)*b) a + (++b)*((c/a)* b)a+(++b)*((c/a)*b) a+(++b)*((c/a)*b)OutputThe result is:42.0

No public clipboards found for this slide

Be the first to comment