1 / 21

The assignment expressions

This article explains assignment expressions and operators in Java, including their effect and how they return values. It also covers the priority and associativity of assignment operators. Example programs are provided to illustrate the concepts.

josephdwyer
Download Presentation

The assignment expressions

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. 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 operatoralsoreturns 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:

  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

More Related