First acquainted with the Java language-basic grammar 2

Getting to know the Java language 2-basic grammar 2

Similar to the C language, Java also has conditional operators, logical operators, bitwise operators, compound operators, selection structures, loop structures, and so on.
1. Conditional operator - >,<,<=,>=,!=,== The
usage is similar to C language, I will not explain too much, but it should be noted that the result of conditional operator is no longer in C language Non-zero is true and zero is false, but it is a boolean type, that is, true is true and false is false . So you can't think about some things in Java with C language thinking.
2. Logical operators - &&, ||,!
The usage is similar to the C language, and I won't explain too much, but it should also be noted that the expressions on both sides of the logical operators must also be Boolean expressions. That is, true is true and false is false . !true=false !false=true.
Third, the site operator - &, |, ~, ^
& and | ^ and we are all familiar, and C language exactly the same here say is "~" - Bitwise. That is, if the current bit is 1, it becomes 0, and if the current bit is 0, it becomes 1.
Fourth, the conformity operator - +=, -=, *=, /=, &=, |=, ^=
similar to the C language, but it should be noted that the conformity operator in the C language does not automatically coerce type conversion, that is It is said that the high-byte type operation result needs to be forced to be assigned to the low-byte data, but if the conforming operator is used in Java, then the forced type conversion will be performed automatically.

short a=10;
int b=20;
a+=b;//1
a=a+b;//2

The above 1 and 2 are equivalent in the C language.

short a=20;
int b=10;
a+=b;//1
a=(short)(a+b);//2

The above 1 and 2 are equivalent in Java.
Five, selection structure - if, switch, if-else, if-else if-...else
and C language are basically the same, but it should also be noted that the judgment expression in the selection structure must be of Boolean type, that is, true is true. false is false . In switch, floating-point types float and double; boolean type boolean; long integer type long can not be used as parameters.
Sixth, loop structure - for, while. No goto is
basically the same as the C language, but it should also be noted that the loop judgment condition is a Boolean type- true is true, false is false .

Guess you like

Origin blog.csdn.net/qq_45841205/article/details/110684228