0% found this document useful (0 votes)
1 views

Boolean class

The Java Boolean class wraps a boolean primitive in an object and provides various methods for manipulation, comparison, and conversion of boolean values. Key methods include logical operations, parsing strings to booleans, and retrieving hash codes. Example code demonstrates the usage of these methods in practical scenarios.

Uploaded by

mohol college
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Boolean class

The Java Boolean class wraps a boolean primitive in an object and provides various methods for manipulation, comparison, and conversion of boolean values. Key methods include logical operations, parsing strings to booleans, and retrieving hash codes. Example code demonstrates the usage of these methods in practical scenarios.

Uploaded by

mohol college
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Java Boolean class

The Boolean class wraps a value of the primitive type boolean in an


object. Its object contains only a single field whose type is boolean.

Methods

Methods Description

booleanValu Returns a Boolean primitive for the value of this Boolean object.
e()

compare() Compares two Boolean values.

compareTo() Compares this Boolean instance with another.

equals() Returns a Boolean value true if the argument is a Boolean object that
represents the same value as this object.

getBoolean() Returns a Boolean value true if the system property name is equal to
the string "true".

hashCode() Returns a hash code for the Boolean object.

logicalAnd() Returns the result of implementing logical AND operation on the


assigned boolean operands.

logicalOr() Returns the result of implementing logical OR operation on the


assigned boolean operands.

logicalXor() Returns the result of implementing logical XOR operation on the


assigned boolean operands.

parseBoolea Parses the string argument as a Boolean.


n()

toString() Returns a String instance representing the specified Boolean's value or


the specified boolean.

valueOf() Returns a Boolean instance representing the specified Boolean value or


string value.
Example 1

1. public class JavaBooleanExample1


2. {
3. public static void main(String[] args)
4. {
5. Boolean b1= true;
6. boolean b2=false;
7. //assigning boolean value of b1 to b3
8. Boolean b3= b1.booleanValue();
9. String str1 = "Value of boolean object "+b1+" is "+b3+".";
10. System.out.println(str1);
11. //compare b1 and b2
12. int val1 = Boolean.compare(b1,b2);
13. if(val1>0)
14. {
15. System.out.println("b1 is true.");
16. }
17. Else
18. {
19. System.out.println("b2 is true");
20. }
21. // logicalAnd() with return the same result as AND operat
or
22. Boolean val2 = Boolean.logicalAnd(b1,b2);
23. System.out.println("Logical And will return "+val2);
24. }
25. }

Output:

Value of boolean object true is true.


b1 is true.
Logical And will return false
Example 2

1. public class JavaBooleanExample2


2. {
3. public static void main(String[] args)
4. {
5. Boolean b1 = true;
6. System.out.println("Boolean value = " + b1);
7. //prints the hash code of the boolean value
8.
System.out.println("Hash Code for boolean value = " + b1.hashCode
());
9. //converting boolean value tp String
10. String str = b1.toString();
11. System.out.println("String value = " + str);
12.
System.out.println("Hash Code for String Value = " + str.hashCode()
);
13. // will return a boolean instance corresponding to Boolean
b1
14. Boolean b2 = Boolean.valueOf(b1);
15.
System.out.println("valueOf() method will return = " + b2);
16. boolean val1 = false;
17. for (int i = 0; i < 10; i++)
18. {
19. if (i == 5)
20. {
21. System.setProperty("val1", "true");
22. break;
23. }
24. }
25. boolean b3 = Boolean.getBoolean("val1");
26. System.out.println("value of val is " + b3);
27. }
28. }
Test it Now

Output:

Boolean value = true


Hash Code for boolean value = 1231
String value = true
Hash Code for String Value = 3569038
valueOf() method will return = true
value of val is true

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy