首页 > Java > java教程 > 正文

Java 中的布尔运算符

WBOY
发布: 2024-08-30 15:20:36
原创
498 人浏览过

随着科技的出现,计算机的发展,也带来了对编程语言的需求。许多编程语言既包括低级语言也包括高级语言。与低级语言相比,高级语言更容易使用,因为它们更容易理解。 Java 就是这样一种高级语言,被广泛用作编程目的的支持语言。有很多概念需要学习和练习才能理解基本概念。在本主题中,我们将讨论 Java 中的布尔运算符。

广告 该类别中的热门课程 JAVA 掌握 - 专业化 | 78 课程系列 | 15 次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

什么是布尔运算符?

布尔运算符只是一组可用于比较表达式的不同运算符。布尔运算符通常有两个值,即 false 或 true。布尔运算符比较左侧和右侧的表达式。相比之下,它只是返回一个布尔值。

Java 中布尔运算符的类型

Java中有多种类型的布尔运算符。以下是 Java 中使用最广泛的各种类型的布尔运算符。

Java 中的布尔运算符

1.逻辑与运算符

这是一个使用 && 运算符来比较逻辑表达式的逻辑赋值。如果多个逻辑中的任何一个失败,它通常给出 false;如果所有表达式都生成 true,它通常给出 true。

AND 运算符示例
  • 如果两个操作数都为 true,则运算结果为 true

代码:

public class Main
{
public static void main(String[] args) {
boolean a = true;
boolean b = true;
System.out.println (a && b); // shows the logical operation using operator
}
}
登录后复制

现在,执行上面的代码

输出:

Java 中的布尔运算符

  • 如果两个操作数都为 false,则运算结果为 false。

代码:

public class Main
{
public static void main(String[] args) {
boolean a = false;
boolean b = false;
System.out.println (a && b); // shows the logical operation using operator
}
}
登录后复制

现在,执行上面的代码

输出:

Java 中的布尔运算符

  • 如果一个操作数为 true,另一个为 false,则运算结果为 false。

代码:

public class Main
{
public static void main(String[] args) {
boolean a = true;
boolean b = false;
System.out.println (a && b); // shows the logical operation using operator
}
}
登录后复制

现在,执行上面的代码

输出:

Java 中的布尔运算符

  • 如果一个操作数为假,另一个为真,则运算结果为假。

代码:

public class Main
{
public static void main(String[] args) {
boolean a = false;
boolean b = true;
System.out.println (a && b); // shows the logical operation using operator
}
}
登录后复制

现在,执行上面的代码

输出:

Java 中的布尔运算符

2.逻辑或运算符

这是一个使用 || 的逻辑赋值比较逻辑表达式的运算符。通常,如果任何一个表达式为真,则返回 true;如果所有表达式失败,则返回 false。

OR 运算符示例
  • 如果两个操作数都为 true,则运算结果为 true。

代码:

public class Main
{
public static void main(String[] args) {
boolean a = true;
boolean b = true;
System.out.println (a || b); // shows the logical operation using operator
}
}
登录后复制

现在,执行上面的代码

输出:

Java 中的布尔运算符

  • 如果两个操作数都为 false,则运算结果为 false。

代码:

public class Main
{
public static void main(String[] args) {
boolean a = false;
boolean b = false;
System.out.println (a || b); // shows the logical operation using operator
}
}
登录后复制

现在,执行上面的代码

输出:

Java 中的布尔运算符

  • 如果一个操作数为 true,另一个为 false,则运算结果为 true。

代码:

public class Main
{
public static void main(String[] args) {
boolean a = true;
boolean b = false;
System.out.println (a || b); // shows the logical operation using operator
}
}
登录后复制

现在,执行上面的代码

输出:

Java 中的布尔运算符

  • 如果一个操作数为假,另一个为真,则运算结果为真。

代码:

public class Main
{
public static void main(String[] args) {
boolean a = false;
boolean b = true;
System.out.println (a || b); // shows the logical operation using operator
}
}
登录后复制

现在,执行上面的代码

输出:

Java 中的布尔运算符

3.等于运算符

该运算符用于检查运算符两边的操作数或表达式是否相等。

等于运算符示例
  • 如果两个操作数不相同,则运算结果为 false。

代码:

public class Main
{
public static void main(String[] args) {
String a = "abc";
String b = "abcd";
System.out.println (a == b); // shows the logical operation using operator
}
}
登录后复制

现在,执行上面的代码

输出:

Java 中的布尔运算符

  • If both operands are the same, the operation result is true.

Code:

public class Main
{
public static void main(String[] args) {
String a = "abc";
String b = "abc";
System.out.println (a == b); // shows the logical operation using operator
}
}
登录后复制

Now, execute the above code

Output:

Java 中的布尔运算符

4. Not Equal to Operator

This operator is used to check if operand or expression on both sides of the operator are equal or not. It produces true if operands on both sides are not the same; else gives false.

Examples of not equal to operator
  • If both operands are not the same, the operation result is true.

Code:

public class Main
{
public static void main(String[] args) {
String a = "abc";
String b = "abcd";
System.out.println (a != b); // shows the logical operation using operator
}
}
登录后复制

Now, execute the above code

Output:

Java 中的布尔运算符

  • If both operands are the same, the operation result is false.

Code:

public class Main
{
public static void main(String[] args) {
String a = "abc";
String b = "abc";
System.out.println (a != b); // shows the logical operation using operator
}
}
登录后复制

Now, execute the above code

Output:

Java 中的布尔运算符

5. Ternary Operator

This operator is used to check if else conditions. It is generally shorthand for the if-else statement. If the expression is true, then if the part is executed otherwise, else block is executed. It uses two operands which are ?:

Example of Ternary Operator
public class Main
{
public static void main (String[]args){
int a = 2;
int b = 5;
int minOfNum = (a < b) ? a : b;
System.out.println (minOfNum);
}
}
登录后复制

Output:

Java 中的布尔运算符

In expression, (a < b) ? a : b it evaluates the value. Based on the evaluation, it executes if or else block

Conclusion

Java is a programming language where there are lots of concepts that one needs to study. Boolean operators are one of those. These boolean operators basically execute the code to check whether the expression value is true or not. Based on the expression evaluation, it returns the value. A boolean operator is widely used in any programming language to various logical programming expressions.

以上是Java 中的布尔运算符的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!