Boolean operators in the-java of Oracle face test questions

Source: Internet
Author: User
Tags bitwise bitwise operators logical operators

Important point:

(&, |) ==> binary Boolean operator, (&&,| |) ==> Conditional Boolean operator

Binary Boolean operator, both sides will execute, regardless of whether the left is true or false ==> the operands on both sides of the operator, whether true or false, will be calculated first and then the Boolean logic operation

But the conditional Boolean operator is different, more advanced, if the && left is false, then the right side is not executed; The left is true and the right is not executed;

Specific details:

Boolean operators in Java can be divided into "binary Boolean operators" and "conditional Boolean operators".

1. Binary Boolean operators Binary Boolean operators: With (&), or (|), XOR (^), when their operands are Boolean, they are logical operators, note: For operators on both sides of the operands, whether true or false, the result is calculated first and then the Boolean logic operation; When the operand is an integer, They are the bitwise operators.   2. Conditional Boolean operators Conditional Boolean operator: Condition with (&&), condition or (| |), condition non (!) operator, only the Boolean value can be evaluated, and from left to right, if an operand is true, the operand to its right is no longer evaluated, and the result is calculated directly. look at an example:
public class Test4 {private static int j=0;private static Boolean methodB (int k) {J+=k;return true;} public static void MethodA (int i) {Boolean b;b=i<10|methodb (4); b=i<10| | MethodB (8);} public static void Main (string[] args) {//TODO auto-generated method stub      MethodA (0);      System.out.println (j); } }

The result is:

4

Because the bitwise operator | , both sides to execute, | | operator because the left is true, only the left is executed;

The bitwise operator is primarily for binary, and it includes: "and", "Non", "or", "XOR". On the surface it seems a bit like a logical operator, but the logical operator is a logical operation on two relational operators, while the bitwise operator is mainly for the bits of two binary numbers. Each bit operator is described in detail below.


1. With operator
And the operator is denoted by the symbol "&", which uses the following rules:
The two operands have a bit of 1 and the result is 1, otherwise the result is 0, for example, the following program segment.
public class Data13
{
public static void Main (string[] args)
{
int a=129;
int b=128;
System.out.println ("A and B" with the result is: "+ (a&b));
}
}
Run results
A and B with the result is: 128
The following analysis of this program:
The value of "a" is 129, converted to binary is 10000001, and the value of "B" is 128, converted to binary is 10000000. According to the operator's operation Law, only two bits are 1, the result is 1, you can know the result is 10000000, that is, 128.


2. Or operator
Or operator with the symbol "|" Shows that its operation law is as follows:
Two bits as long as there is a 1, then the result is 1, otherwise 0, see a simple example below.
public class Data14
{
public static void Main (string[] args)
{
int a=129;
int b=128;
System.out.println ("A and B" or the result is: "+ (a|b));
}
}
Run results
A and B or the result is: 129
The following analysis of this program segment:
The value of a is 129, the conversion to binary is 10000001, and the value of B is 128, converted to binary is 10000000, according to the operator's operation Law, only two bits have a 1, the result is 1, you can know the result is 10000001, that is 129.


3. Non-operator
The non-operator is denoted by the symbol "~", and its Operation law is as follows:

If the bit is 0, the result is 1, if the bit is 1, the result is 0, see a simple example below.
public class Data15
{
public static void Main (string[] args)
{
int a=2;
SYSTEM.OUT.PRINTLN ("A non-result is:" + (~a));
}
}


4. Xor operator
The XOR operator is denoted by the symbol "^", and its Operation law is:
The two operand bits, the same result is 0, and the result is 1. Let's look at a simple example.
public class Data16
{
public static void Main (string[] args)
{
int a=15;
int b=2;
System.out.println ("A and B xor the result is:" + (a^b));
}
}
Run results
The result of A and B xor is: 13
Analysis of the above program segment: The value of a is 15, converted to binary 1111, and the value of B is 2, converted to binary 0010, according to the different or the Operation law, can be derived from the result of 1101 is 13.

Boolean operators in the-java of Oracle face test questions

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.