Shorthand Assignments

Java provides special shorthand assignment operators that simplify the coding of certain assignment statements. Let’s begin with an example. The assignment statement shown here

Image

can be written, using Java shorthand, as

Image

Ask the Expert

Q: Since the short-circuit operators are, in some cases, more efficient than their normal counterparts, why does Java still offer the normal AND and OR operators?

A: In some cases you will want both operands of an AND or OR operation to be evaluated because of the side effects produced. Consider ...

Get Java, A Beginner's Guide, 5th Edition, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.