Home » Java programming language

Java Boolean class valueOf() method with example

Boolean class valueOf() method: Here, we are going to learn about the valueOf() method of Boolean class with its syntax and example.
Submitted by Preeti Jain, on October 25, 2019

Syntax:

    public static Boolean valueOf (boolean value);
    public static Boolean valueOf (String value);

Boolean class valueOf() method

  • valueOf() method is available in java.lang package.
  • valueOf(boolean value) method is used to represent Boolean object denoted by the given argument (value) is of boolean type.
  • valueOf(String value) method is used to represent Boolean object holding the boolean value denoted by the given argument (value) is of String type.
  • valueOf(boolean value) and valueOf(String value) methods do not throw an exception at the time of returning a Boolean instance.
  • These are static methods, they are accessible with the class name too and, if we try to access these methods with the class object then also we will not get an error.

Parameter(s):

  • In the first case, boolean value – represents the value of boolean type.
  • In the second case, String value –represents the value of String type.

Return value:

In the first case, the return type of this method is Boolean - it returns the Boolean representation of this boolean argument.

Note:

  • If the given argument value is true, it returns Boolean.TRUE.
  • If the given argument value is false, it returns Boolean.FALSE.

In the second case, the return type of this method is Boolean - it returns the Boolean representation of this String argument.

Note:

  • If the given argument value is either not null or true (true can be sensitive or insensitive), it returns true.

Example:

// Java program to demonstrate the example 
// of valueOf() method of Boolean class

public class ValueOfBooleanClass {
    public static void main(String[] args) {
        boolean b1 = true;
        boolean b2 = false;

        // Display b1,b2 values
        System.out.println("b1:" + b1);
        System.out.println("b2:" + b2);

        // It returns Boolean object holding the value 
        // denoted by the given boolean argument
        Boolean value1 = Boolean.valueOf(b1);
        Boolean value2 = Boolean.valueOf(b2);

        // String object initialization for valueOf(String s)
        String s = "80";

        // It returns Boolean object holding the value 
        // denoted by the given String argument
        Boolean value3 = Boolean.valueOf(s);

        // Display result values
        System.out.println("Boolean.valueOf(b1): " + value1);
        System.out.println("Boolean.valueOf(b2): " + value2);
        System.out.println("Boolean.valueOf(s): " + value3);
    }
}

Output

b1:true
b2:false
Boolean.valueOf(b1): true
Boolean.valueOf(b2): false
Boolean.valueOf(s): false





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.