×

Java Boolean toString() Method

The toString() method of Java Boolean class returns a String corresponding to this Boolean object. It returns a string value “true”, if the defined object is true else it returns false.

Syntax

  1. public String toString()
  2. public static String toString(boolean b)

Parameters

The parameter ‘b’ represents the Boolean argument to be converted in String.

Return Value

The toString() method returns the String depicting this Boolean value.

Example 1

public class JavaBooleanToStringMethodExample1 {
    //returns the string representation of the specified boolean
    public static void main(String[] args) {
        Boolean b1=true;
        String str= b1.toString();
        System.out.println(str);
  // returns the string representation of the specified boolean
        Boolean b2=false;
        String str1= b2.toString();
        System.out.println(str1);
    }
}

Output

true
false

Example 2

public class JavaBooleanToStringMethodExample2 {
      public static void main(String[] args) {
      Boolean b1 =null;
     //for null parameter it will return an exception
      String str= b1.toString();
      System.out.println(str);
      }
  }

Output

Exception in thread "main" java.lang.NullPointerException
            at com.interf.JavaBooleanToStringMethodExample2.main(BooleanToStringMethod
Example2.java:8)

Example 3

import java.util.Scanner;
public class JavaBooleanToStringMethodExample3 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
            System.out.println("True/False:-\n Sun rises from west. ");
            System.out.print("Ans: ");
            Boolean b1 = scanner.nextBoolean();
            //calling static tpString() method
            if (Boolean.toString(b1) == "false") {
                System.out.println("Right Answer ");
            } else {
                System.out.println("Wrong Answer ");
            }
    }
}

Output

True/False:-
5 months have 30 days.
Ans: false
Right Answer

Example 4

public class JavaBooleanToStringMethodExample4 {
    public static void main(String[] args) {
        boolean b1 =false;
        System.out.println(b1);
        String str =b1.toString();
        System.out.println(str);
    }
}

Output

Error:(7, 23) java: boolean cannot be dereferenced

Related Topics

Count of Range Sum Problem in Java

In this article, we will discuss the basic approach or native approach used to count range sum problem in java. To solve this problem, we will check for numbers if they...

6 minutes read.

Jagged Array in Java

Prerequisite We must first understand what Arrays are and Multi-dimensional Arrays are before we can learn about Jagged arrays. Java Arrays: A collection of data types that are similar is called an...

5 minutes read.

Java Strings

String class is the most used class in Java programming language. The string is the sequence of characters, which is treated as objects in Java. Creating String objects We create String objects...

5 minutes read.

Constructor Program in Java

Constructor Program in Java In Java, a constructor is a piece of code that is used to create an object. A constructor is called implicitly when an object is created in...

7 minutes read.

Java Extends vs Implements

Java: We known that the java is a pure object oriented programming language. Java programming language consists of many features such as portable, plat form independence, secured, robust, simple, architecture neutral,...

4 minutes read.

Kotlin Vs Java

Kotlin Vs Java There are many languages available for Android development. Java is the official language for android development but Kotlin is becoming popular nowadays. This article discusses both of these...

4 minutes read.

Isomorphic String in Java

In this tutorial, we will understand what is meant by isomorphic String in java. We will also see a Java program to find out if the string is isomorphic or...

4 minutes read.

Java String Methods

Java String Methods Java String class is the most important class of the java.lang package. It is used to handle the String related operations. It contains a lot of built-in Java...

2 minutes read.

How to take Array Input in Java

In this tutorial, we will learn about how to take array input in Java. So, before taking inputs let us know what an array is first. Array: An array is a collection...

10 minutes read.

Java program to count the occurrences of each character

The count of each character is the frequency of the characters. For example, the given String is “Programming”. In the given string, the count of the characters ‘P’ -1, ’r’-2,...

6 minutes read.

How to Convert String to Integer in Java

How to convert String to int in Java You need to convert String into int if you want to perform a mathematical operation on string which contains digits. To do so,...

3 minutes read.

Java 9 Interface Private Method

Java 9 provides us the facility to include private methods inside the java interface. In java 8 and earlier versions, we are supposed to use only constant variables and abstract...

3 minutes read.

Sorting a String in Java

Sorting a String in Java Sorting is a process of arranging available data objects in a meaningful format that is ascending or descending order. Sorting a string or array of String...

4 minutes read.

Java Math getExponent() Method

The getExponent() method of Math class returns the unbiased exponent of the argument. Syntax: public static int getExponent (double d) Parameters: The parameter ‘d’ represents the double value. Return Value: The getExponent () method returns the...

1 minute read.

Zig Zag star and Number Pattern in Java

We covered many Java pattern applications in the preceding part. We will write Java applications for zigzag star and number patterns in this part. Printing Zig Zag Number Pattern Steps Print one...

3 minutes read.

Java Wrapper Class

Wrapper class in Java encapsulates a primitive type within an object. In other words, it is a unique mechanism of Java to convert primitive type in to object and object into primitive type....

3 minutes read.

Java Math.multiplyExact() method

In java, we are provided with multiplyExact method in the Math module. This Math module belongs to the java.lang package. In general, this method returns the product of the provided...

2 minutes read.

Powerful Number in Java

We will define a powerful number in this article and write Java programs to determine whether a given number is a powerful number or not. Java coding interviews and academic...

6 minutes read.

Longest Arithmetic Progression Sequence in Java

The task is to find the length of the largest sequence in an array to form an arithmetic progression. The array arr[] is given. Longest Arithmetic Progression Sequence in Java Algorithm set...

5 minutes read.

Timestamp Operation in Java

JDBC escape syntax is supported by Timestamp's formatting and parsing functions. Additionally, it adds support for fractional seconds values for SQL TIMESTAMP.java.util.Date is wrapped in a lightweight wrapper that enables...

3 minutes read.