Syntax Basics for Java Tutorials (middle)

Source: Internet
Author: User

Operation Symbols:

1), arithmetic operator.

+-*/% : the remainder of any integer is not 0 or 1, so the switching operation can be achieved as long as the remainder is changed.

+: Connector.

++,--

2), assignment operator.

=  += -= *= /= %=

3), comparison operator.

Features: This operator is characterized by the following: The result of the operation, either True or false.

4), logical operator.

& |   ^  ! && | |

Logical operator except! are used to concatenate two Boolean expressions.

&: Only both sides are true and the result is true. Otherwise, it is false.

|: If both sides are false the result is false, otherwise it is true

^: xor: And or a little different.

On both sides, the result is false.

It is true that the results are different on both sides.

& and && difference: &: No matter what the left result is, the right side is involved in the operation.

&&: Short circuit with , if the left is false, then the right side is not a parameter and operation.

| and | | difference:|: Both sides of the operation.

|| : Short circuit or , if the left is true, then the right side does not participate in the operation.

5), bitwise operator: operator used to manipulate bits.

& | ^

<< >> >>> (unsigned Right shift)

simple little exercise: Swap data for two variables. No third-party variables are required.

int a = 3,b = 5;-->b = 3,a = 5;

A = a + B; A = 8;

b = a A; b = 3;

A = a-B; A = 5;

A = a ^ b;//

b = a ^ b;//b = a ^ b ^ b = a

A = a ^ b;//a = a ^ b ^ a = b;

Exercise: efficiently Calculate 2*8 = 2<<3;

5 , statement.

If switch do and while and for

When do these statements work?

1), when judging the fixed number of values, you can use if, you can also use switch.

However, it is recommended to use switch for relatively high efficiency.

Switch (variable) {

Case value: The statement to be executed;

...

Default: the statement to execute;

}

How it works: compare the values of the variables in parentheses with the values after the case, and the values after the case are the same

Executes the statement following the case, and if there is no same, execute the statement after the default;

Details: 1): Break can be omitted, if omitted, has been executed until a break is encountered;

2): The variables in the parentheses after the switch should be one of the Byte,char,short,int four types, and some Java Tutorials also write the string type in it, which is not right;

3): Default can be written anywhere in the switch structure, if the default statement is placed in the first row, regardless of whether expression matches value in case, the program starts execution from default until the first break occurs.

2), when judging the range of data, get the Boolean type of judgment operation, you need to use if.

3), the loop structure is used when certain statements need to be executed many times.

While and for can be interchanged.

The difference is that if you need to define a variable control loop number. It is recommended to use for. Because the For loop is complete, the variable is freed in memory.

Break : acts on a switch, and a loop statement, used to jump out, or to be called an end.

When a break statement exists separately, the following do not define other statements, because the compilation fails because it is not executed. When a loop is nested, break only jumps out of the current loop. To jump out of a nested outer loop, just give the loop a name, which is called a label .

continue: only for loop structure, continue to recycle.

Effect: End this cycle and continue the next cycle. When the statement is present alone, the statement below cannot be defined and cannot be executed.

Syntax Basics for Java Tutorials (middle)

Related Article

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.