JAVA must ensure the consistency of data types. If the data types are inconsistent, type conversion will occur.

Automatic type conversion (implicit):
1. Features: the code does not need special processing and is automatically completed
2. Rules: data range from small to large

float num = 30L;    //30.0

1. Features: the code needs special format processing and cannot be completed automatically.
2. Format: small range type small range variable name = (small range data) originally large range data;

int num = (int)100L;

matters needing attention:
1. Forced type conversion is generally not recommended because of the possibility of precision loss (this is not rounding, all decimal places will be discarded) and data overflow.
2. The three data types byte / short / char can generate mathematical operations, such as addition "+".
3.byte/short/char are three types of data that will be promoted to int first and then to be calculated.
4.boolean type cannot have data type conversion.

byte num1 = 40;
byte num2 = 50;
byte num3 = num1 + num2;//Error, byte + byte -- > int + int -- > int precision loss
int  num3 = num1 + num2;//Correct
short num4 = 60;
short num5 = (short)(num1 + num4);//byte + short -->int +int -->int
//int cast to short must ensure that the logical true size does not exceed the range of short, otherwise data overflow will occur.

There are three common uses of the plus sign "+" in the four operations:

1. For numerical value, that is addition.
2. For character char type, char will be promoted to int before calculation. Comparison table between char type characters and int type numbers: ASCII, Unicode
3. For String (initial uppercase, not keyword), plus represents String connection operation.
When any data type is connected to a string, the result becomes a string

System.out.println("Hello" + "World"); // HelloWorld
String str2 = "Java";
// String + int --> String
System.out.println(str2 + 20); // Java20
//When any data type is connected to a string, the result becomes a string		
// Priority issues
// String + int + int
// String		+ int
// String
System.out.println(str2 + 20 + 30); // Java2030	
System.out.println(str2 + (20 + 30)); // Java50

Autoincrement operator:++
Self subtraction operator: –

Basic meaning: let a variable increase a number by 1, or let a variable decrease a number by 1
Use format: write before or after the variable name. For example, + + num, or num++
1. Use alone: not mixed with any other operations, it becomes a step independently.
2. Mixed use: mixed with other operations, such as mixed with assignment, or mixed with print operation, etc.
Difference in use:
1. When used alone, there is no difference between pre + + and post + +. That is, + + num; and num + +; are exactly the same.

public class Demo06Operator {
	public static void main(String[] args) {
		int num1 = 10;
		System.out.println(num1); // 10
		++num1; // Use alone, front++
		System.out.println(num1); // 11
		num1++; // Used alone, after++
		System.out.println(num1); // 12

2. When mixing, there are [significant differences]
A. If it is pre + +, then the variable [immediately + 1], and then use it with the result. [add before use]
B. If it is [post + +], first use the original value of the variable, [and then let the variable + 1]. [use before add]

        // When mixing with print operations
		int num2 = 20;
		// Mixed use, first + +, the variable immediately becomes 21, and then print the result 21
		System.out.println(++num2); // 21
		System.out.println(num2); // 21
		int num3 = 30;
		// Mixed use, after + +, first use the variable original 30, then let the variable + 1 get 31
		System.out.println(num3++); // 30
		System.out.println(num3); // 31
		int x = 10;
		int y = 20;
		// 11 + 20 = 31
		int result3 = ++x + y--;
		System.out.println(result3); // 31
		System.out.println(x); // 11
		System.out.println(y); // 19
		 30++; // Wrong way to write! Constants cannot use + + or--

matters needing attention:
Only variables can use the increment and decrement operators. Constants cannot be changed, so they cannot be used.

Note for assignment operators:
1. Only variables can use assignment operators, and constants cannot be assigned.
2. A cast is implied in the compound assignment operator.

        50 = 30; // Constants cannot be assigned and written to the left of assignment operators. Wrong way to write!
		// A cast is implied in the compound assignment operator.
		byte num = 30;
		// num = num + 5;
		// num = byte + int
		// num = int + int
		// num = int
		// num = (byte) int
		num += 5;
		System.out.println(num); // 35

Comparison operator:
It is an operation to compare two data. The operation results are either Boolean values true or false.
matters needing attention:

  1. The result of the comparison operator must be a boolean value. If it is true, it is false
System.out.println(10 > 5); // true
  1. If you make multiple judgments, you can't write them together.
    Writing in mathematics, for example: 1 < x < 3
    This is not allowed in the program.

With "& &", or "|", it has a short-circuit effect: if the final result can be judged according to the left side, the code on the right side will not be executed, thus saving some performance.

        int a = 10;
		// false && ...
		System.out.println(3 > 4 && ++a < 100); // false
		System.out.println(a); // 10
		int b = 20;
		// true || ...
		System.out.println(3 < 4 || ++b < 100); // true
		System.out.println(b); // 20

matters needing attention:

  1. Logical operators can only be used with boolean values.
  2. And, or need to have a boolean value on the left and right respectively, but only a unique boolean value can be used for negation.
  3. And, or two operators, if there are multiple conditions, you can write continuously.
    Two conditions: condition a & & condition B
    Multiple conditions: condition a & & condition B & & condition C

In the case of 1 < x < 3, it should be split into two parts and then connected with the operator:
int x = 2;
1 < x && x < 3

Unary operator: an operator that can operate with only one data. For example: reverse!, auto increase + +, auto decrease –
Binary operator: an operator that requires two data to operate on. For example: addition +, assignment=
Ternary operator: an operator that requires three pieces of data to operate on.

Data type variable name = condition judgment? Expression A: expression B;

Technological process:
First, judge whether the conditions are valid:
If true, the value of expression A is assigned to the variable on the left;
If it is not set to false, the value of expression B is assigned to the variable on the left;
Choose one of them.

matters needing attention:

  1. Both expression A and expression B must meet the requirements of the left data type.
int result = 3 > 4 ? 2.5 : 10; // Wrong way to write!
  1. The result of the ternary operator must be used.
System.out.println(a > b ? a : b); // Write correctly!
a > b ? a : b; // Wrong way to write!

Define the format of a method:
public static void method name (){
Method body
The naming rules for method names are the same as for variables, using small humps.
Method body: that is, the braces can contain any statement.

matters needing attention:

  1. The order in which methods are defined does not matter.
  2. The definition of a method cannot produce a nested containment relationship.
  3. After the method is defined, it will not be executed. If you want to execute, you must call the method.

How to call a method, format:
Method name ();

public class Demo11Method {
	public static void main(String[] args) {
		farmer(); // Call farmer's method
		seller(); // Call the vendor's method
		cook(); // Call cook's method
		me(); // Call my own methods
	// Cook
	public static void cook() {
		System.out.println("Washing vegetables");
		System.out.println("cut up vegetables");
		System.out.println("Stir fry");
	// I
	public static void me() {
	// Peddler
	public static void seller() {
		System.out.println("Transport to the farmer's market");
		System.out.println("Raise the price");
		System.out.println("Cry out");
		System.out.println("Sell to cook");
	// Uncle farmer
	public static void farmer() {
		System.out.println("Apply fertilizer");
		System.out.println("Pest control");
		System.out.println("Sold to peddlers");

How to use jshell? Exit must be / exit

|  Welcome to use JShell -- Edition 9.0.4
|  To get a general idea of this version, Please type: /help intro

jshell> System.out.println("Hello,World!");

jshell> /exit
|  Bye


For the three types of byte/short/char, if the value assigned to the right side does not exceed the range,
Then the javac compiler will automatically and implicitly add a (byte)(short)(char) for us.

  1. If it does not exceed the range on the left, the compiler makes up the forced rotation.
  2. If the right side exceeds the left side, the direct compiler reports an error.
public class Demo12Notice {
	public static void main(String[] args) {
		// It is true that the right side is an int number, but it does not exceed the range on the left side, which is correct.
		// Int -- > byte, not automatic type conversion
		byte num1 = /*(byte)*/ 30; // The right side does not exceed the left side
		System.out.println(num1); // 30
		// byte num2 = 128; / / right side exceeds left side
		// Int -- > char, not out of range
		// The compiler will automatically fill in an implied (char)
		char zifu = /*(char)*/ 65;
		System.out.println(zifu); // A

When assigning values to variables, if all the expressions on the right are constants and there are no variables, the compiler javac will directly evaluate several constant expressions to get the results.

short result = 5 + 8; // To the right of the equal sign are all constants. No variables are involved in the operation

After compilation, the resulting. class bytecode file is equivalent to [directly]:

short result = 13;

The constant result value on the right side does not exceed the range on the left side, so it is correct.

This is called "compiler constant optimization.".

But note: once variables are involved in the expression, this optimization cannot be carried out.

public class Demo13Notice {
	public static void main(String[] args) {
		short num1 = 10; // The right side is not beyond the left side,
		short a = 5;
		short b = 8;
		// short + short --> int + int --> int
		// short result = a + b; / / wrong writing! int type expected on left
		// On the right side, we use constant instead of variable, and there are only two constants, no one else
		short result = 5 + 8;
		short result2 = 5 + a + 8; // Error, compiler constant optimization cannot occur
Little onion! Published 16 original articles, won praise 5, visited 224 Private letter follow