5. Java operators

Source: Internet
Author: User

1. Arithmetic operators

Note:% take remainder

(1) Self-increment

(+ +) Pre-increment: The first self-increment complete, and then the entire expression, the statement semicolon before the expression is an operation;

After self-increment, the first operation of the entire expression (the semicolon before the expression), and then the self-increment;

2. Assignment Operators

Operator

Operation

Example

Results

=

Assign value

a=3,b=2

a=3,b=2

+=

Plus equals

A=3,b=3;a+=b;

a=5,b=2;

-=

Minus equals

A=3,b=2,a-=b;

a=1,b=2;

*=

Multiply equals

A=3,b=2,a*=b;

a=6,b=2

/=

except equal to

A=3,b=2,a/=b;

a=1,b=2;

%=

Modulo equals

A=3,b=2,a%=b;

a=1,b=2

3. Comparison operators

Operator

Operation

Example

Results

==

Equivalent to

4= =3

False

!=

Not equal to

4!= 3

True

<

Less than

4 < 3

Flase

>

Greater than

4>3

True

<=

Less than or equal

4<=3

False

>=

Greater than or equal

4>=3

True

instanceof

Check if the object is a class

"Hello" instanceof String

True

Note the details:

    1. When using comparison operators, it is required that the two data types must be identical.

4. Logical Operators

&& with: There is a false. The result is false only if both sides are true and the result is true.

|| Or: Two sides as long as there is a true result is true, when both sides of the same false results are false

^ XOR: The same result on both sides is false on both sides of a different result is true;

! Not the opposite.

Logical operators are used to concatenate Boolean expressions that cannot be written as 3<x<6in Java and should be written in X>3 & X<6.

(1) The difference between "&" and "&&":

Single and the left, whether true or false, on the right side of the operation;

Double and when, if the left is true, the right side participates in the operation, if the left is false, then the right side does not participate in the operation.

(2) "|" and "| |" The difference is the same.

(3) "^" Xor and "|" Or the difference is:

When both left and right are true, the result is false.

5. Ternary operators

Format

(conditional expression)? Expression 1: Expression 2;

Returns 1 if the condition is true;

Returns 2 if the condition is false;

Example:

1 Gets the large number of two numbers.

int x=3,y=4,z;

z = (x>y)? The x:y;//z variable stores a large number of two numbers.

5. Java operators

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.