Java has a simple type that represents a logical value, called a Boolean. Its value can only be one of the two values, true or FALSE. It is all of the following programs, such as a, that illustrate the use of Boolean types:
Demonstrate Boolean values.
Class Booltest {public
static void Main (String args[]) {
Boolean B;
b = false;
System.out.println ("B is" + B);
B = true;
System.out.println ("B is" + B);
A Boolean value can control the IF statement
if (b) System.out.println ("This is executed.");
b = false;
if (b) System.out.println ("This isn't executed.");
Outcome of a relational operator is a Boolean value
System.out.println (' > 9 is ' + (+ > 9);
}
}
The results of this program run as follows:
B is false
B are true this is
executed.
> 9 is True
There are 3 interesting things to note about this program. First, you've seen that when you output a Boolean value using the method println (), the display is "true" or "false." Second, the value of the Boolean variable itself is sufficient to control the IF statement. There is no need to write an if statement as follows:
Third, the result of a relational operator (for example, <) is a Boolean value. This is why the display value of an expression 10>9 is "true." Additionally, additional parentheses are added on both sides of the expression 10>9 because the plus "+" operator is higher than the operator ">" precedence.
The difference between logical operations and bitwise operations for Java Boolean types
from the result, 2 kinds of results are the same, but the logical operation will have a "short circuit" phenomenon, bitwise not, and bitwise more than the logical operation of the "different or" function.
Short Circuit phenomenon
Class BR {
static Boolean F1 () {return
false;
}
Static Boolean F2 () {return
true;
}
Static Boolean F3 () {return
true;
}
}
Boolean f_1 = Br.f1 () &&br.f2 () &&br.f3 ();
The result is false, when the F1 () is false, then the subsequent && operations do not know the result, Java will occur "short-circuit" on the subsequent operation of a little, performance improvement.
Boolean f_2 = Br.f2 () | | BR.F1 () | | BR.F3 ();
The result is true, and the same F2 () is true, followed by no more shipping.
It seems very convenient and efficient, but there are still shortcomings.
Boolean f_3 = Br.f2 () | | BR.F3 () &&br.f1 ();
The result becomes true, the correct should be false, this is the error caused by the "short-circuit", and the parentheses are needed to get the correct answer:
F_3= (br.f2 () | | BR.F3 ()) &&br.f1 ();
Bitwise operations provide no differences or functions that are logical:
Result F = false;