×

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

What is new in Java 17?

Java 17 LTS is the most recent long-term support release for the Java SE platform. Under the Oracle No-Fee Terms and Conditions License, which stands for long-term support, JDK 17...

6 minutes read.

Java Strictfp Keyword

Strictfp is used to impose limits on floating-point calculation. It ensures that we will get the same result on every platform while performing an operation with the floating-point variable. The floating-point calculation is platform-dependent due...

1 minute 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.

Types of Events in Java

One of Java's key ideas is the principle of an event. Events in Java are activities that result in a change in the state or behavior of an object. The...

4 minutes read.

Java 8 Features

Java 8 Features: After the great success of Java 7, many developers were waiting for the next version of one of the best languages in the tech world. Moreover, on...

6 minutes read.

Arithmetic exception in Java

Exception Handling is one of the most potent ways of handling runtime faults and preserving the application's normal flow. In Java, an exception is an out-of-the-ordinary state, and exceptions are...

3 minutes read.

Iccanobif Numbers in Java

In this article, you will be very well equipped with the knowledge of iccanobif numbers in Java. You will also know how they are formed and basic example programs on...

3 minutes read.

Hamming Code in Java

In a computer network, hamming code is a unique set of error-correction codes. It is mostly utilised in computer graphics for mistake detection and correction during data transmission from sender...

8 minutes read.

Pig Latin Program in Java

Pig Latin Program in Java Pig Latin is a method for translating words of the English language into a different language. It is an encrypted word that is generated by using the following steps....

4 minutes read.

Program to Reverse a Number in Java

In order to reverse a number, the digit in the first place must be swapped with the digit in the final position, the second digit with the second-to-last digit, and...

3 minutes read.

Java Imageio

The javax.imageio package contains the final class known as Java ImageIO. For easy image reading, writing, and simple encoding and decoding, the class offers a convenient way. The class offers...

4 minutes read.

How to avoid deadlock in java

Deadlock: A deadlock is an event that never going to occur. In java, deadlock is just a part of the multithreading. It is an environment that allows us to run multiple...

4 minutes read.

Generics vs Wildcard in Java

In generic programming, the question mark (?) is often referred to as the wildcard. It stands for a mysterious type. The wildcard can be used for many different contexts, such as...

4 minutes read.

Java Array Generic

Creating Generic Array in Java A collection of comparable sorts of data is kept in an array. In Java, making a generic array is challenging. The type information of an array's...

4 minutes read.

How to Create Singleton Class in Java

In this tutorial, we will discuss the singleton class and how to create it. Introduction Java is a purely object-oriented programming language. It consists of classes and objects. But Singleton is one...

4 minutes read.

Singleton class in Java

What is Singleton class in Java? Singleton means it is one. That means we can create only one instance or object of a class. For example, let us have a class...

3 minutes read.

Java Math toDegrees() Method

The toDegrees() method of Java Math class converts a radian angle to an approximately equivalent angle measured in degrees. Syntax: public static double toDegrees(double angrad) Parameters: The parameter ‘angrad ‘represents an angle measured in...

2 minutes read.

Missing Number in an Arithmetic Progression in Java

Given an array that shows the elements of an orderly arithmetic progression. Find the missing number to complete the succession of elements. Example:  Input: a [ ] = {2 , 4 , 6...

3 minutes read.

Java Math sinh() Method

The sinh() method of Java Math class returns the hyperbolic sine of the specified double value. Syntax: public static double sinh(double x) Parameters: The parameter ‘a’ represents the number whose hyperbolic sine is to...

2 minutes read.

Copy data/content from one file to another in java

In this article, you will be acknowledged about how to copy data or content from one file to another file. Also, you will be acknowledged about the classes and methods...

3 minutes read.