Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA."— Presentation transcript:

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

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

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

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

5 Unary Operators  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 The Increment and Decrement Operators ++ and --

6 Unary Operators... InitialExpression Final value Final value InitialExpression Final value Final value value of x of y of x 5y=x++5 6 5y=x++5 6 5y=++x6 6 5y=++x6 6 5y=x-- 5 4 5y=x-- 5 4 5y=--x 4 4 5y=--x 4 4

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

8  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 Bitwise inversion operator: ~ Example ~ 00001111  11110000 Unary Operators...

9  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 Boolean Complement operator: ! ! true  false ! false  true Unary Operators...

10 1. public Object MyMethod(Object x) { 2. if (x instanceof String) { 3. // do nothing 4. } 5. else { 6. x=x.toString(); 7. } 8. return x; 9. } 1. public Object MyMethod(Object x) { 2. if ( !(x instanceof String) ) { 3. x=x.toString(); 4. } 5. return x; 6. }

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

12 Arithmetic Operators Storing 64 *4 in a byte variable will give a value of 0  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 Multiplication and division operators: * and /

13 1.int a =12345, b=234567, c, d; 2.long e, f; 3. 3. 4.c =a * b / b; 5.d =a / b * b; 6.System.out.println(“a is”+a+ 7. “\nb is “+ b+ 8. “\nc is “+ c+ 9. “\nd is “+ d); 10. 10. 11. e =(long)a * b / b; 12.d = (long) a / b * b; 13.System.out.println( 14. “\ne is “+ e+ 15. “\nf is “+ f); Output 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...  Gives the remainder of a division  Generally applied to two integers, although it can be applied to floating point numbers too Modulo Operator: % 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 Arithmetic Operators... 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 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 The modulo operation can throw an ArithmeticException if applied to integral types and the second operand is zero

16 Arithmetic Operators Java does not allow the programmer to perform operator overloading!!!  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 Addition and subtraction operators: + and - Java overloads + to support concatenation of String objects

17 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 @ java.lang.Object@1cc6dd 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 Comparisons with Not a Number 1. x < Float.NaN 2. x <= Float.NaN 3. x == Float.NaN 4. x > Float.NaN 5. x >= Float.NaN  Two NaN values are defined in the java.lang package (Float.Nan and Double.NaN) and are considered non- ordinal for comparisons Float.NaN != Float.NaN returns true return false  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 data192 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 bits that move off the end of a representation are discarded

23 Original data192 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 ( >) >>: the new bits are set to zero >>: the new bits are set to zero >>: the new bits take the value of the most significant bit before the shift >>: 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 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 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 The Comparison Operators  All return a boolean result  Arithmetic promotions are applied. Java promotes the smaller type to the larger type. 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

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

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

30 1.public class Classroom { 2. private Hashtable inTheRoom = new Hashtable(); 3. public void enterRoom( Person p) { 4. inTheRoom.put(p.getName(), p ); 5. } 6. public Person getParent(String name) { 7. Object p=inTheRoom.get(name); 8. if (p instanceof Parent) { 9. return (Parent)p; 10. } 11. else { 12. return null; 13. } 14. } 15.}

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

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 1.&: AND 2.^: XOR 3.|: OR Applicable to integral types Applicable to integral types Bitwise Operators: &, ^ and |

34 The AND Operation Op1 Op2 Op1 AND op2 000 010 100 111 The XOR Operation Op1 Op2 Op1 XOR op2 000 011 101 110 The OR Operation Op1 Op2 Op1 OR op2 000 011 101 111

35 The AND Operation Op1 Op2 Op1 AND op2 on Boolean values false false false falsetruefalse true falsefalse truetrue true The XOR Operation Op1 Op2 Op1 XOR op2 on Boolean values falsefalsefalse falsetrue true true falsetrue truetrue false The OR Operation Op1 Op2 Op1 OR op2 on Boolean values falsefalsefalse falsetrue true true falsetrue true true true

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

37 1. if (s != null) { 2. if (s.length() > 20) { 3. System.out.println(s); 4. } 5.} 1. if ( (s != null) && (s.length() > 20) ) { 2. System.out.println(s); 3. }

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 Provides a way to code simple conditions (if/else) into a single expression Provides a way to code simple conditions (if/else) into a single expression The Ternary Operator: ?: (boolean exp) ? (exp1): (exp2);

39 equivalent to equivalent to 1.if (x) { 2.a=b; 3.} 4.else { 5.a=c; 6.} a =x ? b : c;  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 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 1. = simple assignment 2. op= calculate and assign x op= y is a shorthand for x= x op y

41 The Assignment Operators... 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


Download ppt "Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA."

Similar presentations


Ads by Google