[Java02] Operator

Source: Internet
Author: User

[Java02] Operator
Arithmetic Operators

Java uses four types of operations: +,-, *, And/, indicating addition, subtraction, multiplication, and division. % indicates the remainder operation. If the two numbers involved in the operation are integers, the integer division is used. Otherwise, the floating point division is used. 15/7 = 2.0% 2 = 7.5/=.
Simplified Binary Arithmetic Operator number:

+ =
X + = 4, equivalent to x = x + 4-=
X-= 4 is equivalent to x = X-4 * =
X * = 2 is equivalent to x = x * 2/=
X/= 2 is equivalent to x = x/2. Auto-increment and auto-increment Operators

Prefix auto-increment: ++ n
Suffix auto-increment: n ++

int a=6;int b=6;int m=++a //m=7,a=7int n=b++ // n=b,b=7

During prefix operations, n is first added with 1 before the operation. During suffix operations, n is first computed with 1 after the operation.

Relational operators
Operator Function
= Checks whether values of equal signs are equal.
! = Checks whether the values on both sides of the equal sign are not equal.
> Greater
< Less
> = Greater than or equal
<= Less than or equal
Logical operators

& Indicates logic and. If A & B is true only when A and B are true, A | B. If either A or B is true, A | B is true. & | calculate the value based on the short-circuit mode. If the first operand can determine the result of the expression, the second operand is no longer calculated.

x!=0 && 1/x>x+y
Bitwise operators

Four bitwise operators:

&
Take 29 as an example. The binary value of 29 is (11101), the binary value of 16 is (10000), and the result of 29 & 16 is 16 (1 0000)
  System.out.println(29&16);  //16
| Or ^ exclusive or ~ Non <left shift
Move left is similar to move the decimal point. move the decimal point to the left by 10 times, and move the decimal point to the left by 2 times in the computer. Shift n places to the left to enlarge the n power of 2.> shift right
The right shift is the opposite of the Left shift.
int a=16;System.out.println(a>>2); //a=4System.out.println(a<<2); //a=64

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.