Java逻辑运算符如何运用

作者:暴躁小n | 创建时间: 2023-08-12
在Java程序中,需要进行大量的计算,所以要使用到运算符号,下面先来给大家说明Java逻辑运算符如何运用...
Java逻辑运算符如何运用

操作方法

赋值运算符是为变量指定具体值的符号,顺序是从左到右。

下面说明短路与“&&”的运用 首先我们输入以下代码: public class Test { public static void main(String[] args) { boolean a = true; boolean b = false; System.out.println("a && b:"+(a&&b)); } } 会看到输出结果:a && b:false

下面说明短路或“||”的运用 首先我们输入以下代码: public class Test { public static void main(String[] args) { boolean a = true; boolean b = false; System.out.println("a || b:"+(a||b)); } } 会看到输出结果:a || b:true

下面说明非“!”的运用 首先我们输入以下代码: public class Test { public static void main(String[] args) { boolean a = true; System.out.println("!a :"+(!a)); } } 会看到输出结果:!a :false

下面说明或“&”的运用 首先我们输入以下代码: public class Test { public static void main(String[] args) { boolean a = true; boolean b = false; System.out.println("a & b :"+(a&b)); } } 会看到输出结果:a & b :false

下面说明或“|”的运用 首先我们输入以下代码: public class Test { public static void main(String[] args) { boolean a = true; boolean b = false; System.out.println("a | b :"+(a|b)); } } 会看到输出结果:a | b :true

下面说明异或“^”的运用 首先我们输入以下代码: public class Test { public static void main(String[] args) { boolean a = true; boolean b = false; System.out.println("a ^ b :"+(a^b)); } } 会看到输出结果:a ^ b :true

温馨提示

本教程只介绍了常用逻辑运算符的运用
&&与&在功能上本身没有区别
点击展开全文

更多推荐