Convert Java Boolean Primitive to Boolean object

Java 8Object Oriented ProgrammingProgramming

To convert Boolean Primitive to Boolean object, use the valueOf() method in Java.

Firstly, let us take a boolean primitive.

boolean val = false;

To convert it into an object, use the valueOf() method and set the argument as the boolean primitive.

Boolean res = Boolean.valueOf(val);

Let us see the complete example to learn how to convert Boolean Primitive to Boolean object.

Example

 Live Demo

public class Demo {
   public static void main(String[] args) {
      boolean val = false;
      // converting to object
      Boolean res = Boolean.valueOf(val);
      System.out.println(res);
   }
}

Output

False
raja
Published on 13-Nov-2018 09:55:16
Advertisements