Java 简明教程

Java - Boolean class

Java Boolean Class

Java Boolean 类将基本类型 boolean 的值封装在一个对象中。类型为 Boolean 的对象包含一个字段,其类型为 boolean。

The Java Boolean class wraps a value of the primitive type boolean in an object. An object of type Boolean contains a single field whose type is boolean.

Boolean Class Declaration in Java

以下是*java.lang.Boolean* 类的声明:

Following is the declaration for java.lang.Boolean class −

public final class Boolean
   extends Object
      implements Serializable, Comparable<Boolean>

Boolean Class Fields

以下是*java.lang.Boolean* 类的字段:

Following are the fields for java.lang.Boolean class −

  1. static Boolean FALSE − This is the Boolean object corresponding to the primitive value false.

  2. static Boolean TRUE − This is the Boolean object corresponding to the primitive value true.

  3. static Class<Boolean> TYPE − This is the Class object representing the primitive type boolean.

Boolean Class Constructors

Sr.No.

Constructor & Description

1

Boolean(boolean value) This allocates a Boolean object representing the value argument.

2

Boolean(String s) This allocates a Boolean object representing the value true if the string argument is not null and is equal, ignoring case, to the string "true".

Boolean Class Methods

Sr.No.

Method & Description

1

boolean booleanValue()This method returns the value of this Boolean object as a boolean primitive.

2

int compareTo(Boolean b)This method compares this Boolean instance with another.

3

boolean equals(Object obj)This method returns true if and only if the argument is not null and is a Boolean object that represents the same boolean value as this object.

4

static boolean getBoolean(String name)This method returns true if and only if the system property named by the argument exists and is equal to the string "true".

5

int hashCode()This method returns a hash code for this Boolean object.

6

int hashCode(boolean value)This method returns a hash code for a given boolean value. It is compatible with Boolean.hashCode().

7

static boolean logicalAnd​(boolean a, boolean b)This method returns the result of applying the logical AND operator to the specified boolean operands.

8

static boolean logicalOr(boolean a, boolean b)This method returns the result of applying the logical OR operator to the specified boolean operands.

9

*static boolean logicalXor(boolean a, boolean b)*This method returns the result of applying the logical XOR operator to the specified boolean operands.

10

static boolean parseBoolean(String s)This method parses the string argument as a boolean.

11

String toString()This method returns a String object representing this Boolean’s value.

12

static String toString(boolean b)This method returns a String object representing the specified boolean.

13

static Boolean valueOf(boolean b)This method returns a Boolean instance representing the specified boolean value.

14

static Boolean valueOf(String s)This method returns a Boolean with a value represented by the specified string.

Methods Inherited

此类从以下类中继承方法:

This class inherits methods from the following classes −

  1. java.lang.Object

Example of Java Boolean Class

以下示例展示了 Boolean 类提供的一些重要方法的使用。

The following example shows the usage of some important methods provided by Boolean class.

package com.tutorialspoint;
public class BooleanDemo {
   public static void main(String[] args) {

      // create 2 Boolean objects b1, b2
      Boolean b1, b2;

      // assign values to b1, b2
      b1 = Boolean.valueOf(true);
      b2 = Boolean.valueOf(false);

      // create an int res
      int res;

      // compare b1 with b2
      res = b1.compareTo(b2);

      String str1 = "Both values are equal ";
      String str2 = "Object value is true";
      String str3 = "Argument value is true";
      if( res == 0 ) {
      	System.out.println( str1 );
      } else if( res > 0 ) {
         System.out.println( str2 );
      } else if( res < 0 ) {
         System.out.println( str3 );
      }
   }
}

Output

让我们编译并运行上述程序,这将生成以下结果 −

Let us compile and run the above program, this will produce the following result −

Object value is true