Đang chuẩn bị liên kết để tải về tài liệu:
Sams Teach Yourself Java 6 in 21 Days 5th phần 2

Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ

Sử dụng một biểu thức để thay đổi giá trị của một biến là một nhiệm vụ phổ biến trong lập trình. Các nhà khai thác được sử dụng đúng trong những trường hợp này. Bảng 2.4 cho thấy các nhà khai thác chuyển nhượng và các biểu thức có chức năng tương đương với. | Simpo PDF Merge and Split Unregistered VerSfiBff-W WWWSimPopdf.com More About Assignment Assigning a value to a variable is an expression because it produces a value. Because of this feature you can string assignment statements together the following way x y z 7 In this statement all three variables end up with the value of 7. The right side of an assignment expression always is calculated before the assignment takes place. This makes it possible to use an expression statement as in the following code int x 5 2 x x 2 In the expression x x 2 the first thing that happens is that x 2 is calculated. The result of this calculation 7 is then assigned to x. Using an expression to change a variable s value is a common task in programming. Several operators are used strictly in these cases. Table 2.4 shows these assignment operators and the expressions they are functionally equivalent to. TABLE 2.4 Assignment Operators Expression Meaning x y x x y x - y x x - y x y x x y x y x x y CAUTION These shorthand assignment operators are functionally equivalent to the longer assignment statements for which they substitute. If either side of your assignment statement is part of a complex expression however there are cases where the operators are not equivalent. For example if x equals 20 and y equals 5 the following two statements do not produce the same value x x y 5 x y 5 When in doubt simplify an expression by using multiple assignment statements and don t use the shorthand operators. 52 Simpo2PDF Mergef andrSplit Unregistered Version - http www.simpopdf.com Incrementing and Decrementing Another common task required in programming is to add or subtract 1 from an integer variable. There are special operators for these expressions which are called increment and decrement operations. Incrementing a variable means to add 1 to its value and decrementing a variable means to subtract 1 from its value. The increment operator is and the decrement operator is --. These operators are placed