Java Boolean valueOf() Method

The Boolean.valueOf() method in Java is used to convert a String or a boolean primitive to a Boolean object.

Table of Contents

  1. Introduction
  2. valueOf() Method Syntax
  3. Overloaded valueOf() Methods
    • valueOf(boolean b)
    • valueOf(String s)
  4. Examples
    • Converting a boolean Primitive
    • Converting a String
  5. Real-World Use Case
  6. Conclusion

Introduction

The Boolean.valueOf() method is a static method in the Boolean class in Java. It converts a boolean primitive or a String to a Boolean object. This method is useful for converting values to Boolean objects, which can be used in collections or other places where Boolean objects are required.

valueOf()() Method Syntax

The syntax for the valueOf() method is as follows:

valueOf(boolean b)

public static Boolean valueOf(boolean b)
  • b: The boolean value to be converted to a Boolean object.

valueOf(String s)

public static Boolean valueOf(String s)
  • s: The string to be converted to a Boolean object. If the String is equal to "true" (ignoring case), the method returns Boolean.TRUE. For any other input, including null, it returns Boolean.FALSE.

Overloaded valueOf()() Methods

valueOf(boolean b)

This method converts a boolean primitive to a Boolean object.

Example

public class ValueOfBooleanExample {
    public static void main(String[] args) {
        boolean boolPrimitive = true;

        Boolean boolObject = Boolean.valueOf(boolPrimitive);

        System.out.println("Boolean object: " + boolObject);
    }
}

Output:

Boolean object: true

valueOf(String s)

This method converts a String to a Boolean object. If the String is equal to "true" (ignoring case), it returns Boolean.TRUE. For any other input, it returns Boolean.FALSE.

Example

public class ValueOfStringExample {
    public static void main(String[] args) {
        String trueString = "true";
        String falseString = "false";
        String invalidString = "yes";

        Boolean trueObject = Boolean.valueOf(trueString);
        Boolean falseObject = Boolean.valueOf(falseString);
        Boolean invalidObject = Boolean.valueOf(invalidString);

        System.out.println("Boolean object (trueString): " + trueObject);
        System.out.println("Boolean object (falseString): " + falseObject);
        System.out.println("Boolean object (invalidString): " + invalidObject);
    }
}

Output:

Boolean object (trueString): true
Boolean object (falseString): false
Boolean object (invalidString): false

Examples

Converting a boolean Primitive

The valueOf(boolean b) method can be used to convert a boolean primitive to a Boolean object.

Example

public class PrimitiveToBooleanExample {
    public static void main(String[] args) {
        boolean isAvailable = true;

        Boolean isAvailableObject = Boolean.valueOf(isAvailable);

        System.out.println("Boolean object: " + isAvailableObject);
    }
}

Output:

Boolean object: true

Converting a String

The valueOf(String s) method can be used to convert a String to a Boolean object.

Example

public class StringToBooleanExample {
    public static void main(String[] args) {
        String str = "true";

        Boolean boolObject = Boolean.valueOf(str);

        System.out.println("Boolean object: " + boolObject);
    }
}

Output:

Boolean object: true

Handling Null Values

When passing null to valueOf(String s), the method returns Boolean.FALSE.

Example

public class NullStringExample {
    public static void main(String[] args) {
        String nullString = null;

        Boolean boolObject = Boolean.valueOf(nullString);

        System.out.println("Boolean object: " + boolObject);
    }
}

Output:

Boolean object: false

Real-World Use Case

Storing Boolean Values in Collections

In a real-world scenario, you might need to store boolean values in a collection that requires Boolean objects.

Example

import java.util.ArrayList;
import java.util.List;

public class BooleanCollectionExample {
    public static void main(String[] args) {
        List<Boolean> booleanList = new ArrayList<>();

        boolean isCompleted = true;
        String isAdmin = "false";

        booleanList.add(Boolean.valueOf(isCompleted));
        booleanList.add(Boolean.valueOf(isAdmin));

        System.out.println("Boolean List: " + booleanList);
    }
}

Output:

Boolean List: [true, false]

Parsing Configuration Settings

When reading configuration settings from a file or environment variables, you can use valueOf() to convert string representations to Boolean objects.

Example

public class ConfigExample {
    public static void main(String[] args) {
        String debugMode = System.getenv("DEBUG_MODE");

        Boolean isDebugMode = Boolean.valueOf(debugMode);

        if (isDebugMode) {
            System.out.println("Debug mode is enabled.");
        } else {
            System.out.println("Debug mode is disabled.");
        }
    }
}

Output:

Debug mode is disabled.

(Assuming the DEBUG_MODE environment variable is not set or is set to a value other than "true")

Conclusion

The Boolean.valueOf() method in Java provides a convenient way to convert boolean primitives and String values to Boolean objects. By understanding how to use this method and its overloaded variants, you can efficiently handle boolean conversions in your Java applications. Whether you are storing boolean values in collections, parsing configuration settings, or handling user input, the valueOf() method provides a reliable solution for these tasks.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top