Either Order (usually)

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

Answer:

age >= 21 && age <= 35

Either Order (usually)

In most situations, the operands of AND can be in either order. The following

age >= 21 && age <= 35 

is the equivalent of this:

age <= 35 && age >= 21 

One false is enough to make the entire expression false, regardless of the false occurs.

Warning: If a boolean expression includes an assignment operator or method calls, then the order sometimes does matter. The reason for this involves the short-circuit optimization mentioned previously. Mostly you don't need to worry about this, but make a mental note about this potential problem.

Chapter 40 describes this situation in detail. For the examples in this chapter, the order of operands does not matter.

QUESTION 13:

Examine this expression:

( Monster.isAlive() && (hitpoints = Hero.attack()) < 50 )

Do you suspect that the order of operands in this expression matters?