Download presentation
Presentation is loading. Please wait.
Published byDwayne Atkinson Modified over 9 years ago
1
The assignment expressions
2
The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value of expression expr in the variable named var
3
The assignment expression Consider the following arithmetic expression: int a = 3; int b = 4; Arithmetic expression: a + b Returns the result: 7
4
The assignment expression (cont.) Definition: assignment expression: Result returned by an assignment expression: var = expr Result of var = expr is equal to the value of expr
5
The assignment expression (cont.) Note: The assignment operator will (still) update the value stored inside the variable var In addition to updating the variable. the assignment operator also returns a value
6
The assignment expression (cont.) Example: public class AssignExpr01 { public static void main(String[] args) { int a; a = 0; System.out.println(a); System.out.println(a = 2); // Prints 2 System.out.println(a); // Prints 2 }
7
The assignment expression (cont.) Explanation: System.out.println(a = 2) will print the value between its brackets The expression between the brackets is: which is an assignment expression The assignment expression returns the value 2 Therefore, System.out.println will print the value 2 a = 2
8
The assignment expression (cont.) Example Program: (Demo above code) –Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/Progs/ AssignExpr01.java How to run the program: Right click on link and save in a scratch directory To compile: javac AssignExpr01.java To run: java AssignExpr01
9
The result returned by the assignment operators We have seen many different assignment operators: = += -= *= /= %=
10
The result returned by the assignment operators (cont.) Result returned by the assignment operators: Operator Example Returned value =a = 22 +=a += 2a + 2 -= a -= 2a - 2 *=a *= 2a * 2 /= a /= 2a / 2 %=a %= 2a % 2
11
The result returned by the assignment operators (cont.) Example: public class AssignExpr02 { public static void main(String[] args) { int a; a = 5; System.out.println(a = 2); a = 5; System.out.println(a += 2); a = 5; System.out.println(a -= 2); a = 5; System.out.println(a *= 2); a = 5; System.out.println(a /= 2); a = 5; System.out.println(a %= 2); }
12
The result returned by the assignment operators (cont.) Output: 2 7 3 10 2 1
13
The result returned by the assignment operators (cont.) Example Program: (Demo above code) –Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/Progs/ AssignExpr02.java How to run the program: Right click on link and save in a scratch directory To compile: javac AssignExpr02.java To run: java AssignExpr02
14
Priority and associativity of the assignment operators Priority of the assignment operators: All of the assignment operators have the lowest priority among all operators in Java
15
Priority and associativity of the assignment operators Associativity of the assignment operators: All of the assignment operators are right associative Meaning: When there are multiple assignment operators in an expression, the expression is evaluate from right to left
16
Priority and associativity of the assignment operators (cont.) Example: cascaded assignment public class AssignExpr03 { public static void main(String[] args) { int a, b, c; c = b = a = 4 + 2; // Cascade assignment System.out.println(a); // Prints 6 System.out.println(b); // Prints 6 System.out.println(c); // Prints 6 }
17
Priority and associativity of the assignment operators (cont.) Explanation: c = b = a = 4 + 2; is evaluated as follows: c = b = a = 4 + 2; higher priority ^^^^^ Reduces to: c = b = a = 6; from right to left ^^^^^ assigns 6 to variable a and returns 6 Reduces to: c = b = 6; from right to left ^^^^^ assigns 6 to variable b and returns 6 Reduces to: c = 6; assigns 6 to variable c (and returns 6) ^^^^^
18
Priority and associativity of the assignment operators (cont.) Example Program: (Demo above code) –Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/Progs/ AssignExpr03.java How to run the program: Right click on link and save in a scratch directory To compile: javac AssignExpr03.java To run: java AssignExpr03
19
Exercise What will the following Java program print: public class AssignExpr04 { public static void main(String[] args) { int a, b, c; a = 1; b = 1; c = 2; c *= b -= a += 1 + 2; System.out.println(a); System.out.println(b); System.out.println(c); }
20
Exercise (cont.) Answer: 4 -3 -6
21
Priority and associativity of the assignment operators (cont.) Example Program: (Verify for yourself...) –Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/Progs/ AssignExpr04.java How to run the program: Right click on link and save in a scratch directory To compile: javac AssignExpr04.java To run: java AssignExpr04
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.