Java Preliminary Learning

Source: Internet
Author: User

10 What is an operator?

The operator is a functional symbol that informs Java about the operation.

Operators can be divided into the following categories:

(1) Arithmetic operators

+ (addition),-(subtraction), * (multiplication),/(division),% (for remainder), + + (self-increment),--(self-subtract)

Note: + + and--placed on the left and right sides of the operand are different, for example:

int a= 7;

int b=a++;

System.out.println (the value of "a" is: "+a);

System.out.println (the value of "B" is: "+b);

When + + is on the right side of the operand, it is self-increment after performing the operation, so the result is:

The value of A is: 7

The value of B is: 7

int a= 7;

int b=++a;

System.out.println (the value of "a" is: "+a);

System.out.println (the value of "B" is: "+b);

When + + is on the left side of the operand, the operation is performed after the increment operation, so the result is:

The value of A is: 7

The value of B is: 8

(2) Assignment Operators

= (equals), + = ( plus equals ),-= (minus equals), *= (multiply equals),/= (except equals)

a+=b; equivalent to a=a+b;

(3) comparison Operators

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

Note:< > <= >= only support both left and right operands are numeric types

= =! = Both operands can be numeric types or reference types

= = and = One is the comparison operator, one is the assignment operator

(4) logical Operators

&& (with), | | (OR),! (non), ^ (XOR)

(5) Conditional operator

The conditional operator (? :) also known as the ternary operator, the basic expression is:

Boolean expression? Expression 1: Expression 2 When the Boolean expression is true, expression 1 is executed, and expression 2 is executed when false.

For example: int a= (2>3)? (2+3): (2*3);

System.out.println (a); output is 6

11 article statements

(1) If statement

Several forms of the IF statement are as follows:

There is only one statement that can be used without braces, and multiple lines must be there.

①if (condition) {

Statements executed when the condition is established

}

②if (Boolean expression for the condition) {

Statement executed when the condition is true

}else{

Statement executed when the condition is false

}

③if (condition 1) {

Code 1

}else if (condition 2) {

Code 2

}else{

Code 3

}

(2) Switch Case statement

④if (condition 1) {

if (condition 2) {

Code

}else{

Code

}

}else{

Code

}

(2) Switch Case statement

switch (expression) {

Case 1:

EXECUTE Statement 1

Break

Case 2:

EXECUTE Statement 2

Break

Case 3:

EXECUTE Statement 3

Break

Default

Execute default Code

}

Note: 1. The value of the expression in parentheses after the switch must be an integer or character type

2. The value after the case can be a constant value, such as 1, 2, or a constant expression, such as a. T, but not a variable or an expression with a variable, such as a * 2

3, case matching, execute the program code in the matching block, if you do not meet the break will continue to execute the contents of the next box block until the break statement or switch statement block end

12 Loop statements

(1) While statement

while (judging condition) {

Looping operation statements

}

First determine whether the condition within () is False/true, which is true when the loop code inside the curly braces is executed. Features: first judgment after execution.

(2) Do While statement

D o{

Looping operations

}while (Judging condition)

Perform a loop before you decide if you want to continue execution.

(3) For statement

for (loop variable initialization; loop condition: Change of loop variable) {

Looping operations

}

Can not be omitted, three expressions can be omitted

(1) Omitting initialization

Assigning a value to a loop variable before a for statement

(2) Omit loop condition

There will be a dead loop, to avoid jumping out with Braek.

(3) Omitting the change of cyclic variables



This article is from the "popular tastes" blog, make sure to keep this source http://asalawys.blog.51cto.com/12008007/1858266

Java Preliminary Learning

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.