Java boolean type
In this chapter you will learn:
- What is Java boolean type
- How to create boolean Literals
- How to create Boolean value object with constructors
- How to Get boolean value from constants in Boolean class
- How to Compare two boolean values
boolean type
Java has a boolean type for logical values.
It can have only one of two possible values, true
or false
.
This is the type returned by all relational operators.
Here is a program that demonstrates the boolean
type:
public class Main {
public static void main(String args[]) {
boolean boolVariable;
boolVariable = false;/* java 2 s . c o m*/
System.out.println("b is " + boolVariable);
boolVariable = true;
System.out.println("b is " + boolVariable);
if (boolVariable) {
System.out.println("This is executed.");
}
boolVariable = false;
if (boolVariable) {
System.out.println("This is not executed.");
}
System.out.println("10 > 9 is " + (10 > 9));
}
}
Output:
Boolean Literals
Boolean literals are only two logical values: true
and false
.
The values of true
and false
do not convert into any numerical representation.
The true literal in Java does not equal 1
, nor does the false literal equal 0
.
In Java, they can only be assigned to variables declared as boolean
.
public class Main {
public static void main(String[] argv) {
boolean b = true;
// j av a 2 s. c o m
int i = b;
}
}
If you try to compile the program, the following error message will be generated by compiler.
The Boolean class wraps a primitive type boolean in an object. An object of type Boolean contains a single field whose type is boolean.
Boolean class has the methods for converting a boolean to a String and a String to a boolean.
Create Boolean value object with constructors
Boolean(boolean value)
creates a Boolean object for value.Boolean(String s)
creates a Boolean object true value for string "true" igoring case.
public class Main {
public static void main(String[] args) {
Boolean boolean1 = new Boolean("true");
System.out.println(boolean1);//from j a v a 2 s .c o m
Boolean boolean2 = new Boolean(true);
System.out.println(boolean2);
}
}
The output:
Get boolean value from constants in Boolean class
Boolean
class defines two constants to store
the two boolean
value: true
and false
.
static Boolean FALSE
stores the Boolean object value false.static Boolean TRUE
stores the Boolean object value true.
public class Main {
public static void main(String[] args) {
System.out.println(Boolean.TRUE);
System.out.println(Boolean.FALSE);
}/*from java 2 s . co m*/
}
The output:
Compare two boolean values
The following methods do the comparison for boolean
values.
int compareTo(Boolean b)
compares this Boolean instance with another.boolean equals(Object obj)
returns true if the argument is not null and is a Boolean object that represents the same boolean value as this object.
compareTo(Boolean b)
returns:
Value | Meaning |
---|---|
zero | if this object represents the same boolean value as the argument; |
a positive value | if this object represents true and the argument represents false; |
a negative value | if this object represents false and the argument represents true |
public class Main {
public static void main(String[] args) {
Boolean boolean1 = new Boolean("true");
System.out.println(boolean1);/* j a va 2 s . c o m*/
Boolean boolean2 = new Boolean(true);
System.out.println(boolean2.compareTo(boolean1));
}
}
The output:
Next chapter...
What you will learn in the next chapter: