Home > Java > javaTutorial > body text

What is the importance of Boolean class in Java?

WBOY
Release: 2023-09-10 20:33:12
forward
805 people have browsed it

What is the importance of Boolean class in Java?

The java.lang.Boolean class is a final class and it is a subclass of Object class. The Boolean class wraps the value of primitive datatype boolean into a Boolean object. An object of type Boolean contains a single field whose type is boolean. In other words, the wrapper classes create objects for primitive data types. This class provides many methods for converting a boolean to a String and a String to a boolean as well as other constants and methods useful when dealing with a boolean.

Syntax

<strong>public final class Boolean extends Object implements Serializable, Comparable</strong>
Copy after login

Example1

public class BooleanTest {
   public static void main(String[] args) {
      Boolean b;
      b = new Boolean(true);
      boolean b1;
      b1 = b.booleanValue();
      String data = "The Primitive value of Boolean object " + b + " is " + b1;
      System.out.println(data);
   }
}
Copy after login

Output

The Primitive value of Boolean object true is true
Copy after login

Example2

的中文翻译为:

示例2

public class BooleanTest1 {
   public static void main(String args[]) {
      Boolean b1, b2;
      b1 = new Boolean(true);
      b2 = new Boolean("false");
      System.out.println("b1 Value is : "+ b1.booleanValue());
      System.out.println("b2 Value is : "+ b2.booleanValue());
      System.out.println("b1 compareTo b2 : "+ b1.compareTo(b2));
      System.out.println("b1 equals b2 : "+ b1.equals(b2));
   }
}
Copy after login

Output

b1 Value is : true
b2 Value is : false
b1 compareTo b2 : 1
b1 equals b2 : false
Copy after login

The above is the detailed content of What is the importance of Boolean class in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!