简体   繁体   English

操作员! 不能应用于int

[英]Operator ! cannot be applied to int

In C++, !1 means false which is equivalent to 0 . 在C ++中, !1表示false ,它等于0 I apply the same rule in Java, but IDE told me ! 我在Java中应用了相同的规则,但是IDE告诉我! cannot be applied to int. 不能应用于int。 Is there any way in Java to apply ! Java中有什么方法可以应用! to int instead of just writing true or false ? 诠释而不是只写true还是false

No, not in Java. 不,不是Java。
In java ! 在java中! can operate only on boolean as negate operator, 1/0 are not considered as booleans in Java 只能以布尔值作为取反运算符进行操作,在Java中1/0不被视为布尔值

! only applies to Boolean values in Java, or the results of conditional statements. 仅适用于Java中的布尔值或条件语句的结果。 The closest thing you could do is if (x == 1) or boolean y = x == 1 . 您可以做的最接近的事情是if (x == 1)boolean y = x == 1

Nope, operators do not work on primitive data types, with the exception of, of-course, booleans. 不,运算符不能对原始数据类型进行操作,当然,布尔值除外。 Using !0 would simply result in an error. 使用!0只会导致错误。 Even though 0s and 1s can be considered boolean values, they are not in Java. 即使0和1都可以视为布尔值,但它们不在Java中。 The negation operator is only for booleans. 否定运算符仅适用于布尔值。 Something like !0 would be: !0这样的东西是:

boolean alwaysfalse = (0 != 0);

or 要么

boolean alwaysTrue = (0 == 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运算符 ',=' 不能应用于 'int'、'int[]' - Operator '!=' cannot be applied to 'int', 'int[]' 运算符 &amp;&amp; 不能应用于 &#39;boolean&#39;, &#39;int - Operator && cannot be applied to 'boolean', 'int 错误消息“运算符'&&'无法应用于'int''boolean' - Error message "Operator '&&' cannot be applied to 'int' 'boolean' “运算符 &#39;&lt;&#39; 不能应用于 &#39;<lambda parameter> &#39;,&#39;int&#39;”在java中 - "Operator '<' cannot be applied to '<lambda parameter>', 'int' " in java 错误信息:运算符 &lt; 不能应用于 boolean,int - Error message: operator < cannot be applied to boolean,int 运算符“&”不能应用于byte,int,boolean - Operator “&” cannot be applied to byte, int, boolean 如何按位操作? (运算符&不能应用于Integer或int) - How to operate bitwise? (Operator & cannot be applied to Integer or int) 运算符&#39;==&#39;不能应用于&#39;java.lang.String&#39;,&#39;int错误 - Operator '==' cannot be applied to 'java.lang.String', 'int error 运算符&#39;&gt;&#39;不能应用于&#39;android.widget.EditText&#39;,&#39;int&#39; - Operator '>' cannot be applied to 'android.widget.EditText','int' 运算符“+”不能应用于“int”、“java.lang.Object” - Operator '+' cannot be applied to 'int', 'java.lang.Object'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM