Java basic syntax (2)

Source: Internet
Author: User

Java basic syntax (2)

Next I will go on to the previous article: getting started with JavaSE 5: Java basic syntax (1) Let's take a look at the basic syntax of Java.

Five Operators

An operator is a "function" symbol used to notify Java to perform related operations. For example, we need to set the age variable to 20.

A "=" is required to tell the program to assign values.

Common operators in Java can be divided into the following types: Arithmetic Operators, value assignment operators, comparison operators, logical operators, and conditional operators.

(1) Arithmetic Operators

Arithmetic Operators are mainly used for basic arithmetic operations, such as addition, subtraction, multiplication, and division.

Common Arithmetic Operators in Java:

In this example, ++ and -- can appear both on the left side of the operand and on the right side, but the result is different.

% Is used to evaluate the remainder, also known as the modulo operator.

(2) value assignment operator

A value assignment operator is a symbol that specifies a value for a variable or constant. For example, you can use "=" to assign the result of the expression on the right to the operand on the left.

Common Value assignment operators supported by Java are shown in the following table:

(3) Comparison Operators

The comparison operator is used to determine the size of two data types, for example, greater than, equal to, or not equal. The comparison result is a Boolean value (true or false ).

The following table lists common comparison operators in Java:

1) >,<>=, <=only the operands on both sides of the left and right are numerical values.

2) = ,! = The operands on both sides can be numeric or reference type.

(4) logical operators

Logical operators are mainly used for logical operations. Common logical operators in Java are shown in the following table:

We can understand the logical operators from the perspective of "voting for election:

1) and: requires everyone to vote for a certain topic.

2) or: only one person is required to vote for a topic.

3) Non: a user who originally voted for the vote can invalidate the vote by using non-operators.

4) exclusive or: only one person who votes to agree can pass a certain topic.

When using logical operators, we will encounter an interesting "Short Circuit" phenomenon.

For example: (one> two) & (one If the two running result is false, the system considers it unnecessary to execute

One on the right < p=""> <>

Similarly, in (one> two) | (one

Perform one on the right < p=""> <>

(5) conditional Operators

Conditional operators (? :) Is also called the "ternary operator ".

Syntax: Boolean expression? Expression 1: expression 2;

Operation Process: If the Boolean expression value is true, the value expressed as 1 is returned; otherwise, the value of expression 2 is returned.

(6) operator priority

The so-called priority is the order of operation in the expression. The following table lists the priorities of common operators in Java:

Statement 6
(1) if

In life, we often need to make judgments before deciding whether to do something. For example, if the score is greater than 90, you will be rewarded with one

The IPHONE 5S can use the if Condition Statement to determine if conditions are met before execution.

Syntax:

Execution Process:

 

For example:

 

If there is only one execution statement when the if condition is set, braces can be omitted. However, if there are multiple execution statements, braces cannot or

Missing.

(2) if... else of Java conditional statements

If... else statements have more operations than if statements: when the condition is set, the code block in the if part is executed; when the condition is not set, the code block in the else part is entered.

For example, if the exam score is greater than 90, an IPHONE 5S will be rewarded; otherwise, 500 push-ups will be fined.
Syntax:

 

Execution Process:

 

 

For example:

 

 

(3) Multiple if statements with Java Conditions

 

When condition 1 does not meet multiple if statements, condition 2 is judged. if none of the current conditions are met, the generation in the else block will be executed.

. For example, if the exam score is greater than 90, an IPHONE 5S is rewarded, and if the score is between 70 and 90, a red rice is rewarded, no

The penalty is 500 push-ups.

Syntax:

Execution Process:

 

For example:

 

(4) nesting of Java conditional statements if

Nested if statements determine the conditions of the inner if only when the condition of the outer if is set. For example, if today is a workday,

Go to work. If today is a weekend, go out to play. If the weather is fine on weekends, go to the outdoor playground; otherwise, go to the indoor playground.

Play.

Syntax:

Execution Process:

For example:

(4) switch of Java conditional statements

The switch statement is more concise and clear when you need to determine the equivalence of options. For example, the first four prizes are given based on the exam rankings.

The first prize is a laptop, the second prize is IPAD2, the third prize is a mobile power supply, and the last prize is a USB flash drive.

Syntax:

Execution Process: when the value of the expression after the switch is the same as the value after the case statement, execute downward from this position until a break statement or

The switch statement block ends. If no matching case statement exists, the code of the default block is executed.

For example:

Note:

1) The expression value in the parentheses after the switch must be of the integer or simplified type.

2) The value after case can be a constant value, such as 1 and 2, or a constant expression, such as 2 + 2, but not a variable or expression with a variable.

Type, such as a * 2.

3) after case matching, execute the program code in the matching block. If the break is not met, it will continue to execute the content of the next case block

The break statement or switc statement block ends. It is not recommended to use this method, for example:

Running result:

4) You can combine case statements with the same functions, for example:

5) The default block can appear at any position or be omitted.

 

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.