Spaces:
Running
Running
b = a++ [add, keep initial a]; ++a [add, get new 'a']
Browse files
Week 7: Enum, Generic Type, Streams, write to file, class diagram/13A. Expression Statements+++
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Let's return to Java's 'expression statements' for a moment.
|
| 2 |
+
So far, we have mainly used expression statements
|
| 3 |
+
to increase and decrease the value of a variable in a statement,
|
| 4 |
+
for example in a 'for statement':
|
| 5 |
+
|
| 6 |
+
// i++ increases the value of i by one
|
| 7 |
+
for (int i=0; i<10; i++) {
|
| 8 |
+
System.out.println("i: " + i);
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
As the name suggests, incrementing and decrementing also works as part of an expression.
|
| 13 |
+
Although its use in this way may not be recommended, it's good to be aware of how the change works.
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
=========================================
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
The idea is that if the increment or decrement is after the variable,
|
| 23 |
+
- the value is FIRST READ into the expression
|
| 24 |
+
- and THEN CHANGED.
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
So the statement
|
| 28 |
+
|
| 29 |
+
int b = a++;
|
| 30 |
+
|
| 31 |
+
means that b INITIALLY GETS the CURRENT VALUE of a.
|
| 32 |
+
THEN the value of 'a' is INCREASED by one.
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
For example,
|
| 37 |
+
|
| 38 |
+
// from above
|
| 39 |
+
int a = 5;
|
| 40 |
+
int b = a++;
|
| 41 |
+
System.out.println("The value of a is " + a + " and b is " + b);
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
// explained later below
|
| 45 |
+
a = 5;
|
| 46 |
+
b = ++a;
|
| 47 |
+
System.out.println("The value of a is " + a + " amd b is " + b);
|
| 48 |
+
|
| 49 |
+
The program prints:
|
| 50 |
+
The value of a is 6 and b is 5 => a++ so 'a' increased from 5 to 6. BUT 'b' RETAINS INITIAL 'a' value of 5
|
| 51 |
+
The value of a is 6 and b is 6 => ++a so 'a' increased from 5 to 6. THEN 'b' GETS NEW 'a' value of 6
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
=========================================
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
If the VALUE CHANGE is in front of the variable,
|
| 63 |
+
it is performed BEFORE the EXPRESSION is EVALUATED.
|
| 64 |
+
|
| 65 |
+
So the expression
|
| 66 |
+
|
| 67 |
+
int b = ++a;
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
...means that the value of '
|
| 71 |
+
- a' is first increased by one and
|
| 72 |
+
- then b gets the changed value.
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
For example,
|
| 76 |
+
|
| 77 |
+
a = 5;
|
| 78 |
+
b = a--;
|
| 79 |
+
System.out.println("The value of a is " + a + " and b is " + b);
|
| 80 |
+
|
| 81 |
+
a = 5;
|
| 82 |
+
b = --a;
|
| 83 |
+
System.out.println("The value of a is " + a + " and b is " + b);
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
The program prints:
|
| 87 |
+
The value of a is 4 and b is 5 => a-- so 'a' decreases from 5 to 4. BUT 'b' keeps initial 'a' which is 5
|
| 88 |
+
The value of a is 4 and b is 4 => --a so 'a' decreases from 5 to 4. THEN 'b' gets new 'a' which is 4
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
|