Basic Introduction to Boolean types in Java _java

Source: Internet
Author: User
Tags bitwise

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:

if (b = = True) ... 

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:

Boolean f = true^true;

Result F = false;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.