Compound Assignment Operators Flashcards
Give a pitfall related to compound
assignement operators !
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.
Give a key characteristic of the
assignement operator !
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
Give a pitfall of related to compound
operators and casting !
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