NOT(!)、AND(&&) 等运算符在 java 中如何工作?
how does Operators such as NOT(!), AND(&&) etc work in java?
请帮助我,我不知道运算符(AND、NOT、XOR ,..ETC)在 java 中是如何工作的。我知道 AND 和 OR 的输出,但我对 NOT 毫无头绪。例如,我不完全理解诸如变量!=整数(i!= 3)之类的语句。我的意思是 NOT 运算符 work.for 示例如何在这里不起作用。
class Demo {
public static void main(String args[]) throws java.io.IOException {
char ch;
do {
System.out.print("Press a key followed by ENTER: ");
ch = (char) System.in.read(); // get a char
} while (ch != 'q');
}
}
如果你做了一些系统输出,你会发现:
char ch = 'l';
System.out.print(2 != 3);
--true, they are not equal
System.out.print('q' != 'q');
-- false, they are equals
System.out.print(ch != 'q');
-- true, they are not equals
这意味着,它们 !=
检查它们是否完全相同(注意在这种情况下用于原始类型,例如 int、char、bool 等。此运算符在对象中的工作方式不同,例如字符串)
!运算符是一元的。当应用于布尔值时,它会切换,例如
bool on = true;
System.out.print(!on); //false
System.out.print(on); //true
当在等号旁边使用时,它会检查值是否不相等。如果它们不相等,则 returns 为真。否则,它 returns 为假。例如,
int two = 2;
int three = 3;
System.out.print(two != three);
//returns true, since they are not equal
System.out.print(two == three);
//returns false, since they are not equal
int x = 4;
int y = 5;
!case a
表示不是 case a
x == y
表示 x 和 y 引用内存中的相同位置(要了解其含义,请参阅 this question)。
x != y
与上面相反,写成!(x == y)
(不是x等于y)是一样的
case a && case b
和运算符 - 案例 a 和 案例 b 都为真。
case a || case b
或运算符 - 情况 a 或 情况 b 都为真。
这里有相同的例子,所以一切都更清楚:
1==1 // true, as 1 is equal to 1
2==1 // false, as 1 is not equal to 2.
!true // false, as not true is false.
1!=1 // false, as 1 is equal to one.
!(1==1) // The same as above - exact same output.
// You need the brackets, because otherwise it will throw an error:
!1==1 // Error - what's '!1'? Java is parsed left-to-right.
true && false // false - as not all cases are true (the second one is false)
true || false // rue - as at least one of the cases are true (the first one)
请帮助我,我不知道运算符(AND、NOT、XOR ,..ETC)在 java 中是如何工作的。我知道 AND 和 OR 的输出,但我对 NOT 毫无头绪。例如,我不完全理解诸如变量!=整数(i!= 3)之类的语句。我的意思是 NOT 运算符 work.for 示例如何在这里不起作用。
class Demo {
public static void main(String args[]) throws java.io.IOException {
char ch;
do {
System.out.print("Press a key followed by ENTER: ");
ch = (char) System.in.read(); // get a char
} while (ch != 'q');
}
}
如果你做了一些系统输出,你会发现:
char ch = 'l';
System.out.print(2 != 3);
--true, they are not equal
System.out.print('q' != 'q');
-- false, they are equals
System.out.print(ch != 'q');
-- true, they are not equals
这意味着,它们 !=
检查它们是否完全相同(注意在这种情况下用于原始类型,例如 int、char、bool 等。此运算符在对象中的工作方式不同,例如字符串)
!运算符是一元的。当应用于布尔值时,它会切换,例如
bool on = true;
System.out.print(!on); //false
System.out.print(on); //true
当在等号旁边使用时,它会检查值是否不相等。如果它们不相等,则 returns 为真。否则,它 returns 为假。例如,
int two = 2;
int three = 3;
System.out.print(two != three);
//returns true, since they are not equal
System.out.print(two == three);
//returns false, since they are not equal
int x = 4;
int y = 5;
!case a
表示不是 case a
x == y
表示 x 和 y 引用内存中的相同位置(要了解其含义,请参阅 this question)。
x != y
与上面相反,写成!(x == y)
(不是x等于y)是一样的
case a && case b
和运算符 - 案例 a 和 案例 b 都为真。
case a || case b
或运算符 - 情况 a 或 情况 b 都为真。
这里有相同的例子,所以一切都更清楚:
1==1 // true, as 1 is equal to 1
2==1 // false, as 1 is not equal to 2.
!true // false, as not true is false.
1!=1 // false, as 1 is equal to one.
!(1==1) // The same as above - exact same output.
// You need the brackets, because otherwise it will throw an error:
!1==1 // Error - what's '!1'? Java is parsed left-to-right.
true && false // false - as not all cases are true (the second one is false)
true || false // rue - as at least one of the cases are true (the first one)