Java Knowledge points (ii)

Source: Internet
Author: User
Tags arithmetic arithmetic operators bitwise integer division logical operators

1. Variables

Defined:

A variable is a small box that loads data in memory, and you can only use it to store data and fetch data.

To define the syntax format for a variable:

Data type variable name = data value

Considerations for variable use:

A variable can be assigned without assigning a value after it is defined. No assignment cannot be used

Variables cannot be defined repeatedly

2. Data type Conversion

Various data types are listed from small to large by data range: Double, float, long, short, int

There are two ways of data type conversions:

Automatic type conversion

A data type that represents a small range is converted to a large range of data types, which is called automatic type conversion

Automatic type conversion format:

Data type variable with large range = data type value with a small range

Cases:

Double d = 1000;

Or

int i = 100;

Double D2 = i;

Forcing type conversions

A data type that represents a large range is converted to a small range of data types, which is called coercion type conversion

Forced type conversion Format:

Range small Data type variable = (range small data type) range large data type value

Cases:

int i = (int) 6.718;-->6

Or

Double d = 3.14;

int i2 = (int) d; -->3

3. Operators

Arithmetic operators:

An operator is a symbol used to calculate data. The data can be constants, or it can be variables. The number of operations that are manipulated by operators is called operands.

The most common operation of arithmetic operators is to involve the operands in mathematical calculations, using the following:

Operator

Arithmetic rules

Example

Results

+

Positive sign

+3

3

+

Add

2+3

5

+

Connection string

"Chinese" + "country"

China

-

Minus sign

int A=3;-a

-3

-

Reducing

3-1

2

*

By

2*3

6

/

Except

5/2

2

%

Take the mold

5/2

1

++

Self-increment

int A=1;a++/++a

2

--

Self-reduction

int B=3;a--/--a

2

Note the following items:

The addition operator should be aware when connecting strings that only the strings are added directly to the string

Division "/" when the two sides are integers, take the integer part, the remainder. When one side is floating-point, divide by normal rule

"%" is the divisible-by-remainder symbol, and the fractional remainder is meaningless. The result symbol is the same as the remainder symbol

Integer do dividend, 0 can not do divisor, otherwise error

The arithmetic operator + + 、--used:

When the ++,--operator is placed, the value of variable A is increased by 1 or self-minus 1 by using the original value of variable A to participate in the operation, when the operation is completed.

++,--operator precedes, increment the value of variable a by 1 or decrement by 1, and then participate in the operation with the new updated value

Assignment operators:

An assignment operator is a symbol that assigns a value to a variable, and the assignment operator is used to see:

  

Operator

Arithmetic rules

Example

Results

=

Assign value

int a=2

2

+=

Add after Assignment

int a=2,a+=2

4

-=

Reduced-value Assignment

int a=2,a-=2

0

*=

Multiply post-Assign value

int a=2,a*=2

4

/=

Assign value after integer division

int a=2,a/=2

1

%=

Assign value after modulo

int a=2,a%=2

0

Comparison operators:

A comparison operator, also known as a relational operator, that is used to determine the size and equality of two operands, and the result is a Boolean value of TRUE or False

Operator

Arithmetic rules

Example

Results

==

Equivalent to

4==3

False

!=

Not equal to

4!=3

True

<

Less than

4<3

False

>

Greater than

4>3

True

<=

Less than or equal

4<=3

False

>=

Greater than or equal

4>=3

True

Comparison operator Considerations:

The = sign of the assignment operator differs from the = = Symbol of the comparison operator:

The = sign of the assignment operator, which is used to assign the value to the right of the = symbol to the left of the = symbol

the = = Symbol of the comparison operator, which is used to determine whether the value of the left and right variables of the = = symbol is equal

Logical operators:

Logical operator, which is used for Boolean operations, the final result of which is a Boolean value of TRUE or False

Operator

Arithmetic rules

Example

Results

&

And

False&true

False

|

Or

False|true

True

^

XOR or

True^flase

True

!

Non -

!true

Flase

&&

Short Circuit and

False&&true

False

||

Short Circuit or

false| | True

True

Logical operator rule:

Short-circuit and &&: Data on both sides of the operation, with false, the result of the operation is false

Short Circuit or | |: The data on both sides of the operation, with true, the result of the operation is true

Logical Non! : Data that participates in the operation, which is true, turns false, and false turns true

Ternary operators:

Format:

(conditional expression)? Expression 1: Expression 2

Ternary operator Operation rules:

Determine the value of the conditional expression first, if true, the result of the operation is an expression of 1; if false, the result of the expression is 2

Operator Precedence:

Priority level

Describe

Operator

1

Brackets

()、[]

2

PLUS sign

+,-

3

Self-increment, non-

+ + 、--、!

4

multiplication, taking surplus

*、/、%

5

Add and Subtract

+,-

6

Shift Operations

<<, >>, >>>

7

Size relationship

>, >=, <, <=

8

Equality relationship

= =,! =

9

Bitwise-AND

&

10

Bitwise XOR OR

^

11

Bitwise OR

|

12

Logic and

&&

13

Logical OR

||

14

Conditional operations

?:

15

Assignment operations

=, + =,-=, *=,/=,%=

16

Bit assignment operations

&=, |=, <<=, >>=, >>>=

Java Knowledge points (ii)

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.