Javatpoint Logo
Javatpoint Logo

Boolean values in Java

In programming, we generally need to implement values that can only have one of two values, either true or false. For this purpose, Java provides a special data type, i.e., boolean, which can take the values true or false.

The boolean values can be declared with the boolean keyword. Consider the following example.

Output:

true
you are logged into the system

For general purpose, we often use boolean expressions that return boolean value either true or false. For example, the comparison operator returns boolean values.

Output:

false

Boolean Class in Java

In Java, the package java.lang provides the definition for a wrapper class Boolean that wraps the primitive type boolean in the object. The class also provides useful general-purpose methods that can be used to convert boolean values to string or vice-versa.

This class provides two constructors to create the objects, which are given below.

The above constructor accepts a primitive boolean value and creates an object which contains a single boolean value.

The above constructor creates a boolean object which contains a value "true" if the string argument is not null and is equal (ignore case) to the string "true". Otherwise, a boolean value false will be created.

Boolean class provides the following methods.

SN Definition Description
1 public static boolean parseBoolean(String s) public static boolean parseBoolean(String s) This method is used to parse the string argument as the boolean value. If the string value is not null and is equal to (ignore case) true, then a boolean value true is returned otherwise false.
2 public boolean booleanValue() This method returns the value of the boolean objects as primitive boolean type
3 static Boolean valueOf(boolean b) This method returns the boolean instance that represents the specified boolean argument. It returns Boolean.TRUE if the specified argument is true; otherwise, it returns Boolean.FALSE.
4 static Boolean valueOf(String s) This method returns the boolean instance that represents the specified string value. If the specified value is not null and equal to true (ignore case), otherwise it returns false.
5 static String toString(boolean b) This method converts the specified boolean argument to a string value. It is a static method.
6 String toString() This method converts this boolean object to the string value.
7 int hashCode() This method returns the hashcode for this boolean object.
8 boolean equals(Object obj) This method compares this boolean object to the specified object and returns true if it is not null and represents the same boolean value as this object.
9 int compareTo(Boolean b) This method is used to compare this boolean object to the specified boolean object and returns 0 if both are equal.
10 int compare(boolean x, boolean y) This method is used to compare the specified boolean primitive values.





Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA