Following is an example to understand the functionality of
java.lang.Boolean.booleanValue() method.
DemoBoolean.java
[java]
package com.SPlessons;
public class DemoBoolean {
public static void main(String[] args) {
// b is object
Boolean b;
// assign value to b
b = new Boolean(true);
// create a boolean primitive type bool
boolean bool;
// assign primitive value of b to bool
bool = b.booleanValue();
String str = "The Primitive value of Boolean object " + b + " is " + bool;
// print bool value
System.out.println( str );
}
}
[/java]
Where created the object to the Boolean that is
b and also created the primitive to the Boolean that is
bool.
Output: The functionality of booleanValue() is that to make the Boolean object as boolean primitive.
[java]The Primitive value of Boolean object true is true[/java]
Following is the code for
java.lang.Boolean.compareTo(Boolean b) method.
[java]
package com.SPlessons;
public class DemoBoolean {
public static void main(String[] args) {
// b1, b2 are objects
Boolean b1, b2;
// values to b1, b2
b1 = new Boolean(true);
b2 = new Boolean(false);
// create an int res
int res;
// compare b1 with b2
res = b1.compareTo(b2);
String str1 = "Both values are equal ";
String str2 = "Object value is true";
if( res == 0 ){
System.out.println( str1 );
}
else if( res > 0 ){
System.out.println( str2 );
}
}
}
[/java]
Where the developer is comparing the two objects by using the method
compareTo()
and written some logic by using two strings.
Output: The functionality of compareTo() is to compare the Boolean instance with another.
[java]Object value is true[/java]