开发者

& symbol in php equivalent in Java

开发者 https://www.devze.com 2023-01-12 11:45 出处:网络
Can the following <?=($Num&1) ? \"odd\" : \"even\"?开发者_高级运维> translate to an equivalent in Java?String s = (num % 2 == 0) ? \"even\" : \"odd\";

Can the following

<?=($Num&1) ? "odd" : "even"?开发者_高级运维>

translate to an equivalent in Java?


String s = (num % 2 == 0) ? "even" : "odd";

To be thorough, that isn't the same operator that you're using (bitwise AND). Bitwise AND is the same in java, so you could also write your php line like:

String s = (num & 1 == 0) ? "even" : "odd";


A direct translation would be:

string s = ( num & 1 != 0 ) ? "odd" : "even"

* note: not entirely sure if the !=0 part is strictly necessary.

0

精彩评论

暂无评论...
验证码 换一张
取 消