Compound Assignment Operators Flashcards

1
Q

Give a pitfall related to compound
assignement operators !

A

A compound assignement operator is a shorthand operator that combines an arithmetic operator with assignment

PITFALL

A compound assignment operator cannot be used to declare a variable. It is used to update the value of an existing variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Give a key characteristic of the
assignement operator !

A

A characteristic of the assignement operator is that it returns the value its right expression

int y = (x = 5); // y is assigned the value 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Give a pitfall of related to compound
operators and casting !

A

A key characteristic of the compound assignment operator is that it often performs implicit casting when types are compatible, but explicit casting may be required when the types are incompatible.

long x = 10;
int y = 5;
y *= x; // COMPILES SUCCESSFULLY

int x = 10;
double y = 5.7;
Object o = y ;
x += (double) o; // Compiles Successfully
x += o; // Error: possible lossy conversion

How well did you know this?
1
Not at all
2
3
4
5
Perfectly