Web server logs - 100 Part II: Writing Your Own Java Programs
Wednesday, December 12th, 2007100 Part II: Writing Your Own Java Programs Figure 4-14: A run of the code in Listing 4-8. Listing 4-8 shows how versatile Java s assignment operators are. With the assignment operators, you can add, subtract, multiply, or divide a variable by any number. Notice how += 5adds 5 to numberOfBunnies, and how *= 2 multiplies numberOfBunniesby 2. You can even use another expression s value (in Listing 4-8, numberExtra) as the number to be applied. The last two lines in Listing 4-8 demonstrate a special feature of Java s assignment operators. You can use an assignment operator as part of a larger Java statement. In the next to last line of Listing 4-8, the operator subtracts 7 from numberOfBunnies, decreasing the value of numberOfBunniesfrom 172to 165. But then the whole assignment business is stuffed into a call to out.println, so the number 165is printed on the computer screen. Lo and behold, the last line of Listing 4-8 shows how you can do the same thing with Java s plain old equal sign. The thing that I call an assignment statement near the start of this chapter is really one of the assignment operators that I describe in this section. So, whenever you assign a value to something, you can make that assignment be part of a larger statement. Each use of an assignment operator does double duty as both a statement and an expression. In all cases, the expression s value equals whatever value you assign. For example, before executing the code out.println (numberOfBunnies -= 7), the value of numberOfBunniesis 172. As a statement, numberOfBunnies -= 7tells the computer to subtract 7 from numberOfBunnies(so the value of numberOfBunniesgoes from 172down to 165). As an expression, the value of numberOfBunnies -= 7is 165. So the code out.println(numberOfBunnies -= 7)really means out. println(165). The number 165 is displayed on the computer screen. For a richer explanation of this kind of thing, see the sidebar, Statements and expressions, earlier in this chapter.
Get account with us and you will get completely access to our free web templates database with over 10.000 templates in it to build your website.Don’t wait, go and check free web templates.