relational operator
Overview
- Note 1: The results of comparison operators are all boolean, that is, either true or false.
- Note 2: The comparison operator "==" cannot be mistakenly written as "=".
code demo
/*
比较运算符:
==,!=,>,>=,<,<=
特点:
无论你的操作是简单还是复杂,结果是boolean类型。
注意事项:
"=="不能写成"="。
*/
class OperatorDemo {
public static void main(String[] args) {
int x = 3;
int y = 4;
int z = 3;
System.out.println(x == y);
System.out.println(x == z);
System.out.println((x+y) == (x+z));
System.out.println("------------");
System.out.println(x != y);
System.out.println(x > y);
System.out.println(x >= y);
System.out.println(x < y);
System.out.println(x <= y);
System.out.println("------------");
int a = 10;
int b = 20;
//boolean flag = (a == b);
//boolean flag = (a = b); //这个是有问题的,不兼容的类型
//System.out.println(flag);
int c = (a = b); //把b赋值给a,然后把a留下来
System.out.println(c);
}
}
Logical Operators
Overview
- Logical operators are used to connect Boolean expressions. In Java, they cannot be written as 3<x<6, they should be written as x>3 & x<6 .
- The difference between "&" and "&&":
- When a single &, the left side is true or false, the right side is operated;
- When double &, if the left side is true, the right side participates in the operation, and if the left side is false, then the right side does not participate in the operation.
- The difference between "|" and "||" is the same. In double OR, the left side is true, and the right side does not participate in the operation.
- The difference between XOR ( ^ ) and OR ( | ) is that when both left and right are true, the result is false.
/*
逻辑运算符:
&,|,^,!
&&,||
特点:
逻辑运算符一般用于连接boolean类型的表达式或者值。
表达式:就是用运算符把常量或者变量连接起来的符合java语法的式子。
算术表达式:a + b
比较表达式:a == b
结论:
&逻辑与:有false则false。
|逻辑或:有true则true。
^逻辑异或:相同为false,不同为true。
举例:情侣关系。男男,男女,女男,女女
!逻辑非:非false则true,非true则false。
特点:偶数个不改变本身。
*/
class OperatorDemo {
public static void main(String[] args) {
int a = 3;
int b = 4;
int c = 5;
//&逻辑与
System.out.println((a > b) & (a > c)); //false & false = false
System.out.println((a > b) & (a < c)); //false & true = false
System.out.println((a < b) & (a > c)); //true & false = false
System.out.println((a < b) & (a < c)); //true & true = true
System.out.println("---------------");
//|逻辑或
System.out.println((a > b) | (a > c)); //false | false = false
System.out.println((a > b) | (a < c)); //false | true = true
System.out.println((a < b) | (a > c)); //true | false = true
System.out.println((a < b) | (a < c)); //true | true = true
System.out.println("---------------");
//^逻辑异或
System.out.println((a > b) ^ (a > c)); //false ^ false = false
System.out.println((a > b) ^ (a < c)); //false ^ true = true
System.out.println((a < b) ^ (a > c)); //true ^ false = true
System.out.println((a < b) ^ (a < c)); //true ^ true = false
System.out.println("---------------");
//!逻辑非
System.out.println(!(a > b)); //!false = true
System.out.println(!(a < b)); //!true = false
System.out.println(!!(a > b)); //!!false = false
System.out.println(!!!(a > b)); //!!false = true
}
}
/*
&&和&的区别? 同理||和|的区别?
A:最终结果一样。
B:&&具有短路效果。左边是false,右边不执行。
开发中常用的逻辑运算符:
&&,||,!
*/
class OperatorDemo2 {
public static void main(String[] args) {
int a = 3;
int b = 4;
int c = 5;
//&&双与
System.out.println((a > b) && (a > c)); //false && false = false
System.out.println((a > b) && (a < c)); //false && true = false
System.out.println((a < b) && (a > c)); //true && false = false
System.out.println((a < b) && (a < c)); //true && true = true
System.out.println("----------------");
int x = 3;
int y = 4;
//boolean b1 = ((x++ == 3) & (y++ == 4));
//boolean b1 = ((x++ == 3) && (y++ == 4));
//boolean b1 = ((++x == 3) & (y++ == 4));
boolean b1 = ((++x == 3) && (y++ == 4));
System.out.println("x:"+x);
System.out.println("y:"+y);
System.out.println(b1);
}
}
呆萌钟, 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明呆萌钟Java基础教程_12_关系运算符与逻辑运算符!