- java.lang.Boolean Class is used to represent primitive boolean value in the form of object.
- public final class Boolean
- extends Object
- implements Serializable, Comparable<Boolean>
Boolean Class Constructors:
1. public Boolean(boolean value)
- Boolean obj=new Boolean(true);
Java code to convert boolean primitive to Boolean object
- package com.instanceofjavatutorial;
- public class BooleanDemo {
- public static void main(String[] args) {
- Boolean obj=new Boolean(false);
- System.out.println(obj);
- }
- }
Output:
- true
2. public Boolean(String s)
- Boolean b=new Boolean("true");
Java Program to Convert String Object to Boolean Object
- package com.instanceofjavatutorial;
- public class BooleanDemo {
- public static void main(String[] args) {
- String str="true";
- Boolean obj=new Boolean(str);
- System.out.println(obj);
- }
- }
Output:
- true
Boolean class Methods:
1.public boolean booleanValue()
- This method gives the boolean primitive value from Boolean object
- See below java code which explains how to get boolean primitive value and assig to primitive variable from Boolean Object.
Java Program To Convert boolean primitive value to Boolean Object and To get boolean value from Boolean Object
- package com.instanceofjavatutorial;
- public class BooleanDemo {
- public static void main(String[] args) {
- // converting boolean variable in to Boolean Object
- Boolean bobj=new Boolean(true);
- System.out.println(bobj);
- //converting Bolean object into boolean primitive value
- boolean bval=bobj.booleanValue();
- System.out.println(bval);
- }
- }
Output:
- true
- true
No comments