Java multicast statements

I am learning some Java at the moment when I came across the following piece of code. I understand it as a typical ternary operator (for example, a line starting with "boolean a" below), but I cannot figure out how to read an expression in a line starting with "boolean b". Any help on how to read this line would be greatly appreciated! Thank!

public class Ternary{
    public static void main (String[] args){
        int x = 10;
        int i = 2;
        boolean a = x > 10 ? true: false;
        boolean b = a = true ? ++i > 2 ? true:false:false;
        System.out.print(b);
    }
}
+4
source share
7 answers

Break it as follows:

true ? (++i > 2 ? true 
                : false)
     : false;

So, here the test condition is always set to true. Thus, the ternar branch that is executed is a part ++i > 2 ? true : false.

, i , 2. , true. false.

. :

boolean b = a = (++ i > 2);

, , , . a, , a. , :

boolean b = a == true ? ++i > 2 ? true : false : false; //notice the ==

:

(a == true) ? (++i > 2 ? true
                       : false)
            : false;

a == true, a boolean, :

a ? (++i > 2 ? true
             : false)
  : false;

, a true. , , ( , , i 2), false.

:

boolean b = a && (++i > 2);
+5

! . , . :

// I assume that `a == true` instead of `a = true`
boolean b = a == true ? (++i > 2 ? true : false)
                      : false;

:

// a == true is better written as just `a`. You shouldn't do boolean comparison
// like that.
boolean b = a ? (++i > 2) : false;

// If that is really a = true, then you can break it as:
boolean b = a = true ? (++i > 2) : false;

:

// If that is `a == true`
boolean b = a && (++i > 2)

// If that is really a = true, then you can break it as:
boolean b = a = (++i > 2);

, :

boolean a = x > 10 ? true: false;

:

boolean a = x > 10;
+1

boolean b = a = true ? ++i > 2 ? true:false:false; :

a = true

a true true.

, ++i > 2 ++i > 2 ? true:false, . true.

0

true false

( a true).

, ++ 2 ( 3).

, true b. , false. false .

0

, :

boolean b = a = (true ? (++i > 2 ? true : false) : false);

, :

if (true) // # If Number:1
{
    if (++i > 2) // # If Number:2
    {
        a = true;            
    }
    else { a = false; }
}
else { a = false; }

if(true) Tautology If Number: 2 . ; :

if (++i > 2)
{
    a = true;            
}
else { a = false; }

; a = (++i > 2) ? true : false); : a = ++i > 2 b = a, ++i > 2.

0

!

, b = a = true ? ... b = a == true ? ..., (a ), false . , .

==, , , "spot the bug", , .

, . , .

 b = a == true ? ++i > 2 ? true:false:false;
 ... becomes ...
 b = (a == true) 
          ? (++i > 2 ? true:false)
          :false;

:

  • a == true a.
  • boolean x = a ? true : false; boolean x = a.

:

 b = a
     ? (++i > 2)
     :false;

 b = a && ( ++i > 2 );

"" , - , . , , , .

, true false. - , , - , .

, :

 shippingPrice = orderTotal >= freeShippingThreshold ? 0 : getStandardShipping();
0

With some guesses about what is being done, and some renaming of the variables, and assuming what a = trueshould have been a == true, you get:

    boolean failed = result > 10 ? true: false;
    boolean b = failed ? ++retries > 2 ? true:false:false;

This can then lead to a much more logical one:

    boolean failed = result > 10;
    boolean giveUp = failed && ++retries > 2;
0
source

Source: https://habr.com/ru/post/1529831/


All Articles