Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
The Boolean
class in Java is a wrapper class for the primitive data type boolean
. It provides utility methods and allows you to use boolean
values in contexts where an object is required, such as in collections and generics.
In this tutorial, we will go through examples of using the Boolean
class.
You can create Boolean
objects using the constructor or the valueOf
method:
Boolean bool1 = new Boolean(true); Boolean bool2 = new Boolean("true"); // The string "true" is case-insensitive Boolean bool3 = Boolean.valueOf(false); Boolean bool4 = Boolean.valueOf("TruE"); // The string "true" is case-insensitive
To compare two Boolean
objects, you can use the equals
method:
Boolean bool1 = new Boolean(true); Boolean bool2 = new Boolean(false); System.out.println(bool1.equals(bool2)); // Output: false
To convert a Boolean
object to a primitive boolean
, you can use the booleanValue
method:
Boolean boolObj = new Boolean(true); boolean boolPrimitive = boolObj.booleanValue();
The Boolean
class provides methods to parse a string and convert it to a boolean
or Boolean
value:
String boolString = "true"; // Parsing to a primitive boolean boolean boolPrimitive = Boolean.parseBoolean(boolString); // The string "true" is case-insensitive // Parsing to a Boolean object Boolean boolObj = Boolean.valueOf(boolString); // The string "true" is case-insensitive
You can convert a boolean
or Boolean
value to a string using the toString
method:
boolean boolPrimitive = true; Boolean boolObj = new Boolean(false); String boolPrimitiveString = Boolean.toString(boolPrimitive); String boolObjString = boolObj.toString();
true
and false
:The Boolean
class provides two static final fields representing the logical values true
and false
:
Boolean trueValue = Boolean.TRUE; Boolean falseValue = Boolean.FALSE;
These are the basic usages of the Boolean
class in Java. Using the Boolean
class, you can perform various operations on boolean values and use them in object-oriented contexts.
Creating Boolean Objects in Java:
The Boolean
class is used to create Boolean objects.
Boolean boolObj = new Boolean(true);
Boolean Class vs. boolean Primitive Type in Java:
Understand the difference between the Boolean
class and the boolean
primitive type.
boolean primitiveBool = true; Boolean objectBool = Boolean.valueOf(true);
Java Boolean Class Methods and Constructors:
Explore methods and constructors provided by the Boolean
class.
Boolean boolObj1 = new Boolean(true); Boolean boolObj2 = Boolean.valueOf("true");
Converting Boolean to boolean in Java:
Convert a Boolean
object to a boolean
primitive type.
Boolean boolObj = Boolean.valueOf(true); boolean primitiveBool = boolObj.booleanValue();
Parsing Boolean Values in Java: Parse a string to obtain a Boolean value.
String boolStr = "true"; Boolean boolObj = Boolean.parseBoolean(boolStr);
Java Boolean Class and Null Values:
Boolean
objects can be null, unlike the primitive boolean
type.
Boolean boolObj = null;
Comparing Boolean Objects in Java:
Compare Boolean
objects for equality.
Boolean boolObj1 = Boolean.valueOf(true); Boolean boolObj2 = Boolean.valueOf(true); boolean areEqual = boolObj1.equals(boolObj2);
Java Boolean Class and Logical Operators:
Use logical operators with Boolean
objects.
Boolean boolObj1 = Boolean.valueOf(true); Boolean boolObj2 = Boolean.valueOf(false); Boolean result = boolObj1 && boolObj2;
Boolean.valueOf() Method in Java:
The valueOf
method returns a Boolean
object representing the specified boolean value.
boolean boolPrimitive = true; Boolean boolObj = Boolean.valueOf(boolPrimitive);
Java Boolean Class and Autoboxing:
Automatic conversion between boolean
and Boolean
is known as autoboxing.
boolean boolPrimitive = true; Boolean boolObj = boolPrimitive;
Using Boolean as a Parameter in Java Methods:
Boolean
can be used as a parameter in Java methods.
public void processBoolean(Boolean boolObj) { // Process Boolean object }
Java Boolean Class and Conditional Statements:
Use Boolean
objects in conditional statements.
Boolean boolObj = Boolean.valueOf(true); if (boolObj) { // Execute when the condition is true }
Boolean Class and Bitwise Operations in Java:
Bitwise operations are not applicable to Boolean
objects.
// This will result in a compilation error Boolean result = boolObj1 & boolObj2;
Java Boolean Class and String Representation:
Obtain a string representation of a Boolean
object.
Boolean boolObj = Boolean.valueOf(true); String strRepresentation = boolObj.toString();
Java Boolean Class and Hash Code:
Get the hash code of a Boolean
object.
Boolean boolObj = Boolean.valueOf(true); int hashCode = boolObj.hashCode();
Common Use Cases for the Boolean Class in Java:
Use the Boolean
class for scenarios requiring a Boolean object.
Boolean boolObj = someCondition ? Boolean.TRUE : Boolean.FALSE;
Common Pitfalls with the Java Boolean Class: Be aware of potential pitfalls, such as unintentional null values.
Boolean boolObj = null; // Potential NullPointerException