Chapter 4: (Best web site) Making the Most of Variables and

Chapter 4: Making the Most of Variables and Their Values 95 in Listing 4-7, you see an error message (whatsLeft is already defined) when you try to compile your code. To find out what a block is, see Chapter 5. Then, for some honest talk about redeclaring variables, see Chapter 10. The increment and decrement operators Java has some neat little operators that make life easier (for the computer s processor, for your brain, and for your fingers). Altogether, four such operators exist two increment operators and two decrement operators. The increment operators add 1, and the decrement operators subtract 1. The increment operators use double plus signs (++), and the decrement operators use double minus signs (–). To see how they work, you need some examples. The first example is in Figure 4-10. A run of the program in Figure 4-10 is shown in Figure 4-11. In this horribly uneventful run, the count of bunnies is printed three times. The double plus signs go by two different names, depending on where you put them. When you put the ++before a variable, the ++is called the preincrement operator. (The pre stands for before.) Figure 4-10: Using preincrement. numberOfBunnies becomes 28. import static java.lang.System.out; class preIncrementDemo { public static void main(String args[]) { 28 gets printed. int numberOfBunnies = 27; ++numberOfBunnies; out.println(numberOfBunnies); numberOfBunnies out.println(++numberOfBunnies);becomes 29, and 29 gets printed. out.println(numberOfBunnies); } 29 gets printed again. } Figure 4-11: A run of the code in Figure 4-10.
We feature a web hosting shopping cart and live support solution. Just try our web hosting shopping cartwhich provides a secure way of obtaining payments through your website.

Leave a Reply