Boolean is a type that represents true and false values in Java, and the value is true or false. Its usage includes: conditional judgment, logical operation and Boolean return value of method. The declaration usage method is boolean variable name = true/false. Use == or != for comparison. Logical operators include AND, OR, and NOT. Example: Check if a number is even and display a message based on the result.
Usage of boolean type in Java
Boolean is a data type in Java that represents true and false values. It has only two possible values: true or false.
Usage
The boolean type is mainly used in the following situations:
Declaration
To declare a boolean variable, you can use the boolean keyword, followed by the variable name:
boolean isTrue = true;
Assignment
Boolean variables can be assigned values through the assignment operator (=):
isTrue = false;
Comparison
Boolean values can be assigned through the equality operator (== ) or the inequality operator (!=) for comparison:
if (isTrue == true) { // 执行代码块 }
Logical operations
Java provides the following logical operators:
Application Example
The following is a practical application example of the boolean type:
// 检查一个数字是否为偶数 boolean isEven = (number % 2 == 0); // 根据布尔值显示消息 if (isEven) { System.out.println("该数字是偶数。"); } else { System.out.println("该数字是奇数。"); }
The above is the detailed content of boolean usage in java. For more information, please follow other related articles on the PHP Chinese website!