Java's basic syntax: basic data type conversion, operators and loop nesting

1.Conversion of basic data types

     First of all, you need to clarify what the basic data types are, how many bytes they occupy, and the conversion order between them.

1.1 The basic data types are:

  1. Integer type: byte (1 byte); short (2 bytes); int (4 bytes); long (8 bytes).
  2. Floating point type: float (4 bytes); double (8 bytes).
  3. Character type: char (2 bytes).
  4. Boolean type: boolean cannot be converted to other types (because Boolean type only has true and false).

 1.2 Conversion rules for basic data types:

        Small capacity to large capacity can be converted directly. e.g.:

byte a = 6;  
int  b = a;

        It should be noted that: float is 4 bytes, but because the binary storage structure of decimals is different from the binary storage structure of integers, a
                                  4-byte float is larger than a 4-byte int and larger than an 8-byte long.

The conversion sequence between basic data types is:

 byte->short->char->int->long->float->double       

2.Operator

2.1 Arithmetic operators:

+ : 1. Addition operation value + value (char can participate in the operation)
2. String connection value + string string + string == String
- : value - value (char can participate in the operation)
* : value * value ( char can participate in operations)
/: numerical value/numeric value (char can participate in operations)
%: take the remainder numerical value/numeric value (char can participate in operations)
++: increase by 1. Note that in mixed operations, ++ is incremented before ++ After, the latter increases automatically
-: Decreases by 1, - Before the front, increases automatically - After, the latter increases automatically

2.2 Relational (comparison) operators:

>, <, >=, <=,,! =.
Character types: >, <, >=, <=,, != are all used for size comparison between numerical values ​​(char)
== != can also be used for comparison between reference types

2.3 Assignment operator:

=, +=, -=, *=, /=, %= (that is, assigning the value on the right side of the assignment operator to the left side)

Note: When assigning, pay attention to the value type matching the declared data type. At the same time, the assignment operator can perform data type conversion implicitly, so pay attention to the variable type.

2.4 Logical operators:

The left and right sides of the symbol are logical values

  •         & -- logical AND                   

Note : All expressions return true, and the result is true. If one of them returns false, the result is false. When the result of the first logical AND expression is false, the following expressions will still be executed.                                      

  •         &&-- logical AND (short-circuit AND)   

 Note : All expressions return true, and the result is true. If one of them is false, the result is false. && If the first expression is false, subsequent expressions will not be executed.

  •         | -- Logical OR                   

Note : Among all expressions, as long as one is true, the result is true; if all are false, the result is false. If the first expression is true, subsequent expressions will continue to execute.

  •         || -- Logical OR (short-circuit OR)     

 Note : Among all expressions, as long as one is true, the result is true; if all are false, the result is false. If the first expression is true, subsequent expressions will not continue to execute.

  •         !---Logical NOT                       

eg:System.out.println(!(a>b));//!true --> false

  •         ^ -- Logical XOR                 

Note : The same is false, the difference is true

2.5 Conditional operator (ternary operator/ternary operator)

(Conditional expression)?Expression1:Expression2

            First execute the conditional expression.
            When the conditional expression value is true, run expression 1.
            When the conditional expression value is false, run expression 2.

            Ultimately expression1 and expression2 must return a result

            eg:

if(num>60){
     System.out.println("成绩合格");
}else{
     System.out.println("成绩不合格");
}
                      

2.6-bit operations:

It refers to the operation on the binary bits of the value
        << left shift
        >> right shift
        >>> unsigned right shift
        & | ^ If the left and right sides are logical values, it is a logical operation; if the left and right sides are not logical values, It’s just bit operations
        ~

3. Loop statement

3.1 if statement

The basic structure of if statement:

if(条件){
      语句;
}else{
      语句;
}
The conditional expression must be an expression that returns a Boolean result. Statements can be blocks of statements enclosed by {}.
if statement operation diagram:

 Nested structure of if statement:

if (条件1){
   语句1;
} else if (条件2){
   语句2;
}……
}else if (条件N){
   语句N;
}

3.2 switch statement

switch statement: a multi-branch selection statement that performs one of multiple operations based on the value of an expression.
The general structure of a switch statement is:
switch (表达式){
case value1: {
语句1;
break;//结束本次循环
}
…………
case valueN: 
{
语句N;
break;
}
default : {
缺省语句;
}
}

Notice:

  • Expressions can be byte, short, int, char, or enumeration types. After JDK 7, the String type can be used;
  • Only constant expressions can be used after the case statement;
  • The value after case cannot be repeated;
  • If a case branch does not provide a break statement, the statement after the next case will continue to be executed;
  • When the value of the expression does not match the value in any case clause, the statement following the default will be executed; if there is no default clause, the program will directly jump out of the switch statement.

3.3while & do while statement

The while statement is of the following form:
while(逻辑表达式)
{ 
语句;
 … ; 
}
Implementation process:
First determine the value of the logical expression. If =true. then execute the following statement, then judge the condition again and execute it repeatedly until the condition is not established.
Schematic diagram of the operation process:

The do  while statement is of the following form:

do 
{ 
语句;
 … ; 
} while(逻辑表达式);
Implementation process:
The statement is executed first, and then the value of the logical expression is judged. If it is true, the statement is executed again, otherwise the loop ends.
Schematic diagram of the operation process:

 
3.3 for loop statement
The for statement is of the following form:
for(表达式1; 表达式2; 表达式3){ 
   语句; 
    … ; 
}

Implementation process:

First, expression 1 is calculated, and then expression 2 is executed. If the value of expression 2 = true, the loop statement is executed, then expression 3 is calculated, and then the value of expression 2 is judged; this repeats until the value of expression 2 value=false.
eg:
public static void main(String args[]) {
for (int i = 1; i <= 10; i++) {
System.out.println(“i=" + i);//遍历输出i
}
}
3.4 break & continue statement
The break statement is used to terminate the execution of a block of statements. Used in the body of a loop statement to forcefully exit the loop;
The continue statement is used in the loop statement body to terminate a certain loop process, skip the unexecuted loop under the continue statement in the loop body, and start the next loop process;

4. Loop nesting

  • Nested loops are formed by placing a loop within the body of another loop. Among them, for, while, do...while can be used as outer loops and inner loops.
  •  In essence, nested loops treat the inner loop as the loop body of the outer loop. When only the loop condition of the inner loop is false, the inner loop will be completely jumped out, and the current outer loop can be ended and the next loop can be started.
  • End outer loop
  • If you need to break out of a multi-layer loop, you need to use a label, define a label, such as label, and then use break label where you need to jump out.

 5.return statement

      The return statement is used to exit from the currently executing method and return to the statement that called the method to continue execution.
      There are two formats:
  •        return expression; //Return the value of the expression
  •        return; //Does not return any value. When void is used to declare no return value in the method description, this format can be used. This type of return statement can sometimes be omitted.

 

Guess you like

Origin blog.csdn.net/weixin_69778508/article/details/129929844