2018年3月2日 星期五

Compound Assignment Operators

There are actually 11 or so compound assignment operators, but only the four most commonly used (+=, -=, *=, and /=),are on the exam (despite what the objectives say).
The compound assignment operators let lazy typists shave a few keystrokes off their workload. Here are several example assignments, first without using a compound operator,

y =  y - 6;
x = x + 2 * 5;

Now, with compound operators:

y -= 6;
x += 2 * 5;

The last two assignments give the same result as the first two.

Given:

1. public class TestString1{
2.     public static void main(String[] args){
3.         String str = "420";
4.         str += 42;
5.         System.out.print(str);
6.     }
7. }

What is the output?

A. 42
B. 420
C. 462
D. 42042
E. Compilation fails.
F. An exception is thrown at runtime.

沒有留言:

張貼留言