首页  >  文章  >  Java  >  Java 布尔值

Java 布尔值

王林
王林原创
2024-08-30 15:11:58217浏览

在日常生活中,我们经常对我们的每项活动、情况、愿景、结果、事件等做出决定。我们决定的价值有两个:是或否;真或假;开或关;进行或不进行等。编程不属于任何例外。在编程中,基于我们的核心逻辑和用例,我们需要做出决策,并基于这些决策;我们需要编写相应的代码。作为一种编程语言,Java 也不例外,它允许我们提供一种称为“布尔”的特殊数据类型,以便在代码中使用它们来进行决策。 Java 布尔变量或布尔表达式可以采用两个值之一:true 或 false。

让我们从java编程的角度讨论布尔值。

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

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

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

Java 布尔值的类型

以下是不同类型的 Java 布尔值:

Java 布尔值

1. 带有变量名称的关键字布尔值

关于 java 中布尔类型变量的值,您只有两个选择。布尔类型的值为 true 或 false。没有其他可用的选择。 不要急于寻求Java作业或任务的帮助;继续阅读以了解 Java 布尔值。 您需要将关键字 Boolean 与变量名称一起使用,并为其分配值(true 或 false)。

语法:

Boolean <variable_name> = <value>, where value is either true or false

例如:

boolean bool = true,其中 bool 是变量名称,与 true 关联的值

boolean bool2 = false,其中 bool 是变量名称,与值关联为 false

代码示例 1:

public class BooleanInJava {
public static void main(String[] args){
boolean bool = true ;
boolean bool2 = false;
System.out.println(bool);
System.out.println(bool2);
}
}

输出:

Java 布尔值

2.布尔类型变量

如果向布尔类型变量提供 true 或 false 以外的值会怎样?

例如:

布尔 bool = 1 ;

布尔 bool2 = 0;

您将收到一个错误。

代码示例 2:

public class BooleanInJava {
public static void main(String[] args) {
boolean bool = 1 ;
boolean bool2 = 0;
System.out.println(bool);
System.out.println(bool2);
}
}

输出:

Java 布尔值

3.布尔值的特征

现在,如何有效地利用布尔的这个特性?

我们可以用它在我们的程序中做出决策。我的意思是说,您可以通过使用条件运算符来获取或打印布尔值来测试程序中的一些决定因素。这是布尔表达式条件的测试。程序将评估这个表达式,并做出相应的决定。

让我们举一些例子:

代码示例 3:

public class BooleanInJava {
public static void main(String[] args) {
int num1 = 10;
int num2 =11;
System.out.println(num1 > num2); // returns false, because 11 is higher than 10
System.out.println(num2 > num1); // returns true, because 11 is higher than 10
System.out.println(num1 < num2); // returns true, because 10 is lesser than 11
System.out.println(num2 <num1); // returns false, because 10 is lesser than 11
}
}

输出:

Java 布尔值

布尔值如何工作?

在本文中,我们将指出布尔运算的工作原理,这意味着我们如何在程序或用例中使用布尔运算的功能。由于布尔值帮助我们做出决策,因此我们可以将此决策逻辑放入条件表达式中,例如:在 while 循环评估或 if-else 决策中。首先,让我们看一下布尔运算符,它将用于从布尔表达式生成布尔值,并最终使用该值进行决策。我们将在这里使用布尔逻辑运算符,它们是: | , & , ^ , ! , || , && , == , != 。让我们使用两个布尔变量 num1 和 num2。

Symbol of Boolean operators Name of the corresponding Symbol
| OR
& AND
^ XOR
! NOT
!= NOT EQUAL
&& Short circuit AND
|| Short circuit OR
== EQUAL
布尔运算符的符号 对应交易品种名称 | 或者 & 和 ^ 异或 ! 不 != 不等于 && 短路与 || 短路或 == 等于 表>

Please check the table for your understanding of how evaluation is happening in Boolean expressions. This understanding is very important to clear your concepts:

Variables/Boolean Expressions num1 num2 num1|num2 num1&num2 num1^num2 !num1 !num2
 

Values/Result of evaluations

true true true true false false false
true false true false true false true
false true true false true true false
false false false false false true

true

4. Public Class

Code Example 4: 

public class BooleanInJava {
public static void main(String[] args) {
boolean num1 = true;
boolean num2 = false;
System.out.println("num1|num2 = "+(num1|num2));
System.out.println("num1&num2 = "+(num1&num2));
System.out.println("num1^num2 = "+(num1^num2));
System.out.println("!num1 = "+(!num1));
System.out.println("!num2 = "+(!num2));
}
}

Output:

Java 布尔值

Let us see some more code examples.

5. Boolean Operators

Code Example 5:

Here we will compare two Boolean variables and assign values to them and then create Boolean expression for those using Boolean operators and then print them to see the final output.

public class BooleanInJava {
public static void main(String[] args) {
boolean num1 = true;
boolean num2 = false;
boolean num3=(num1==num2); // Boolean expression evaluating whether values of num1 and num2 are equal or not
System.out.println(num1);
System.out.println(num2);
System.out.println(num3); //will return false as num1 and num2 have different values
}
}

Output:

Java 布尔值

6. Boolean Objects.

Code Example 6:

Here we will compare two Boolean objects.

public class BooleanInJava {
public static void main(String[] args) {
boolean boolObj1=new Boolean("TRUE");
boolean boolObj2=new Boolean("FALSE");
boolean boolObj3=new Boolean("FALSE");
boolean decision=(boolObj1==boolObj2);  // evaluating values of boolObj1 and boolObj2
System.out.println("Are the value of boolean objects 1 and 2 equal? "+decision);
boolean decision2=(boolObj3==boolObj2);  // evaluating values of boolObj2 and boolObj3
System.out.println("Are the value of boolean objects 2 and 3 equal? "+decision2);
}
}

Output:

Java 布尔值

Conclusion – Java Boolean

All of the comparisons and conditions in Java are primarily based on Boolean expressions; hence you need to use them in an effective manner. In this topic, you have learned about many aspects of Boolean values but, you need to use them effectively based on your business/ client requirements and use cases.

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

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn