catalogue
- 1. Operator
- 1.1 arithmetic operators
- 1.2 character and string + operation
- 1.2. 1 character + operation
- 1.2. 2 string + operation
- 1.3 assignment operation
- 1.4 self increasing and self decreasing
- 1.5 relational operators
- 1.5 logical operators
- 1.6 ternary operators
- 2. Type conversion
- 2.1 automatic type conversion
- 2.2 cast type
1. Operator
1.1 arithmetic operators
Like the operators learned in primary school, these symbols are not big. At the beginning, those who have attended school can basically master them. The contents are as follows:
Symbol | effect |
---|---|
+ | plus |
- | reduce |
* | Multiplication is different from the previous symbols, but its function is the same |
/ | except |
% | Surplus |
In java programs, the difference is division. Integer division is integer, and mixed division takes the maximum type. Example:
public static void main(String[] args) { int a = 5; int b = 2; double x = 2.0; // The result is 2 System.out.println(a / b); // The result is 2.5 System.out.println(a / x); }
1.2 character and string + operation
1.2. 1 character + operation
The basic type of char is involved in arithmetic operation, and the bottom layer of decimal system is used to run arithmetic. Here, you must wonder how char represents a single character, not a character, and how it participates in the operation. Then you need to understand the ASCALL code, which corresponds each character to a numerical value, so as to realize the arithmetic operation. Common character decimal numbers:
character | Decimal number |
---|---|
0 | 48 |
A | 65 |
a | 97 |
Here you will encounter the problem of automatic promotion of types, which will be explained in detail later. Examples are as follows:
public static void main(String[] args) { char x = 'A'; char y = 'a'; // 162 is displayed System.out.println(x + y); }
1.2. 2 string + operation
When a string appears in the expression, + represents a string connector, otherwise it is an arithmetic operator. It is calculated from left to right. If it can be calculated, it will be calculated. Examples are as follows:
public static void main(String[] args) { // The result is 3LessAnn System.out.println(1 + 2 + "LessAnn"); // The result is 1LessAnn2 System.out.println(1 + "LessAnn" + 2); }
1.3 assignment operation
The assignment operator uses =, which is different from the = in our life. In life, the calculation result will be returned to = right. In the program, the calculation result of = right will be assigned to = left. Examples are as follows:
public class AssignmentTest { public static void main(String[] args) { // Declare variable int a; // Assign values to variables a = 1 + 2; //The output is 3 System.out.println(a); } }
The above is a simple assignment statement format, and there are some simplified assignment formats in the program, as follows (a=1):
Symbol | explain | Example |
---|---|---|
+= | Add self and number and assign value to self | a+=1, the result is 2 |
-= | Assign a value to yourself after subtracting itself from the number | a-=1, the result is 1 |
*= | It is assigned to itself after multiplying itself by a number | a*=5, the result is 5 |
/= | Divide yourself by a number and assign it to yourself | a/=2, the result is 2 |
%= | Assign value to yourself after the remainder of self and data | a%=5, the result is 2 |
1.4 self increasing and self decreasing
A simplified variable operation (a=1)
Symbol | explain | Example |
---|---|---|
++ | Variable self increment 1 | a + +, the result is 2 |
– | Variable self subtraction 1 | a –, the result is 0 |
It should be noted that this symbol can be placed on the left and right sides of the variable. It makes no difference to the variable itself. It adds or subtracts the variable by 1, but it is different from the whole expression. The symbol on the right indicates that variables are used before self addition and subtraction, and on the left indicates self addition and subtraction before use. Examples are as follows:
public class AutoincreateTest { public static void main(String[] args) { int a = 1, b = 5; // The result is 4 System.out.println(++a + a++); // The result is 8 System.out.println(b-- + --b); // The result is 3 System.out.println(a); // The result is 3 System.out.println(b); } }
1.5 relational operators
Primary school knowledge, but with different symbols, the symbols are as follows:
Symbol | explain |
---|---|
\== | be equal to |
!= | Not equal to |
\> | greater than |
\>= | Greater than or equal to |
< | less than |
<= | Less than or equal to |
All comparison results are boolean values (true, false). Note that = = is not =, = is the assignment symbol.
1.5 logical operators
Symbol | effect | explain |
---|---|---|
& | Logic and | A & b, a and b are true, and the result is true, otherwise it is false |
| | Logical or | a|b, a and B are false, and the result is false, otherwise it is true |
^ | Logical XOR | a^b, the results of a and b are different and true, and the same is false |
! | Logical non | ! a. The result is opposite to that of A |
&& | Short circuit and | A & & b, if a is true and b is true, the result is true. If a is false, b will not execute and directly return false |
|| | Short circuit or | A | b, if a is false and b is false, the result is false. If a is true, b will not execute and directly return true |
Note here that short circuit and or are special logical operators. Examples are as follows:
public class LogicTest { public static void main(String[] args) { int a = 1; int b = 2; // false System.out.println(a++ > 5 && b++ > 1); // 2 System.out.println(a); // 2 System.out.println(b); } }
1.6 ternary operators
Ternary operator syntax:
Relational expression? Expression 1: expression 2;
The relational expression returns a boolean value. If true, execute expression 1 and if false, execute expression 2.
Example:
public class TernaryTest { public static void main(String[] args) { // B System.out.println(1 > 2 ? 'A' : 'B'); // establish System.out.println(1 < 2 ? "establish" : "Not established"); } }
2. Type conversion
There must be type mixing operation in the program. At this time, the problem of output type occurs, which will be explained by classification below.
2.1 automatic type conversion
In the program, the small type can be directly assigned to the large type, for example:
public class TypeConversion { public static void main(String[] args) { byte a = 1; short b = 2; int y = a; System.out.println(y); System.out.println(a + b); } }
It should be noted here that byte, short and char are automatically converted to int types during operation, and these types are rarely used in programs.
2.2 cast type
Cast is to convert a large type to a small type.
Syntax: type 1 variable 1 = (type 1) variable 2;
Examples are as follows:
public class TypeTest { public static void main(String[] args) { int x = 1; // In this way, direct assignment will make an error // byte y = x; // The conversion type needs to be added before byte y = (byte) x; } }
At the end of this chapter, it is used for personal learning and introduction to Xiaobai. Don't spray it!
Source code [GitHub] [code cloud]