×

Java Boolean toValue() Method

The valueOf() method of Java Boolean class returns a Boolean object representing the given Boolean or String value. It returns true, if the specified Boolean or string object is true else it returns false.

Syntax

  1. public static Boolean valueOf(String s)
  2. public static Boolean valueOf(boolean b)

Parameters

The parameters ‘b’ and‘s’ represents a Boolean and a String value.

Return Value

The valueOf() method returns a Boolean instance representing b or s.

Example 1

public class JavaBooleanValueOfMethodExample1 {
    public static void main(String[] args) {
        Boolean b1 = true;
        // will return a boolean instance corresponding to Boolean b1
        Boolean b2 = Boolean.valueOf(b1);
        System.out.println("value returned = "+b2);
        Boolean b3 = Boolean.valueOf(false);
        System.out.println("value returned = "+b3);
    }
}

Output

value returned = true
value returned = false

Example 2

import java.util.Scanner;
public class JavaBooleanValueOfMethodExample2 {
    public static void main(String[] args) {
        Boolean b1 = true;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter two numbers: ");
        System.out.print("a = ");int a=scanner.nextInt();
        System.out.print("b = ");int b=scanner.nextInt();
        if(a<=0 || b<=0 || a==b){
            b1=false;
        }
        Boolean b2=Boolean.valueOf(b1);
        if(b2){
            a=b-a;
            b=b-a;
            a=a+b;
            System.out.println(" After swapping value : ");
            System.out.println("a : "+a);
            System.out.println("b : "+b);
        }
        else{
            System.out.println("Number not in the range. Please try with different numbers.");
        }
    }
}

Output

Enter two numbers:
a = 0
b = -19
Number not in the range. Please try with different numbers.

Example 3

public class JavaBooleanValueOfMethodExample3{
    public static void main(String[] args) {
    //return a boolean value representing the string
    String str1="true";
    Boolean b1 =Boolean.valueOf(str1);
    System.out.println("Boolean value: "+b1);
   // for any value of str other than true it will return false
    String str2="abc";
    Boolean b2 =Boolean.valueOf(str2);
    System.out.println("Boolean value: "+b2);
    }
}

Output

Boolean value: true
Boolean value: false

Example 4

public class JavaBooleanValueOfMethodExample4{
    public static void main(String[] args) {
        //for null value it will return an exception
       Boolean b1=null;
       Boolean b2= Boolean.valueOf(b1);
        System.out.println(b2);
    }
}

Output

Exception in thread "main" java.lang.NullPointerException
            at com.interf.JavaBooleanValueOfMethodExample4.main(JavaBooleanValueOfMethod
Example4.java:8)

Related Topics

Applet Program in Java

Applet Program in Java An applet is a program that can be embedded in a web page. Applets programs are run by a web browser. It mainly works on the client-side....

3 minutes read.

How to add 4 Hours to the Current Date in Java?

In this tutorial, we will learn how to add 4 hours to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

Types of Logical Operators in Java

In this tutorial, we are going to study logical operators. A logical operator is an operator that accomplishes a logical operation that is to connect two or more operations. These...

5 minutes read.

Unicode System in Java

In this tutorial, we will understand the meaning and emergence of the Unicode system in java. The Unicode system is one of the important and useful features in the world...

6 minutes read.

Volatile keyword in Java

Multiple threads can change a variable's value by using the volatile keyword. Making classes thread-safe is another application for it. It indicates that using a method or an instance of...

3 minutes read.

Java LDAP Authentication

WHAT IS LDAP? Clients can communicate with directory services by sending requests and receiving responses using the Lightweight Directory Access Protocol (LDAP). The term "LDAP server" refers to a directory service...

7 minutes read.

Various operations on the Queue using Stack in Java

The Java Collections Framework's core data structures are the Stack and Queue. They are used to store and retrieve identical data in a presentation sequence. These two linear data structures...

7 minutes read.

Figurate Number in Java

There have been several uses for figurate or figural numerals throughout history. A number that may be expressed by regular, distinct geometric shapes with spaced evenly points is referred to...

4 minutes read.

Java Math sqrt() Method

The sqrt() method of Java Math class returns the accurately rounded positive square root of the specified double value. Syntax: public static double sqrt(double a) Parameters: The parameter ‘a’ represents the number whose square...

2 minutes read.

Java Integer rotateRight() method

The rotateRight() method of Java Integer class returns the value obtained by rotating the  2’s complement binary representation of the given integer value right by the specified number of bits. Syntax public...

1 minute read.

How to Convert Octal to Decimal in Java

How to Convert Octal to Decimal in Java There are two methods to convert Octal to Decimal: Using parseInt() method Using user-defined logic Using Integer.parseInt() method The Integer.parseInt() method is a static method...

2 minutes read.

Java Binary Tree

The non-linear data structure known as a binary tree is a type of tree, and because it stores data in a hierarchical manner, it is mostly utilised for finding and...

7 minutes read.

Java Integer toUnsignedString() method

The toUnsignedString() method of Java Integer class returns a string representation of the argument as an unsigned decimal value. The second syntax returns a string representation of the given argument as...

2 minutes read.

Java Integer parseUnsignedInt() method

The parseUnsignedInt() method of Java Integer class parses the string argument as an unsigned decimal integer. The second parameter parses the string argument as an unsigned integer in the radix specified...

2 minutes read.

Tetris Game in Java

The Tetris game is among the most well-known video games ever produced for computers. Today, we may engage in this game on a mobile device as well. Alexey Pajitnov conceptualized...

12 minutes read.

Java Coding Software

Desktop and web apps are created using Java, an object-oriented programming language. Java code may be executed on any platform, making it platform-independent. A text editor, tool, or piece of...

9 minutes read.

Stock Span Problem Using Stack in Java

The stock span problem is an issue in finance where we must determine a stock’s price span over all N days given a set of N daily price quotes. The...

6 minutes read.

Dutch National Flag Problem in Java

Dutch National Flag (DNF) is a programming issue that Edsger Dijkstra put up. The white, red, and blue hues make up the Dutch flag. The goal is to haphazardly set...

6 minutes read.

Java Enum Keyword

Definition: A data type in Java called Enum has a respect to supply of constants. The weekdays (SUN, MON, TUE, WED, THU, FRI, and SAT), directions (NORTH, SOUTH, EAST, and WEST),...

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