beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Convert boolean Primitive to Boolean object in Java

By Chaitanya Singh | Filed Under: java.lang.Boolean

Description

Program to convert boolean primitive to Boolean object

Example

class BooleanPrimToBooleanObj {
	 
  public static void main(String[] args) {
     boolean bvar = true;
	   
     /* Method 1: By passing boolean value to
      * the constructor of Boolean class
      */
     Boolean bObj = new Boolean(bvar);
     System.out.println(bObj);
	   
     /* Method 2: By passing boolean value to the 
      * valueOf() method of Boolean class.
      */
     Boolean bObj2 = Boolean.valueOf(bvar);
     System.out.println(bObj2);
  }
}

Output:

true
true

public static Boolean valueOf(boolean b): Returns a Boolean instance representing the specified boolean value. If the specified boolean value is true, this method returns Boolean.TRUE; if it is false, this method returns Boolean.FALSE. If a new Boolean instance is not required, this method should generally be used in preference to the constructor Boolean(boolean), as this method is likely to yield significantly better space and time performance.

Source: valueOf(boolean obj) method – Boolean javadoc

Enjoyed this post? Try these related posts

  1. Convert String to boolean primitive in Java: parseBoolean() method
  2. Cast Boolean Object to boolean in Java – booleanValue() Method
  3. Boolean.hashCode() Method in Java
  4. Convert String Object to Boolean Object in Java
  5. Compare boolean values in java – compareTo() Method

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2019 BeginnersBook . Privacy Policy . Sitemap