05 operators

619 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
619
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
18
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

05 operators

  1. 1. Operators<br />DhrubojyotiKayal<br />
  2. 2. What is an operator?<br />Precedence<br />Assignment operator<br />Mathematical operators<br />Increment & Decrement operator<br />Relational operators<br />Logical operator<br />Ternary operator<br />String operator<br />Casting operator<br />Agenda<br />
  3. 3. Symbol representing an operation – mathematical, character etc<br />Needs one or more operands to produce result<br />A + B <br />10 + 20 <br />Operator can produce result<br />Operator can change the value of the operand (side effect)<br />Operators, operands and parenthesis combine to form expressions<br />What is an operator?<br />
  4. 4. Mostly mathematical<br />Binary – takes two arguments: +,-,*,/<br />Unary – takes one argument: -,!<br />Boolean operators: !<br />String: +<br />Bitwise & Shift: Left outs<br />Work mostly with primitives<br />Some operators work with objects too =,==<br />Java operators<br />
  5. 5. Defines how an expression evaluates when several operators are present<br />Java has specific rules that determine the order of evaluation <br />multiplication and division happen before addition and subtraction <br />Use parentheses to make the order of evaluation explicit <br />Precedence<br />
  6. 6. public class Precedence { <br /> public static void main(String[] args) { <br />intx = 1, y = 2, z = 3; <br />inta = x + y - 2/2 + z; // (1) <br />int b = x + (y - 2)/(2 + z); // (2) <br />System.out.println("a = " + a + " b = " + b); <br /> } <br />} <br />/* Output: <br />a = 5 b = 1 <br />Precedence Test<br />
  7. 7. =<br />int a = 4; <br />Take the value of the right-hand side (often called the rvalue) and copy it into the left-hand side (often called the lvalue) <br />An rvalue is any constant, variable, or expression that produces a value.<br />An lvalue must be a distinct, named variable. <br />Rectangle rectangle = new Rectangle();<br />Assignment Operator<br />
  8. 8. + , - , * , / , %<br />Operate and assign<br />+= ,-=,*=,/=,%<br />Unary operators<br />- (inverts the sign on the operand),+ (useless)<br />x = -a; <br />x = a * (-b); <br />Mathematical Operators<br />
  9. 9. Exercise<br />
  10. 10. Write a Java program to declare to int variables and assign them literal values<br />Perform different mathematical operations and assign the values to result variables<br />Print the operands and results<br />Print : System.out.println(5/2)<br />Integer division special feature<br />
  11. 11. ++ : increase by one unit<br />-- : decrease by one unit<br />a = 5; <br />b = a++; //post-increment : assign  increase<br />b = a--; //pre-increment : assign  decrease<br />b = ++a;<br />b = --a;<br />Increment Decrement Operator<br />
  12. 12. Exercise<br />
  13. 13. Generate a booleanresult<br />They evaluate the relationship between the values of the operands<br />A relational expression produces true if the relationship is true, and false if the relationship is untrue<br />less than (<), greater than (>), less than or equal to (<=), greater than or equal to (>=), equivalent (==) and not equivalent (!=)<br />Relational Operators<br />
  14. 14. Exercise<br />
  15. 15. Write a simple Java program and declare two int variables.<br />Assign them values<br />Try out the relational operators one by one<br />System.out.println(a>b);<br />Check out equivalence with Objects<br />Integer n1 = new Integer(47); <br /> Integer n2 = new Integer(47); <br />System.out.println(n1 == n2); <br />System.out.println(n1 != n2);<br />Equivalence <br />Compare object references not values<br />Override equals method that exist with all objects<br />
  16. 16. Works on boolean operands<br />Logical operators AND (&&), OR (||) and NOT (!) produces a boolean value of true or false based on the logical relationship of its arguments<br />(A > B) && (C > D)<br />(A > B) || (C > D)<br />!A<br />Logical Operators<br />
  17. 17. Continue from relational operator exercise and declare two more int variables<br />Now try &&, || and !<br />Exercise<br />
  18. 18. Expression will be evaluated only until the truth or falsehood of the entire expression can be unambiguously determined<br />The latter parts of a logical expression might not be evaluated<br />Short circuit<br />
  19. 19. public class ShortCircuit { <br /> static boolean test1(intval) { <br />s.o.p("result: " + (val < 1)); <br /> return val < 1; <br /> } <br /> static boolean test2(intval) { <br />s.o.p("result: " + (val < 2)); <br /> return val < 2; <br /> } <br /> static boolean test3(intval) { <br />s.o.p("result: " + (val < 3)); <br /> return val < 3; <br /> } <br /> public static void main(String[] args) { <br />booleanb = test1(0) && test2(2) && test3(2); <br />s.o.p("expression is " + b); <br /> } <br />}<br />Short circuit blow-up<br />
  20. 20. One special usage of an operator in Java<br />The + and += operators can be used to concatenate strings<br />Operator overloading<br />String a = “AIR”; <br />String b = “INDIA”<br />System.out.println(a + b);<br />int c = 747<br />System.out.println(a + b + c);<br />String Operators<br />
  21. 21. Conditional operator, is unusual because it has three operands. <br />It is truly an operator because it produces a value, unlike the ordinary if-else<br />boolean-exp ? value0 : value1<br />If boolean-exp evaluates to true, value0 is evaluated, and its result becomes the value produced by the operator. <br />If boolean-exp is false, value1 is evaluated and its result becomes the value produced by the operator.<br />Ternary Operator<br />
  22. 22. int a = 5;<br />int b = 6;<br />int c = (a > b) ? a : b ; <br />Try out ternary operator<br />
  23. 23. Casting into a mold<br />Automatically change one type of data into another when appropriate<br />if you assign an integral value to a floating point variable, the compiler will automatically convert the int to a float<br />Casting allows you to make this type conversion explicit, or to force it when it wouldn’t normally happen. <br />To perform a cast, put the desired data type inside parentheses to the left of any value. <br />Casting Operators<br />
  24. 24. inti = 200; <br />long lng = (long)i; <br />lng = i; // "Widening," so cast not really required <br />long lng2 = (long)200; <br />lng2 = 200; <br />// A "narrowing conversion": <br />i = (int)lng2; // Cast required <br />Casting Operators<br />
  25. 25. It’s possible to perform a cast on a numeric value as well as on a variable <br />You can introduce superfluous casts; for example, the compiler will automatically promote an int value to a long when necessary. However, you are allowed to use superfluous casts to make a point or to clarify your code<br />In other situations, a cast may be essential just to get the code to compile. <br />Java allows you to cast any primitive type to any other primitive type, except for boolean, which doesn’t allow any casting at all. <br />Cast works with Objects too.<br />Circle circle = (Circle) shape; <br />Casting Operators<br />
  26. 26. If you cast from a floating point value to an integral value, what does Java do? <br />double above = 0.7, below = 0.4; <br /> float fabove = 0.7f, fbelow = 0.4f; <br /> print("(int)above: " + (int)above); <br /> print("(int)below: " + (int)below); <br /> print("(int)fabove: " + (int)fabove); <br />Casting from a float or double to an integral value always truncates the number. <br />If instead you want the result to be rounded, use the round( ) methods in java.lang.Math<br />print("Math.round(above): " + Math.round(above)); <br />print("Math.round(below): " + Math.round(below)); <br />print("Math.round(fabove): " + Math.round(fabove)); <br />Truncation & Rounding<br />
  27. 27. If you perform any mathematical or bitwise operations on primitive data types that are smaller than an int (that is, char, byte, or short), those values will be promoted to int before performing the operations, and the resulting value will be of type int. <br />You want to assign back into the smaller type, you must use a castand take care of data loss<br />In general, the largest data type in an expression is the one that determines the size of the result of that expression; <br />if you multiply a float and a double, the result will be double; if you add an int and a long, the result will be long. <br />Promotion<br />
  28. 28. Q&A<br />

×