随着科技的出现,计算机的发展,也带来了对编程语言的需求。许多编程语言既包括低级语言也包括高级语言。与低级语言相比,高级语言更容易使用,因为它们更容易理解。 Java 就是这样一种高级语言,被广泛用作编程目的的支持语言。有很多概念需要学习和练习才能理解基本概念。在本主题中,我们将讨论 Java 中的布尔运算符。
广告 该类别中的热门课程 JAVA 掌握 - 专业化 | 78 课程系列 | 15 次模拟测试开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
布尔运算符只是一组可用于比较表达式的不同运算符。布尔运算符通常有两个值,即 false 或 true。布尔运算符比较左侧和右侧的表达式。相比之下,它只是返回一个布尔值。
Java中有多种类型的布尔运算符。以下是 Java 中使用最广泛的各种类型的布尔运算符。
这是一个使用 && 运算符来比较逻辑表达式的逻辑赋值。如果多个逻辑中的任何一个失败,它通常给出 false;如果所有表达式都生成 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 } }
现在,执行上面的代码
输出:
代码:
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 } }
现在,执行上面的代码
输出:
代码:
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 } }
现在,执行上面的代码
输出:
代码:
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 } }
现在,执行上面的代码
输出:
这是一个使用 || 的逻辑赋值比较逻辑表达式的运算符。通常,如果任何一个表达式为真,则返回 true;如果所有表达式失败,则返回 false。
代码:
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 } }
现在,执行上面的代码
输出:
代码:
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 } }
现在,执行上面的代码
输出:
代码:
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 } }
现在,执行上面的代码
输出:
代码:
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 } }
现在,执行上面的代码
输出:
该运算符用于检查运算符两边的操作数或表达式是否相等。
代码:
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 } }
现在,执行上面的代码
输出:
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:
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.
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:
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:
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 ?:
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:
In expression, (a < b) ? a : b it evaluates the value. Based on the evaluation, it executes if or else block
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中文网其他相关文章!