Advertisement



< Prev
Next >



Boolean Wrapper Class





Boolean wrapper class is used to create an object version of a primitive boolean value.






//Constructor of a Boolean class takes a String and a boolean value.

import java.util.*;

class A
{
public static void main(String... ar)
{

//Passing a primitive boolean value to wrapper Integer object
Boolean b1 = new Boolean(true);	


//Case of the String passed to Boolean constructor doesn't matter.
Boolean b2 = new Boolean("True");
Boolean b3 = new Boolean("TRUE"); 
Boolean b4 = new Boolean("TrUe");	

System.out.println("Value in Boolean object, b1 : "+ b1);
System.out.println("Value in Boolean object, b2 : "+ b2);
System.out.println("Value in Boolean object, b3 : "+ b3);
System.out.println("Value in Boolean object, b4 : "+ b3);

System.out.println("Calling booleanValue() method to read value in Boolean object");
System.out.println("Value in Boolean object, b1 : " + b1.booleanValue());
System.out.println("Value in Boolean object, b2 : " + b2.booleanValue());
System.out.println("Value in Boolean object, b3 : " + b3.booleanValue());
System.out.println("Value in Boolean object, b4 : " + b4.booleanValue());
}
}


Output-



Value in Boolean object, b1  : true
Value in Boolean object, b2  : true
Value in Boolean object, b3  : true
Value in Boolean object, b4  : true
Calling booleanValue() method to read value in Boolean object
Value in Boolean object, b1  : true
Value in Boolean object, b2  : true
Value in Boolean object, b3  : true
Value in Boolean object, b4  : true



Advertisement




Some important methods of Boolean wrapper class


Methods Description
boolean booleanValue() Returns a primitive boolean value out of Boolean wrapper object.
int compareTo(Boolean b)
  • Returns a zero if the invoking Boolean object contains the same value as b.
  • Returns 1 if invoking object contains true and b contains false.
  • Returns -1 if invoking object contains false and b contains true.
boolean equals(Object ob) Returns a true if invoking Boolean object has same value as referred by ob, else false.
static boolean parseBoolean(String s) Returns true if String contains true(irrespective of its case), otherwise false.
static boolean valueOf(boolean b) Returns a Boolean object, equivalent to b.
static boolean valueOf(String s) Returns true if String contains true(irrespective of its case), otherwise false.





Using compareTo() method to compare values in two Boolean objects.


Method compareTo() takes a Boolean class type object and it -
class A
{
public static void main(String... ar)
{
Boolean b1 = new Boolean("true"); //Constructor accepting String value
Boolean b2 = new Boolean(true);	  //Constructor accepting primitive boolean value

System.out.println("Value in b1 = "+b1);
System.out.println("Value in b2 = "+b2);

System.out.println("Invoking b1 to compare with b2 : "+ b1.compareTo(b2));

Boolean b3 = new Boolean("true");
Boolean b4 = new Boolean(false);

System.out.println("Value in b3 = "+b3);
System.out.println("Value in b4 = "+b4);

System.out.println("Invoking b3 to compare with b4 : "+ b3.compareTo(b4));

System.out.println("Invoking b4 to compare with b3 : "+ b4.compareTo(b3));
}
}



Output-


Value in b1 = true
Value in b2 = true
Invoking b1 to compare with b2 : 0
Value in b3 = true
Value in b4 = false
Invoking b3 to compare with b4 : 1
Invoking b4 to compare with b3 : -1





parseBoolean() method of Boolean class


Method parseBoolean(String s) takes a String class type object and -

//Wrapper class Character

import java.util.*;

class A
{
public static void main(String... ar)
{
boolean b1 = Boolean.parseBoolean("false");
boolean b2 = Boolean.parseBoolean("TRUE");
boolean b3 = Boolean.parseBoolean("tru");

System.out.println("Primitive boolean value in b1 : "+ b1);
System.out.println("Primitive boolean value in b2 : "+ b2);
System.out.println("Primitive boolean value in b3 : "+ b3);
}
}



Output-


Primitive boolean value in b1 : false
Primitive boolean value in b2 : true
Primitive boolean value in b3 : false




Please share this article -





< Prev
Next >
< Character Wrapper Class
Byte Wrapper Class>



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON


Advertisement