Java BASICS (3) JavaSE and javase

Source: Internet
Author: User

Java BASICS (3) JavaSE and javase

I wrote some notes about operators in the previous section. At the beginning of this section, I still explain some notes about operators!

Comparison operators except>,> =, <, <=, =, and ,! = Note instanceof: Check whether it is a class object, for example, "Hello" instanceof String. The returned result is true.

1. logical operators

& (And) features of the operation: either side of the operation is false, and the result must be false. The result is true only when both sides are true;

| (OR) operation features: If either side of the operation is true, the result must be true. If both sides are false, the result is false;

^ (Exclusive or) operation features: if the results on both sides of the operation are the same, the result is false, the results on both sides are different, and the result is true;

! (Non) Computing Features: Determine the other side of a thing;

& (Short Circuit dual and) features: The operation is basically the same as &, but & when the left side is false, the right side is not involved in the operation, and both sides are involved in the operation;

| (Double or short circuit) operation features: and | the operation is basically the same, only | when the left side is true, the right side is not involved in the operation, but | both sides have to be computed;

2. bitwise operators

& (Bitwise operator): the features of bitwise operations are the same as those of logical operators. They are only binary bitwise operations, for example, 6 & 3 = 2. (Binary principle: 110 & 011 = 010), any number and bit is 1 & the operation is the original number (some valid digits are used );

| (OR operator): the features of the operation or the same as those of the logical operator. It is only binary bitwise operations, for example, 6 | 3 = 7; (Binary principle: 110 | 011 = 111 );

^ (XOR operator): The operation features are the same as those of the logical operator ^. It is only a binary bitwise operation, for example, 6 ^ 3 ^ 3 = 6. Note: when a number exclusive or operation is performed on the same number twice, the result is still the number itself (used for encryption)

~ (Anti-code operator): Operator features and operators in logical operators! Consistent.

<(Left shift operator): The number of digits to be moved to the left is actually multiplied by the power of 2. The power operation can be completed!

> (Right shift operator): the right shift is actually the power of dividing the data by 2. For the vacant positions that appear in the high positions, the old vacant positions are filled with the old ones!

>>> (Unsigned right shift operator): when the data is shifted to the right, no matter what the original high is, the vacancy is supplemented with 0.

Exercise:

1. // the most efficient way to calculate 2 multiplied by 8 equals to a few? Answer: System. out. println (2 <3 );

2. How to swap the values of Two integer variables?

Answer:

Third-party variables are used during development because of high readability.
Int c;
C =;
A = B;
B = c;

Do not use this method. If the values of the two integers are too large, they will be beyond the int range and will be forcibly converted. Data MEETING
Change.

A = a + B; // a = 3 + 5; a = 8;
B = a-B; // 3 + 5-5 = 3; B = 3;
A = a-B; // 3 + 5-3 = 5; a = 5;

Used during the interview.
A = a ^ B; // a = 3 ^ 5;
B = a ^ B; // B = (3 ^ 5) ^ 5; B = 3;
A = a ^ B; // a = (3 ^ 5) ^ 3; a = 5;

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.