java lang Boolean Class - java.lang.Boolean Class in Java
java.lang.Boolean Class
Boolean Class in Java
The Boolean class wraps the value of primitive data type boolean into Boolean object. An object of type Boolean contains a single field whose type is boolean. In other words the wrapper classes create objects for primitive data types. The wrapper classes are Boolean, Byte, Character, Short, Integer, Float, Long and Double.
In addition, this class provides many methods for converting a boolean to a String and a String to a boolean, as well as other constants and methods useful when dealing with a boolean.
What is the Initial or Default Value of a Boolean in Java ?
The default value for a Boolean (object) is null.
The default value for a boolean (primitive) is false.
The default value of any Object, such as Boolean, is null.
java.lang.Boolean Class Example
/* Java Boolean Class Example Save with file name BooleanExample.java */ public class BooleanExample { public static void main(String args[]) { // JAVA BOOLEAN DECLARATION Boolean b; // MEMORY ALLOCATION FOR JAVA BOOLEAN b = new Boolean(true); // JAVA BOOLEAN CLASS OUTPUT System.out.println("Java Boolean Class Example"); System.out.println("Value is : "+ b.booleanValue()); } }
java.lang.Boolean Class Example 2
/* Java Boolean Class Example Save with file name BooleanExample2.java */ public class BooleanExample2 { public static void main(String args[]) { // JAVA BOOLEAN DECLARATION Boolean b, b2; // MEMORY ALLOCATION FOR JAVA BOOLEAN b = new Boolean(true); b2 = new Boolean("false"); // JAVA BOOLEAN CLASS OUTPUT System.out.println("Java Boolean Class Example"); System.out.println("b Value is : "+ b.booleanValue()); System.out.println("b2 Value is : "+ b2.booleanValue()); System.out.println("b compareTo b2 : "+ b.compareTo(b2)); System.out.println("b equals b2 : "+ b.equals(b2)); } }
In the following example you can learn how to use static methods of Boolean Class. If you use static methods you need not to create the class instance. You can call static methods using the class name as reference.
java.lang.Boolean Class Example 3
/* Java Boolean Class Example Save with file name BooleanExample3.java */ public class BooleanExample3 { public static void main(String args[]) { // JAVA BOOLEAN CLASS STATIC METHODS USAGE System.out.println("Java Boolean Class Example"); // RETURN JAVA Boolean OBJECT WITH THE SPECIFIED PRIMITIVE boolean DATA TYPE System.out.println("valueOf : "+ Boolean.valueOf(false)); // RETURN Boolean OBJECT WITH THE SPECIFIED // PRIMITIVE boolean DATA TYPE AS String System.out.println("valueOf : "+ Boolean.valueOf("true")); // RETURN PRIMITIVE boolean DATA TYPE WITH THE SPECIFIED // PRIMITIVE boolean DATA TYPE AS String System.out.println("getBoolean : "+ Boolean.getBoolean("true")); } }