If there is no missing precision, the compiler automatically performs implicit conversions.Int I = 3; double d; d = I; // correct, no type conversion required, d = 3.0d = (double) I; // You can also use the display type to convert double aDouble = 55; // The Compiler automatically converts int 55 to double 55.0 double nought = 0; // The Compiler automatically converts int 0 to double 0.0 // It is worth noting that int 0 and double 0.0 are different.
The following figure shows the sequence of implicit type conversion in the compiler. The conversion rule is to promote small types to large types, so as to prevent missing precision. Type conversion is required for downgrading. The precision is missing. It is worth noting that char is regarded as a 16-bit unsigned integer with a value range of [0, 65535]. conversion is not supported for the boolean type.
For example, calculate the average value from 1 to 100, and carefully study the following code
Public class Sum1To100 {public static void main (String [] args) {int sum = 0; double average; int number = 1; while (number <= 100) {sum + = number; // The final result of sum is int 5050 + + number;} average = sum/100; // average = 50.0 instead of 50.5 System. out. println ("Average is" + average); // The Average value is 50.0 }}
This is because both sum and 100 are of the int type. In addition, the return value is the truncated int. To get the correct result, you can use the following method:
Average = (double) sum/100; // convert sum to double type average = sum/(double) 100 displayed before division; // before division, convert 100 to double average = sum/100.0; average = (double) (sum/100); // this operation is incorrect, do you know why?
6. Compound value assignmentIn addition to the commonly used value assignment = described above, Java also provides other compound value assignment operations:
| Operator |
Explanation |
Use |
Example |
| = |
Assignment Assign the right operand to the left operand |
Var = expr |
X = 5; |
| + = |
Compound addition operation |
Var + = expr Equivalent to var = var + expr |
X + = 5; Equivalent to x = x + 5 |
| -= |
Compound Subtraction |
Var-= expr Equivalent to var = var-expr |
X-= 5; Equivalent to x = x-5 |
| * = |
Compound Multiplication |
Var * = expr Equivalent to var = var * expr |
X * = 5; Equivalent to x = x * 5 |
| /= |
Compound Division |
Var/= expr Equivalent to var = var/expr |
X/= 5; Equivalent to x = x/5 |
| % = |
Compound remainder operation |
Var % = expr Equivalent to var = var % expr |
X % = 5; Equivalent to x = x % 5 |
7. Auto-increment/auto-IncrementFor unary operations such as auto-increment (++) and auto-increment (--), it is applicable to other basic types of bytes, short, char, int, long, float, and double except boolean.
| Operator |
Explanation |
Example |
| ++ |
Add 1 to the original value X ++ or ++ x is equivalent to x + = 1 or x = x + 1. |
Int x = 5; X ++; ++ X; |
| -- |
The original value minus 1 X -- or -- x is equivalent to x-= 1 or x = x-1. |
Int y = 6; Y --; -- Y; |
Auto-increment and auto-increment are based on their own operations. For example, x ++ auto-increment is returned to x again.
The auto-increment/auto-increment operators can be placed before or after the operands, but they have different meanings.
If these operators are based on their own operations, the front and back of the operators have the same effect, for example, ++ x and x ++, because the values of the expressions are ignored.
If it is used for other operations, such as y = x ++ or y = ++ x, there are different values before and after the operator for the y value.
| Operator |
Explanation |
Example |
| ++ Var |
Auto-Increment First, add var and 1. The calculation result is var. |
Y = ++ x; Equivalent to x = x + 1; y = x; |
| Var ++ |
Auto-Increment First, use var for the calculation result, and then add 1 to var. |
Y = x ++; It is equivalent to oldX = x; x = x + 1; y = oldX; |
| -- Var |
Auto-Subtraction |
Y = -- x; Equivalent to x = X-1; y = x; |
| Var -- |
Auto-Subtraction |
Y = x --; Equivalent to oldX = x; x = X-1; y = oldX; |
8. Relational and logical operatorsIn many cases, you need to compare the two values before performing some operations. For example, if the mark value is greater than or equal to 50, the output "PASS! ".
Java provides six comparison operators. After comparison, the return value is true or false.
| Operator |
Explanation |
Use |
Example (x = 5, y = 8) |
| = |
Equal |
Expr1 = expr2 |
(X = y) → false |
| ! = |
Not equal |
Expr1! = Expr2 |
(X! = Y) → true |
| > |
Greater |
Expr1> expr2 |
(X> y) → false |
| > = |
Greater than or equal |
Expr1> = expr2 |
(X> = 5) → true |
| < |
Less |
Expr1 |
(Y <8) → false |
| <= |
Less than or equal |
Expr1> = expr2 |
(Y <= 8) → true |
Each comparison operator requires two operands. The correct syntax is x> 1 & x <100, and the incorrect syntax is 1 <x <100, where & indicates and operates.
Java provides four logic operations based on boolean. The priority order is as follows:
| Operator |
Explanation |
Use |
| ! |
Non-logical |
! BooleanExpr |
| ^ |
Logic exclusive or |
BooleanExpr1 ^ booleanExpr2 |
| && |
Logic and |
BooleanExpr1 & booleanExpr2 |
| | |
Logic or |
BooleanExpr1 | booleanExpr2 |
The truth table is as follows:
| And (&&) |
True |
False |
| True |
True |
False |
| False |
False |
False |
| |
|
|
| Or (|) |
True |
False |
| True |
True |
True |
| False |
True |
False |
| |
|
|
| Non (!) |
True |
False |
| Result |
False |
True |
| |
|
|
| XOR (^) |
True |
False |
| True |
False |
True |
| False |
True |
False |
Example:
// If the value range of x is [0,100], true (x> = 0) & (x <= 100) is returned. // if the value range of x is not [0,100], returns true (x <0) | (x> 100 )! (X> = 0) & (x <= 100) // determines whether the calculation is a leap year. A year is 4, but cannot be divisible by 100, or be divisible by 400 (year % 4 = 0) & (year % 100! = 0) | (year % 400 = 0)
Exercise: Study the following program and explain the output.
public class RelationalLogicalOpTest { public static void main(String[] args) { int age = 18; double weight = 71.23; int height = 191; boolean married = false; boolean attached = false; char gender = 'm'; System.out.println(!married && !attached && (gender == 'm')); System.out.println(married && (gender == 'f')); System.out.println((height >= 180) && (weight >= 65) && (weight <= 80)); System.out.println((height >= 180) || (weight >= 90)); }}
Exercise:Based on the provided Date: year, month (1-12) and Day (1-31), calculate whether the date is earlier than January 1, October 15, 1582.
Operator priorityPriority from high to low :'! ',' ^ ',' & ',' | '. If you are not sure about programming, use parentheses ().
System. out. println (true | true & false); // true (same as the following) System. out. println (true | (true & false); // trueSystem. out. println (true | true) & false); // falseSystem. out. println (false & true ^ true); // false (same as the following) System. out. println (false & (true ^ true); // falseSystem. out. println (false & true) ^ true); // true
Short-circuit OperatorLogic and (&) and logic or (|) are called short-circuit operators. This means that if the calculation result can be determined by the left operand, the right operand is ignored, for example, false &&... returns false, true |... returns true.