Java review (2) (calculation, escape sequence, evaluation rule, type conversion, instruction execution statement)
Part 1 "Calculation"
- Formula construction
(1) Number = operand (operand. The target of the operator)
② Operator
It consists of only these two.
- Literal
Among the operands, a specific value such as 5 or book is called a literal.
Part 2 "Escape sequence"
Special characters can be output to the screen by using .
①¥ ‘
②¥ “
③¥¥
You can also start a new line by putting it in the text.
¥n
Part 3 "Rules for evaluation"
- The calculation method is very ordinary
If 1 + 2 --6
The answer is -3
- Evaluation order
5 + 2 * 2 = 9
( 5 + 2 ) * 2 = 14
If A = B = 1, then
①A = B = 1
②A = 1
③1
As a result, A becomes 1.
Therefore, the following formula becomes as follows.
①A = 2 * 1 = 2 + 3 * 2
②A = 2 * 1 = 8
③A = 10
④10
- Strength of operator evaluation (simplified version)
high Low
(++ - -) > ( * / % ) > ( + - ) > ( = += -+ /= %=)
The assignment is done last, so it has the lowest priority.
- ++ is called increment. Minus is the decrement operator
- Notes on the increment operator
Do not use it with other operators as it will cause bugs.
Part 4 "About type conversion"
- Type rules
For example, the String type is not included in the int type.
However, type conversion may be done automatically under certain rules.
- Conversion by substitution
If it's a number, it has a size
For example, an int smaller than long goes into long
Floats smaller than double can be assigned to double.
- Java automatically converts the type at the time of assignment, and it is converted to the value of the assignment destination.
- Notes on assignment type conversion
Float and double that handle decimal points cannot be assigned to int and long that handle integers.
- Forced conversion by cast
int num = 3.2;
Such assignment is not possible, but it can be compiled by writing as follows.
int num = ( int ) 3.2;
When executed, 3 is assigned to num.
- Operation type conversion
5 / 2 = 2
5.0 / 2.0 = 2.5
The above is the calculation between ints and doubles.
5.0 / 2 = 2.5
In the above, int type 2 is converted to double type 2.0.
Then it becomes 5.0 / 2.0 and the result is 2.5.
- String type conversion
System.out.println (“Tomorrow is” + 5 + “Sun”;);
In the above case, 5 is converted to String type.
Part 5 "Instruction execution statement"
It is provided by Java or you can create it yourself (calling a method). For example, there are the following.
System.out.print();
The parentheses contain what are called arguments and parameters. (Parameter: A value input from the outside that affects the behavior of software and systems.
The following is a list of typical ones.
Math.max (A, B); Substitute the larger value
Integer.parseInt (); Converts a parameter to an int type.
new java.util.Scanner (System.in) .nextLine (); Accepts keyboard input