Java expressions, statements, and blocks (translated from Java tutorials)

Source: Internet
Author: User

From http://www.cnblogs.com/ggjucheng/archive/2012/12/16/2820664.html

English from http://docs.oracle.com/javase/tutorial/java/nutsandbolts/index.html

After learning the variables and operators, start to learn the expressions, statements, and blocks. The operator is used to calculate the value, which is part of the expression. The expression is the main part of the statement, and the block is composed of statements.

Expression

The right variable, operator, and method call of the expression. It is constructed according to the syntax of the language and returns a separate value after calculation. An example of the expression is as follows:

 
IntCadence = 0;Anarray [0] = 100;System. Out. println ("Element 1 at index 0:" + anarray [0]);IntResult = 1 + 2; // Result is now 3If (Value1 = value2) System. Out. println ("Value1 = value2");

The type of the value returned by an expression is dependent on the elements used in the expression. Expression Cadence = 0 returns an int, because the value assignment operator returns a value of the same data type as its left operand. In this case,CadenceIs Int. As you can see in other expressions, expressions can return values of other types, such as boolean values or strings.

JavaProgramming Language, Allows you to construct a composite expression from multiple simple expressions, as long as a part of the expression requires a data type, composite other data types. Here is an example of a composite expression:

 
1*2*3

In this particular example, the order of expression calculation is not important because the result of multiplication is independent from the order. No matter what order is used to apply multiplication, the result is the same. However, this does not use all expressions. The result of the following expressions depends on the division and multiplication operations which are the first.

 
X + Y/100 // ambiguous

Use parentheses (and) to precisely control which expression is executed first. For example, to make the previous expression unambiguous, you can write it like this:

 
(X + y)/100 // unambiguous, recommended

If you do not explicitly indicate which operation is executed first, the execution sequence is determined by the Operation priority of the expression. A high-priority operation is executed first. For example, Division operations have a higher priority than addition operations. Therefore, the following two expressions are the same:

 
X + Y/100 x + (y/100) // unambiguous, recommended

Write a compound expression and use parentheses to explicitly specify which operation is executed first. This practice allowsCodeIt is easier to read and should be maintained.

 

Statement

A statement is roughly equivalent to a sentence in a natural language. The statement constitutes a complete execution unit. The following expression can be used to construct an idiom sentence. It can end with a semicolon (;) in the expression.

    • Value assignment expression
    • Any usage of ++ or --
    • Method call
    • Object creation expression

This is called an expression statement. Below are some example expression statements.

 
/Assignment statementavalue = 8933.234; // increment statementavalue ++; // Method Invocation statementsystem. Out. println ("Hello world! "); // Object creation statementbicycle mybike = new bicycle ();

In addition to expression statements, there are two types of statements: Statement declaration and control flow statements. A declaration statement declares a variable. You have seen many examples of declaration expressions:

 
// Declaration statementdouble avalue = 8933.234;

Finally, control flow statements adjust the statement execution sequence. The control flow statements will be learned later.

Block

A code block consists of 0 to multiple statements, which are placed in braces and can be used where any independent statement can be used. The following example,Blockdemo:

 class blockdemo {public static void main (string [] ARGs) {Boolean condition = true; If (condition) { // begin Block 1  system. out. println ("condition is true. ") ;}< strong> // end block one  else { // begin Block 2  system. out. println ("condition is false. ") ;}< strong> // End Block 2 }
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.

Tags Index: