The assignment expressions. The assignment operator in an assignment statement We have seen the...
-
Upload
dwayne-atkinson -
Category
Documents
-
view
214 -
download
0
Transcript of The assignment expressions. The assignment operator in an assignment statement We have seen the...
The assignment expressions
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
The assignment expression
• Consider the following arithmetic expression:
int a = 3; int b = 4;
Arithmetic expression: a + b Returns the result: 7
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
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
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 } }
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
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
The result returned by the assignment operators
• We have seen many different assignment operators:
• =
• +=
• -=
• *=
• /=
• %=
The result returned by the assignment operators (cont.)
• Result returned by the assignment operators:
Operator Example Returned value
= a = 2 2
+= a += 2 a + 2
-= a -= 2 a - 2
*= a *= 2 a * 2
/= a /= 2 a / 2
%= a %= 2 a % 2
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); } }
The result returned by the assignment operators (cont.)
• Output:
2 7 3 10 2 1
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
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
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
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 } }
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) ^^^^^
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
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); } }
Exercise (cont.)
• Answer:
4
-3
-6
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