Converting Boolean to Integer in Java without If-Statements

后端 未结 12 1969
盖世英雄少女心
盖世英雄少女心 2020-12-30 02:26

I\'m wondering if there\'s a way to convert a boolean to an int without using if statements (as not to break the pipeline). For example, I could write

int bo         


        
12条回答
  •  长情又很酷
    2020-12-30 03:10

    I can't say I recommend this. It's both slower than the ternary operator by itself, and it's too clever to be called good programming, but there's this:

    -Boolean.FALSE.compareTo(value)
    

    It uses the ternary under the covers (a couple of method calls later), but it's not in your code. To be fair, I would be willing to bet that there's a branch somewhere in the Python execution as well (though I probably only bet a nickel ;) ).

提交回复
热议问题