Java Web Learning Note Chapter III Java Basics (three-operator)

Source: Internet
Author: User
Tags arithmetic operators bitwise bitwise operators logical operators java web

Java Operators

1. Arithmetic operators:

Plus (+), minus (-), multiply (*), divide (/), withdraw (%)

Self-Add (+ +), self-subtract (--)

2. Relational operators

Java relational operators are used to determine the size relationship between data

> (greater than), < (less than), >= (greater than or equal), <= (less than equals), = = (equals),! = (Not equal)

The value of the relationship expression is a Boolean type ("true" or "false")

3. Logical operators

The operation of the logical operation is both a Boolean expression and the result is true or false.

The result of each expression in the logical operator must be true or false.

Java provides the following logical operators:

&: When a long road and an operation,& are integers, they are bitwise and operators, and the two sides are relational operations, which are logical operations. Long distances and operations calculate the results of the relational operations on both sides.

&&: Short circuit and operation, when the relationship result on the left side of the operator is false, the relational operation on the right is no longer evaluated.

Or operation: The result of the operation is true when the result of the two relational operation is true, otherwise false

| : A long road or an operation, a long road or an operation on both sides is an integer, is a bitwise OR operation, on both sides is a relational operation, is a logical operation; a long road or an operation calculates the results of both sides of the operation.

|| : short-circuit or operation, when the left side of the operation of the relationship operation is true, no longer the right side of the relational operation, directly derived true results.

! : (non) Reverse the result of the relational operation.

Priority of three logical operations:

Maximum non-operation;

followed by the computation;

The lowest priority is or operation;

4. Conditional operators

The conditional operator, also known as the "Trinocular" operation, has the following structure:

Boolean expression 1? Expression 2: Expression 3

If expression 1 is true, the result is the result of expression 2

If the expression 1 is false, the result is the result of expression 3

5. Assignment operators

"=" is called an assignment operator and is used to assign a value to a variable.

The assignment expression itself also has a value, and the value itself is the value assigned.

You can use an extended assignment expression (+ =,-=,/=, *=,%=).

Eg:int B = 20;

B + = 5.5; (yes)

b = b+5.5; error ===>b = (int) (b+5.5);

6. String Join operator

"+" enables the connection of strings. You can also implement "item-linked" Strings with other data types.

Eg:System.out.println (1+2+3+ "Hello") output 6hello

Eg:System.out.println ("Hello" +1+2+3) output hello123

7, bitwise operators (high efficiency, are directly binary operations)

(1) Right shift operation

>> signed positive 0 negative complement 1

>>> unsigned positive 0 negative complement 0

Positive number: Convert to Binary, move back 2 bit front 0, last two bit erase

Move right one is equivalent to dividing by 2

Eg:int a = 10;  a>>2; Get 2

Negative: Front complement, 1 after erase

Eg:int B =-10; b>>2;

/*

Inference process

A. Get the binary of-10 first

10 binary 28 x 0 1010

Take the inverse 28 x 1 0101-->+1

-10 binary 28 x 1 0110

B. Mobile

Move right Two bits 11 28 1 01 This is the final negative number.

C. Calculating the final result

Negative negation +1 to get corresponding positive number

00 28 x 0 +1====> get corresponding positive number 00 28 x 0 11

Positive number is 3 corresponding negative number-3

*/

Unsigned right moves are specific to negative numbers, positive 0 in front, and 0 for negative numbers.

eg:b>>>2;

(2) left shift operation

<< back 0 (no matter plus or minus), move left one equivalent to multiply by 2

(3) & (Bit and), | (bit or), ^ (XOR)

      • Operand 1 & Operand 2

Eg:int c = 5,d = 4; C & D; (convert two numbers to binary, then a single operation, all 1 This is 1 otherwise 0, the resulting binary is converted to decimal)

      • Operand 1 | Number of Operations 2

Eg:int c = 5,d = 4; C | D; (convert two numbers to binary, then a single operation, all 0 this bit 01 otherwise 1, the resulting binary is converted to decimal)

      • Operand 1 ^ operand 2

Eg:int c = 5,d = 4; c ^ D; (convert two numbers to binary, then a single operation, the same as 0 is not the same as 1, the resulting binary is converted to decimal)

c = a ^ b; Then B equals c^a; A equals the result of c^b;

Java Web Learning Note Chapter III Java Basics (three-operator)

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.