1 / 41

Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas

C. ertificación en. J. AVA. Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas. Unary Operators Arithmetic Operators Shift Operators Comparison Operators Bitwise Operators Short-circuit logical Operators Ternary Operator Assignment Operators.

Download Presentation

Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C ertificación en J AVA Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas

  2. Unary Operators • Arithmetic Operators • Shift Operators • Comparison Operators • Bitwise Operators • Short-circuit logical Operators • Ternary Operator • Assignment Operators 2. OPERATORS AND ASSIGNMENTS

  3. Operator • Type Operators • Unary ++ -- + - ! ~ () • Arithmetic * / % + - • Shift << >> >>> • Comparison < <= > >= instanceof • == != • Bitwise & ^ | • Short-circuit && || • Ternary ?: • Assignment = “op=“

  4. 1. int [] a={4, 4}; 2. int b=1; 3. a[b]=b=0; Evaluation Order All operands are evaluated left to right a[1]=b=0;

  5. Unary Operators The Increment and Decrement Operators ++ and -- • Modify the value of an expression by adding or subtracting 1 • To the left of an expression: the expression is modififed before it takes part in the rest of the calculation • To the right of an expression: the value used in the rest of the calculation is the original value of that expression

  6. Unary Operators ... Initial Expression Final value Final value value of x of y of x 5 y=x++ 5 6 5 y=++x 6 6 5 y=x-- 5 4 5 y=--x 4 4

  7. 1. x=-3; 2. y=+3; 3. z=-(y+6); Unary Operators ... The Unary + and - Operators • + has no effect • - negates an expression z is initialized to -9

  8. Example ~00001111  11110000 Unary Operators ... Bitwise inversion operator: ~ • bitwise inversion on integral types • For each primitive type, a platform independent representation is used • Converts all the 1 bits in a binary value to 0s and all the 0 bits to 1s

  9. ! true  false ! false  true Unary Operators ... Boolean Complement operator: ! • Inverts the value of a boolean expression • Converts all the 1 bits in a binary value to 0s and all the 0 bits to 1s

  10. public Object MyMethod(Object x) { • if (x instanceof String) { • // do nothing • } • else { • x=x.toString(); • } • return x; • } • public Object MyMethod(Object x) { • if ( !(x instanceof String) ) { • x=x.toString(); • } • return x; • }

  11. int circum=(int)(Math.PI*diameter); Vector v=new Vector(); v.addElement(“Hello”); String s=(String)v.elementAt(0); Unary Operators ... Cast operator: (type) • explicit conversion of the type of an expression • Change the type of primitive values • Casts can also be applied to object references

  12. Storing 64 *4 in a byte variable will give a value of 0 Arithmetic Operators Multiplication and division operators: * and / • On all primitive numeric types and char • Integer division can generate an ArithmeticException from a division by zero • when you divide with integer arithmetic, the result is forced into an integer • You should multiply first and then divide

  13. int a =12345, b=234567, c, d; • long e, f; • c =a * b / b; • d =a / b * b; • System.out.println(“a is”+a+ • “\nb is “+ b+ • “\nc is “+ c+ • “\nd is “+ d); • e =(long)a * b / b; • d = (long) a / b * b; • System.out.println( • “\ne is “+ e+ • “\nf is “+ f); Output a is 12345 b is 234567 c is -5965 d is 0 e is 12345 f is 0 The result is correct if the representation is wide enough

  14. Arithmetic Operators ... Modulo Operator: % • Gives the remainder of a division • Generally applied to two integers, although it can be applied to floating point numbers too Reduce the magnitude of the left-hand operand by the magnitude of the right-hand one. Repeat this until the magnitude of the result is less than the magnitude of the right-hand operand.

  15. 1. 7 % 4 is equal to 3 2. 7.6 % 2.9 is equal to 1.8 3. -5 % 2 is equal to –1 4. -5 % -2 is equal to -1 Arithmetic Operators ... The modulo operation can throw an ArithmeticException if applied to integral types and the second operand is zero Rule of thumb: Drop any negative signs from either operand and calculate the result. Then, if the original left-hand operand was negative, negate the result. The sign of the right-hand operand is irrelevant

  16. Java does not allow the programmer to perform operator overloading!!! Arithmetic Operators Addition and subtraction operators: + and - • Apply to operands of any numeric type • + is also permitted where either operand is a String object. The other operand is used to create a String object Java overloads + to support concatenation of String objects

  17. java.lang.Object@1cc6dd How operands are converted to String objects? • By invoking the toString() method of that object (it is defined in java.lang.Object()) • To convert an operand of some object type to a String, the conversion is performed by a static method in a container class, such as Integer.toString() • The toString() method produces a String that contains the name of the object’s class and some identifying value – typically separated by the at symbol @ It is a good idea to define a helpful toString() method in all your classes

  18. + with two operands of primitive numeric type, the result • Is of a primitive numeric type • Is at least int, because of normal promotions • Is of a type at least as wide as the wider type of the two operands • Has a value calculated by promoting the operands to the result type, then performing the addition calculation using that type. This might result in overflow or loss of precision + with any operand that is not of primitive numeric type • One operand must be a String object, otherwise the expression is illegal • If both operands are not String objects, the non-String operand is converted to a String, and the result is the concatenation of the two

  19. Arithmetic Error Conditions • Integer division by zero results in an ArithmeticException • Integer calculations, other than division by zero, that cause overflow or similar error, simply leave the final bit pattern in the result • NaN values are used to indicate that a calculation has no result in ordinary arithmetic

  20. 1. x < Float.NaN 2. x <= Float.NaN 3. x == Float.NaN 4. x > Float.NaN 5. x >= Float.NaN Comparisons with Not a Number • Two NaN values are defined in the java.lang package (Float.Nan and Double.NaN) and are considered non-ordinal for comparisons return false Float.NaN != Float.NaN returns true • To test for a NaN result use the Float.isNaN(float) or Double.isNaN(double) static methods in the java.lang package

  21. The Shift Operators: <<, >> and >>> • Taking the binary representation of a number and moving the bit pattern left or right • Shift operators may be applied to arguments of integral types (they should be only applied to operands of either int or long type)

  22. Original data 192 In binary 00000000 00000000 00000000 11000000 Shifted left 1 bit 0 00000000 00000000 00000001 1000000? Shifted right 1 bit ?0000000 00000000 00000000 01100000 0 Shifted left 4 bits 0000 00000000 00000000 00001100 0000???? Original data -192 In binary 11111111 11111111 11111111 01000000 Shifted left 1 bit 1 11111111 11111111 11111110 1000000? Shifted right 1 bit ?1111111 11111111 11111111 00100000 0 bits that move off the end of a representation are discarded

  23. Original data 192 In binary 00000000 00000000 00000000 11000000 Shifted right 1 bit 00000000 00000000 00000000 01100000 Shifted right 7 bits 00000000 00000000 00000000 00000001 Original data -192 In binary 11111111 11111111 11111111 01000000 Shifted left 1 bit 11111111 11111111 11111110 10100000 Shifted right 7 bits 11111111 11111111 11111111 11111110 Shifting Negative Numbers (<< and >>) << and >>>: the new bits are set to zero >>: the new bits take the value of the most significant bit before the shift

  24. Reduction of the right hand operand The right-hand argument of the shift operators is taken to be the number of bits by which the shift should move It should be smaller than the number of bits in the result (less than 32 for an int type and less than 64 for a long type) If it exceeds these limits, a new value is calculated by reducing the supplied value modulo the number of bits

  25. Arithmetic promotion of operands Takes place before any binary operator is applied so that all numeric operands are at least int type

  26. Original data (-64 decimal)11000000 In binary 11111111 11111111 11111111 11000000 Shifted right unsigned 00001111 11111111 11111111 11111100 4bits gives Truncate to byte gives 11111100 Expected result was 00001100 Calculation for –64 >>> 4 >>>: unsigned right-shift operator

  27. 1. Ordinal: test the relative value of numeric operands 2. Object type: determine if the runtime type of an object is of a particular type or a subclass of that type 3. Equality: test if two values are the same and may be applied to values of non-numeric types The Comparison Operators • All return a boolean result • Arithmetic promotions are applied. Java promotes the smaller type to the larger type.

  28. 1. p < q 2. f < q 3. f <= c 4. c > r 5. c >= q • int p=9; • int q=65; • int r=-12; • float f=9.0F; • char c=’A’; Ordinal Comparisons with <, <=, >, and >= all return true

  29. Left-hand operator: object reference expression (variable or an array element) • Right-hand operator: class, interface, or array (You cannot use a java.lang.Class object or its string name) The instanceof Operator Ttests the class of an object at runtime

  30. public class Classroom { • private Hashtable inTheRoom = new Hashtable(); • public void enterRoom( Person p) { • inTheRoom.put(p.getName(), p ); • } • public Person getParent(String name) { • Object p=inTheRoom.get(name); • if (p instanceof Parent) { • return (Parent)p; • } • else { • return null; • } • } • }

  31. if (x instanceof Component[]) if (x instanceof []) Test if a reference refers to an array You cannot test for “any array of any element type” If the left-hand argument is a null value, the instanceof test simply returns false

  32. The Equality comparison Operators: == and != • Test for equality and inequality, returning a boolean value • For primitve types, the concept of equality is subject to promotion rules A float value 10.0 is equal to a byte value of 10 • For variables of object type, the value is taken as the reference to the object (the memory address) • To achieve a content or semantic comparison, you MUST use the equals() method

  33. &: AND • ^: XOR • |: OR Bitwise Operators: &, ^ and | Applicable to integral types

  34. The AND OperationOp1 Op2 Op1 AND op2 0 0 0 0 1 0 1 0 0 1 1 1 The XOR OperationOp1 Op2 Op1 XOR op2 0 0 0 0 1 1 1 0 1 1 1 0 The OR OperationOp1 Op2 Op1 OR op2 0 0 0 0 1 1 1 0 1 1 1 1

  35. The AND OperationOp1 Op2 Op1 AND op2 on Boolean values false false false false true false true false false true true true The XOR OperationOp1 Op2 Op1 XOR op2 on Boolean values false false false false true true true false true true true false The OR OperationOp1 Op2 Op1 OR op2 on Boolean values false false false false true true true false true true true true

  36. For any boolean value X false AND X  false true OR X  true The Short-Circuit Logical Operators: && and || • Provide logical AND and OR operations on boolean types • Have the ability to “short-circuit” a calculation if the result is definitely known

  37. if (s != null) { • if (s.length() > 20) { • System.out.println(s); • } • } • if ( (s != null) && (s.length() > 20) ) { • System.out.println(s); • }

  38. The (boolean) expression left of the ? is evaluated. If true, the result of the whole expression is the value of the sub-expression to the left of the colon; Otherwise it is the value of the sub-expresion to the right of the colon The Ternary Operator: ?: Provides a way to code simple conditions (if/else) into a single expression (boolean exp) ? (exp1): (exp2);

  39. if (x) { • a=b; • } • else { • a=c; • } a =x ? b : c; equivalent to • The types of b and c ahould be compatible and are made identical through conversion • The type of x should be boolean • The type of b and c should be assignment compatible with the type of a • The value assigned to a will be b if x is true or will be c if x is false

  40. = simple assignment • op= calculate and assign • x op= y is a shorthand for x= x op y The Assignment Operators • Set the value of a variable or expression to a new value • Assignments are supported by a battery of operators • Assignment of object references copies the reference value, not the object body

  41. a = b = c = 0; It is executed from right to left, so that first 0 is assigned into the variable c. Then, the assignment of b takes place. Finally, the variable a is also set to zero The Assignment Operators ...

More Related