public class Millionaire {
public static void main(String args[]) {
double amountInAccount;
amountInAccount = 50.22;
amountInAccount = amountInAccount + 1000000.00;
System.out.print("You have $");
System.out.print(amountInAccount);
System.out.println(" in your account.");
System.out.println("Now you have even more! You have 2000000.00 in your account.");
}
}
public class Main {
public static void main(String[] args) {
int years;
int anniversaries;
years = 4;
anniversaries = years / 4;
System.out.println("Number of anniversaries: " + anniversaries);
years = 7;
anniversaries = years / 4;
System.out.println("Number of anniversaries: " + anniversaries);
years = 8;
anniversaries = years / 4;
System.out.println("Number of anniversaries: " + anniversaries);
}
}
Consider the last six statements in the main method:
System.out.println(i); // The value of i is 8
i++; // The value of i becomes 9
i = i++ + ++i; // See the "Statements and Expressions" sidebar...
9 11
// As an expression, the value of i++ is 9. As a statement, i++ adds 1 to i, making i be 10.
// Then, because i has become 10,
// As an expression, the value of ++i is 11. As a statement, ++1 adds 1 to i, but that doesn't matter because it's only temporary.
// As an expression, the value of i++ + ++i is 9 + 11, which is 20. So i is assigned the value 20.
System.out.println(i); // Prints 20
i = i++ + i++;
20 21
// As an expression, the value of the leftmost is i++ is 20. As a statement, the leftmost i++ adds 1 to i, making i be 21.
// Then, because i has become 21,
// As an expression, the value of the rightmost i++ is 21. As a statement, the rightmost i++ adds 1 to i, but that doesn't matter because it's only temporary.
// As an expression, the value of i++ + i++ is 20 + 21, which is 41. So i as assigned the value 41.
System.out.println(i); // Prints 41
jshell> int i = 8
i ==> 8
jshell> i++
$9 ==> 8
jshell> i
i ==> 9
jshell> i
i ==> 9
jshell> i++
$12 ==> 9
jshell> i
i ==> 10
jshell> ++i
$14 ==> 11
jshell> i
i ==> 11