Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the Boolean.toString method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The toString method of the Boolean class can be used to get the Boolean value of a string.

Syntax

public String toString();
public String toString(boolean b);

Return value

If the argument is passed, then the string representation of the passed Boolean value is returned.

For `true` -- "true" is returned
For `false` -- "false" is returned

If the argument is not passed, then the string representation of the current Boolean object value is returned.

Code

class BooleanToString {
public static void main( String args[] ) {
Boolean trueVal = new Boolean(true);
System.out.println( "trueVal.toString() : " + trueVal.toString());
System.out.println( "trueVal.toString(true) : " + trueVal.toString(true));
System.out.println( "trueVal.toString(false) : " + trueVal.toString(false));
}
}

Explanation

In the code above:

  • We created a Boolean object with true as the value.

  • We called the toString() method without argument, and we will get the string representation of the object’s Boolean value. In our case, "true" will be returned.

  • We called the toString(true) method. For this, we will get the string representation of true; that is, "true" is returned. Then, we called toString(false). For this we will get "false" as a result.

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring