×

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

Java Output Formatting

Sometimes we want a program's output to be displayed in a specific format. The printf () function in the C programming language can be used to achieve this. We will...

4 minutes read.

Lazy loading in Java

Lazy loading is the idea of waiting to load an object until you need it. In other words, it is the practice of postponing class instantiation until it is necessary....

5 minutes read.

Fibonacci Series Program in Java

Fibonacci Series Program in Java using Recursion Fibonacci series is a series whose every term is comprised of adding its previous two terms, barring the first two terms 0 and 1....

3 minutes read.

Properties Class in Java

Properties class is associated with Java since JDK 1.0, i.e. it is a legacy class. It is the subclass of Hashtable. It is used to maintain the lists of values in which...

5 minutes read.

Hogben Numbers in Java

In this section, we will discover what the Hogben number is and develop Java programs that compute it. Java coding interviews and academic exams typically involve questions about the Hogben...

3 minutes read.

Array Programs in Java

Array Programs in Java: An array is a data structure that stores similar elements in a contiguous memory location. In Java, an array is an object that stores the same...

7 minutes read.

What is string in Java why it's immutable

String in Java Strings are a series of characters commonly used in Java language, which are considered objects in the Java. To create and manipulate strings, java will provide the String...

4 minutes read.

Java Continue Keyword

The java keyword ‘continues’ has also been called as java continue statement.The main concepts of the java continue statement or keyword is used in the controlling of the loop structure.Java...

3 minutes read.

List all files in a Directory in Java

The list of all files in the directory can be done using Java. You should be aware that a directory may include a subfolder and that subdirectory may also contain some...

3 minutes read.

Sort Dates in Java

The Collection class's sort() method, which is a component of the Comparator mechanism, arranges the data in decreasing order. The Comparator interface can be used to accomplish the goal while...

4 minutes read.

Char and String differences in Java

Characters in Java Character (char) belongs to the characters group, which represents symbols in a character set, such as alphabets and numerals. A Java char has 16 bits in length and has a range...

5 minutes read.

Rectangular Numbers in Java

In this tutorial, we will understand the meaning of a rectangular number in Java with the aid of examples, illustrations, and implementations. It is one of the popular coding interview...

3 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.

Java extend multiple classes

In Java, what does extend mean? One of several Java inheritance keywords is extended, meaning we pass all or most of the Parent class's characteristics through to the Child class. The...

3 minutes read.

Shift right zero Fill Operator in Java and Operator Shifting

Left shift operator ( << ) : The left shift operator is an operator which performs its action at the bits level of a binary Operator. That means when you perform...

4 minutes read.

Java Math ceil() Method

The ceil() method of Math class returns the smallest double value which is closest to negative infinity and is greater than or equal to the argument. Syntax: public static double ceil(double a) Parameters: The...

2 minutes read.

Convert List to String in Java

We occasionally need to create a string from a list of characters. Since a string is only a collection of characters, a character array can be converted into a string....

6 minutes read.

Java String length() method

Java String length() method returns length of the characters in a String. Syntax: public int length() Returns It returns length of the characters Java String length() method example 1          public class JavaStringLengthEx1  ...

1 minute read.

Anagram Program in Java using String

Anagram Program in Java Anagrams are those words that are generated by rearrangement of the letters of another phrase or word. Usually, while doing the rearrangement, the original letters are used...

8 minutes read.

Default Virtual Behaviour in C++ vs Java

Virtual Behaviour in C++: The class member methods in C++ are, by default, non-virtual. This implies that by simply defining it, they can be turned into virtual. The virtual class can be...

3 minutes read.