The assignment expressions The assignment operator in an
- Slides: 21
The assignment expressions
The assignment operator in an assignment statement • We have seen the assignment statement: var = expr; • Effect: • 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: var = expr Result returned by an assignment expression: 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 Assign. Expr 01 { 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: a=2 which is an assignment expression • The assignment expression returns the value 2 Therefore, System. out. println will print the value 2
The assignment expression (cont. ) • Example Program: (Demo above code) – Prog file: http: //mathcs. emory. edu/~cheung/Courses/170/Syllabus/04/Progs/ Assign. Expr 01. java • How to run the program: • Right click on link and save in a scratch directory • To compile: javac Assign. Expr 01. java • To run: java Assign. Expr 01
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 Assign. Expr 02 { 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/ Assign. Expr 02. java • How to run the program: • Right click on link and save in a scratch directory • To compile: javac Assign. Expr 02. java • To run: java Assign. Expr 02
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 Assign. Expr 03 { 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/ Assign. Expr 03. java • How to run the program: • Right click on link and save in a scratch directory • To compile: javac Assign. Expr 03. java • To run: java Assign. Expr 03
Exercise • What will the following Java program print: public class Assign. Expr 04 { 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/ Assign. Expr 04. java • How to run the program: • Right click on link and save in a scratch directory • To compile: javac Assign. Expr 04. java • To run: java Assign. Expr 04
- Pada tipe data boolean, berlaku operator-operator :
- Prioritas operator
- Unary and binary operations
- Pada tipe data boolean, berlaku operator-operator adalah
- Compound operators in c
- What is shorthand operator in java
- Assignment operator in relational algebra
- Difference between copy constructor and assignment operator
- Assignment operator in relational algebra
- Assignment operator in relational algebra
- Addition assignment operator
- Expresi adalah
- Example of assignment operator
- Examples of ternary operator in c
- Assignment operator c++ linked list
- Copy assignment operator c++
- Assignment expressions
- Hát lên người ơi alleluia
- Khi nào hổ con có thể sống độc lập
- Diễn thế sinh thái là
- đại từ thay thế
- Vẽ hình chiếu vuông góc của vật thể sau