java.lang.String valueOf(boolean b)

Description :

This java tutorial shows how to use the valueOf(boolean b) method of String class of java.lang package. This method returns a string representation of boolean data type.

Method Syntax :

public static String valueOf(boolean b)

Parameter Input :

DataType Parameter Description
boolean b method parameter which we are interested to get the string equivalent

Method Returns :

The String valueOf() method with argument boolean returns a new String object. If the value of the boolean parameter is true the result would be “true” otherwise “false”.

Compatibility Version :

Since the beginning

Exception :

None

Discussion :

The string valueOf(boolean b) method is to get a String equivalent of boolean method parameter. This method be statically accessed which means we should call the method like String.valueOf(boolean value).

Java Code Example :

This example source code demonstrates the use of valueOf(Boolean b) of String class. We have used the contains() method of String class to evaluate if String contains a string “Male”. This is simply to print the value of a boolean datatype. Since the sample string contains a String “Male”, the console output is true.

package com.javatutorialhq.java.tutorial.string;

/*
 * Java example source code to get the value of boolean data type
 */

public class StringValueOfBoolean {

	public static void main(String[] args) {

		// declare a sample string value
		String strValue = "Will Smith-Male-USA";
		// check if String value contains a specific string
		boolean bool = strValue.contains("Male");
		// print the string equivalent of our boolean check
		System.out.println(String.valueOf(bool));
	}

}

Sample Output :

Running the valueOf() method example source code of String class will give you the following output

java string indexOf-boolean method example

java string indexOf boolean method example

Exception Scenario :

N/A

Suggested Reading List :

References :