简体   繁体   中英

Java - Boolean used with If statement

I just started learning Java at school and I encountered the following piece of code. I have trouble understanding why the output shows: no . Since x is updated to 100 shouldn't the boolean also update to false and therefore output: yes ?

Thank you in advance

Here's my code:

   public static void main (String[] args)
   {
       Scanner keyboard = new Scanner(System.in);
       int x = -555;
       boolean isNegative = (x < 0);
       if (isNegative)
       {
           x = 100;
           if (isNegative)
             System.out.println("no");
          else
             System.out.println("yes");
       } 
       else
          System.out.println("maybe");

  }

So, you set x to -555 before you check if it's negative. you then set the boolean isNegative to whether x is smaller than 0, at which point it will be false. You update x later in the program, so that if statement is not affected. The reason it's saying no is because it's given is negative, which is not updated throughout the program. Keep in mind Java runs from top to bottom.

Also something I noticed, is that the code that prints out yes or no is in the block of code that runs if it's negative. You may want to fix that.

While the integer x was updated, the boolean isNegative was not. Even though x is set to 100 later on, isNegative has already been set to true , and after that one line boolean isNegative = (x<0); , isNegative no longer depends on the value of x .

In the code below, the output is "yes" , because isNegative is updated after x is updated.

   int x = -555;
   boolean isNegative = (x < 0);
   if (isNegative)
   {
       x = 100;
       isNegative = (x<0);
       if (isNegative)
         System.out.println("no");
      else
         System.out.println("yes");
   } 
   else
      System.out.println("maybe");

And ignore my formatting mistakes on my part as well, this is my first time answering ;)

In functional spirit, you could declare an IntPredicate:

IntPredicate negative = (i)-> i < 0;

Note the martial arrow operator! :)

Usage:

 negative.test (-500)
 negative.test (500)

A predicate, in programming, is usually a method/function, which returns a boolean for some input. Input is here an int, and for the basic types, there a specialized Predicates like IntPredicate.

While a somewhat brief construction is possible, the overall overhead is pretty high. Compare yourself:

Sample code:

   boolean isNegative = (x < 0);
   if (isNegative)
   {
       x = 100;
       if (isNegative)
           System.out.println("no");
       else
           System.out.println("yes");
   } 
   else
       System.out.println("maybe");

Lean code:

   if (x < 0)
   {
       x = 100;
       if (x < 0)
           System.out.println("no");
       else
           System.out.println("yes");
   } 
   else
       System.out.println("maybe");

Predicate usage:

   IntPredicate negative = (i)-> i < 0;
   if (negative.test (x))
   {
       x = 100;
       if (negative.test (x))
           System.out.println("no");
       else
           System.out.println("yes");
   } 
   else
       System.out.println("maybe");

It has it's usage surely for more complicated tests, which you only want to write once, and which aren't that self documenting like (x < 0), and where a bunch of such tests can be combined:

   IntStream is = ...
   is.filter (! negative).
      filter (prime).
      filter (square).
      filter (luckyNumber).
      filter (...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM