VOOZH about

URL: https://www.geeksforgeeks.org/java/java-lang-boolean-class-methods/

⇱ java.lang.Boolean class methods - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

java.lang.Boolean class methods

Last Updated : 19 Apr, 2022
👁 Boolean class methods.
Boolean class methods.

About : 
java.lang.Boolean class wraps primitive type boolean value in an object.
Class Declaration  

public final class Boolean
 extends Object
 implements Serializable, Comparable


Constructors :  

Boolean(boolean val) : Assigning Boolean object representing the val argument.
Boolean(String str) : Assigning Boolean object representing the value true or false
 according to the string.


Methods : 

  • booleanValue() : java.lang.Boolean.booleanValue() is used to assign value of a Boolean object to boolean primitive. 
    Syntax : 
public boolean booleanValue()
Returns : 
primitive boolean value of the boolean object.
  • compareTo() : java.lang.Boolean.compareTo(Boolean arg) compares this Boolean instance with the passed Boolean instance. 
    Syntax : 
public int compareTo(Boolean arg)
Parameter : 
arg : boolean instance to be compared with this instance.
Returns :
0 : if this instance = argumented instance.
+ve value : if this instance > argumented instance.
-ve value : if this instance < argumented instance.
  • hashCode() : java.lang.Boolean.hashCode() returns hash code value for the assigned boolean object. 
    Syntax : 
public int hashCode()
Returns : 
1231 : if the boolean value of object is true.
1237 : if the boolean value of object is false.
  • toString() : java.lang.Boolean.toString() returns string representation of the boolean object based on its value. 
    Syntax : 
public String toString()
Returns : 
string value - 'true' if boolean object is true, else false.


Implementation: 

Output: 

Boolean object - bool1 : true
Boolean object - bool2 : false
Primitive value of object i.e. bool3 : true
Primitive value of object i.e. bool4 : false

bool1 is greater than bool2 as comp = 1

Hash Code value of bool1 : 1231
Hash Code value of bool2 : 1237

String value of bool1 : true
String value of bool2 : false
  • getBoolean() : java.lang.Boolean.getBoolean(String arg) returns true, if 'true' value is assigned to the System property. 
    To assign any value to the property, we are using setProperty() method of System class. 
    Syntax : 
public static boolean getBoolean(String arg)
Parameters : 
arg - name of the property
Returns : 
true : if 'true' value is assigned to the System property.
false : if no such property exists or if exists then no value is assigned to it.


Implementation: 

Output: 

Bool Value of p1 : false
Bool Value of p2 : false

Since, no value assigned to p1, p2, Bool value is false
Assign value to p1,p2 using java.lang.System.setProperty()

Bool Value of p1 : true
Bool Value of p2 : false
  • valueOf() : java.java.lang.Boolean.valueOf(boolean arg) returns the value assigned to the Boolean variable. 
    If true value is assigned then true is returned else, false. 
    To assign any value to the property, we are using setProperty() method of System class. 
    Syntax : 
public static Boolean valueOf(boolean arg)
Parameters : 
arg - boolean variable
Returns : 
True : if true value is assigned to the boolean variable, else false 
  • parseBoolean() : java.lang.Boolean.parseBoolean(String s) returns true or false value of String argument(taken by it as Boolean). 
    It is case insensitive method. 
    Syntax : 
public static boolean parseBoolean(String arg)
Parameters : 
arg - String argument taken as Boolean
Returns : 
Boolean value of a String argument


Implementation:

Output:

Value of b1 : false
Value of b2 : true

Value of String st1 as Boolean : true
Value of String st2 as Boolean : false
Value of String st3 as Boolean : true


Comment
Article Tags: