×

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

How to find characters with the maximum number of times in a string java

Problem statement In this problem, users want to find the maximum count of a character from the string and return the character along with its count. Your task is to create...

3 minutes read.

How to Split the String in Java with Delimiter

In Java, splitting strings is a significant and typically used activity while coding. Java gives different ways of dividing the String. The most widely recognized way is to use the...

3 minutes read.

Java Delete File

There are two techniques to erase a record in Java: Utilizing File.delete() technique.Utilizing File.deleteOnExit() technique. Using File.delete() technique: In Java, we can erase a document by utilizing the File.delete() technique for File class....

2 minutes read.

How to Convert String to Object in Java

How to Convert String to Object in Java The Object is the super class of all classes. So you can assign a string to Object directly. There are two methods to...

3 minutes read.

Static vs Non-Static in Java

A static method can access and update the value of a static data member. The static keyword is mostly used in Java for memory management. The static keyword can be...

6 minutes read.

Interleaving string in Java

If the string Str3 contains all of the characters from Str1 and Str2, it is considered interleaving Str1 and Str2. Keep in mind that the order of all characters in...

5 minutes read.

Number Pattern Programs in Java

Number Pattern Programs in Java: Number pattern programs are part of pattern programs. In the previous section, we have learned the approach to print the pattern program in Java. To...

6 minutes read.

Hollow Diamond Pattern in Java

Why are patterns important? Programmers frequently create Java pattern programs to practice coding and ace interviews. Interviewers frequently test candidates' logical reasoning and implementation by asking about pattern programs. Hollow Diamond Pattern The...

7 minutes read.

Bean class in Java

The web applications that are created with the help of the JSP or Java Server Pages generally have fairly more functionality than the web pages that specifically are created with...

5 minutes read.

Heart Pattern in Java

Heart Pattern is yet another intricate pattern program, however, due to its complexity, interviewers hardly ever inquire about it. Method for Printing the Heart Number Pattern Put the value of the total row...

2 minutes read.

Java While Keyword

Depending on a specified Boolean condition, a while loop in Java allows code to be executed repeatedly. The while loop can be viewed as an iterative version of the if...

3 minutes read.

How to check version of java in Linux

Java is one of the most famous and thoroughly utilized programming tongues from one side of the world to the other. On the off chance that you are a Java...

2 minutes read.

JAR File in Java

What is a JAR File? JAR stands for Java Archive. It is a file format mainly used to combine many Java class files and the corresponding metadata and resources into one...

4 minutes read.

Decagonal Numbers in Java

This section explains what is a decagonal number and how to write Java programmes that compute decagonal numbers. Both academics and Java programmer interviews regularly question about the Decagonal number...

3 minutes read.

AES 256 Encryption in Java

Now a days, security has grown in importance. Java programming supports a variety of encryption and hashing methods, which offers security for data transport and communication among various nodes. In...

4 minutes read.

Java Math asin() Method

The asin() method of Math class computes the trigonometric Arc Sine (inverse of sine ) of an angle. The value returned is between -pi/2 to pi/2. Syntax: public static double asin(double a) Parameters: The...

1 minute read.

Java Math min() Method

The min() method of Math class returns the smaller of two arguments. The arguments can be of double, float, int or long data type. Syntax: public static double min (double a, double...

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

Java Abstraction

Java Abstraction Abstraction is an advanced feature of Java to make it transparent. The main motive behind the abstraction is to deal with ideas, not with events. Abstraction is a process...

4 minutes read.

Java array list remove time complexity

Java: We know that java is one of the programming languages. The main feature of java which is not in C or object oriented programming language is platform independence. Not only the...

7 minutes read.