Precidence of NOT

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:

No. In this defective fragment, the NOT (the !) applies directly to cost.

Precidence of NOT

The NOT operator has high precedence, so it is done before arithmetic and relational operators unless you use parentheses. Examine the following:

!cost < 50 
-----  
illegal: can't use ! on an arithmetic variable

Since ! has high precedence, the above says to apply it to cost. This which won't work, because cost is an integer and NOT applies only to boolean values.

QUESTION 24:

Look at this fragment:

if ( cost < 50  )
  System.out.println("Acceptable shoes");
else
  System.out.println("Reject these shoes");

Is the new fragment equivalent to the original fragment:

if (  !(cost < 50)  )
  System.out.println("Reject these shoes");
else
  System.out.println("Acceptable shoes");

Try a few trial costs with each fragment to see if they are equivalent.