# Java learning notes - getting started - 003 operators and JShell scripting tools

Posted by waynem801 on Thu, 06 Feb 2020 10:20:18 +0100

## 1, Operators

### 1.1 definition

• Operator: a symbol that performs a specific operation, for example: +.
• Expression: the expression connected by operators is called expression. For example: 20 + 5.
• Operations can be performed between constants, between variables, and between constants and variables.

### 1.2 arithmetic operators

#### 1.2.1 classification

Arithmetic operator Operations that can be performed
+ Addition operation, string connection operation
- Subtraction operation
* Multiplication operation
/ Division operation (no matter whether the calculation result has remainder or not, the result only takes quotient)
% Modular operation (division of two numbers and remainder)
++,-- Self increasing and self reducing operation

Note: modular operation in Java can be used for decimals, but the calculated value is no longer the exact value, and this calculation has little significance.

• Once there are different types of data in the operation, the result will be the one with a wide range of data types.
```public class Demo04Operator {
public static void main(String[] args) {
// Mathematical operations can be performed between two constants
System.out.println(20 + 30);

// Mathematical operations can also be performed between two variables
int a = 20;
int b = 30;
System.out.println(a - b); // -10

// Variables and constants can be mixed
System.out.println(a * 10); // 200

int x = 10;
int y = 3;

int result1 = x / y;
System.out.println(result1); // 3

int result2 = x % y;
System.out.println(result2); // Remainder, modulus, 1

// int + double --> double + double --> double
double result3 = x + 2.5;
System.out.println(result3); // 12.5
}
}
```

#### 1.2.2 multiple uses of addition

② For character char type, char is promoted to int (refer to code table) before calculation, and then calculated.

③ For strings, the plus sign represents a string join operation (when any data type and string are joined, the result becomes a string).

```public class Demo05Plus {
public static void main(String[] args) {
// Basic use of variables of string type
// Data type variable name = data value;
String str1 = "Hello";
System.out.println(str1); // Hello

System.out.println("Hello" + "World"); // HelloWorld

String str2 = "Java";
// String + int --> String
System.out.println(str2 + 20); // Java20

// Priority issues
// String + int + int
// String		+ int
// String
System.out.println(str2 + 20 + 30); // Java2030

System.out.println(str2 + (20 + 30)); // Java50
}
}
```

#### 1.2.3 auto increase and auto decrease operator

• Basic meaning: let a variable add 1 or subtract 1.
• Use format: write before or after the variable name, for example, + + num, or num + +.
• Usage:
```Use alone: do not mix with any other operations, and become a step independently.
Mixed use: mixed with other operations, such as with assignment, or with printing operation.
```
• Difference in use:
```1. When used alone, there is no difference between pre + + and post + +. That is, + + num; and num + +; are exactly the same.
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]
```
```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
System.out.println("=================");

// 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
System.out.println("=================");

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
System.out.println("=================");

int num4 = 40;
// Mixed with assignment
int result1 = --num4; // Mixed use, before --, the variable immediately changes from - 1 to 39, and then gives the result 39 to the result1 variable
System.out.println(result1); // 39
System.out.println(num4); // 39
System.out.println("=================");

int num5 = 50;
// Mix, and then -- first, I give the original number 50 to result2, and then I change - 1 to 49
int result2 = num5--;
System.out.println(result2); // 50
System.out.println(num5); // 49
System.out.println("=================");

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 writing! Constants cannot use + + or--
}
}
```

Note: only variables can use autoincrement and autodecrement operators. Constants cannot be changed, so they cannot be used.

### 1.3 assignment operators

Assignment operators are divided into basic assignment operators and compound assignment operators

① Basic assignment operator: it is an equal sign "=" which means to give the data on the right side to the variable on the left side. For example:

```int a = 30;
```

② Compound assignment operator

compound assignment operators Meaning Example Amount to
+= Plus equal a += 3 a = a + 3
-= Reduction equals b -= 4 b = b - 4
*= Multiplication equals c *= 5 c = c * 5
/= Except equal to d /= 6 d = d / 6
%= Take mould, etc. e %= 7 e = e % 7
• Matters needing attention

① Only variables can use assignment operators, and constants cannot be assigned.

② A cast is implied in the compound assignment operator (whether or not the value calculated at the right end of the variable exceeds the range of the data type on the left, the cast will be performed).

```public class Demo07Operator {
public static void main(String[] args) {
int a = 10;
// Translate according to the formula: a = a + 5
// a = 10 + 5;
// a = 15;
// a used to be 10, now it's 15
a += 5;
System.out.println(a); // 15

int x = 10;
// x = x % 3;
// x = 10 % 3;
// x = 1;
// x used to be 10, and now it's 1
x %= 3;
System.out.println(x); // 1

// 50 = 30; / / constants cannot be assigned and written to the left of assignment operators. Wrong way to write!

byte num = 30;
// num = num + 5;
// num = byte + int
// num = int + int
// num = int
// num = (byte) int
num += 5;
System.out.println(num); // 35
}
}
```

### 1.4 comparison operators

Comparison operator Meaning
== Compare whether the data on both sides of the symbol is equal. The result of equality is true.
< Compare whether the data on the left side of the symbol is smaller than the data on the right side. If it is smaller than the result, it is true.
< Compare whether the data on the left side of the symbol is greater than the data on the right side. If it is greater than the result, it is true.
<= Compare whether the data on the left side of the symbol is less than or equal to the data on the right side. If it is less than, the result is true.
>= Compare whether the data on the left side of the symbol is greater than or equal to the data on the right side. If it is less than the result, it is true.
!= Not equal to symbol. If the data on both sides of the symbol is not equal, the result is true.
• Matters needing attention

① The result of the comparison operator must be a boolean value. If it is true, it is false.

② If you make multiple judgments, you can't write them together. Writing in Mathematics: 1 < x < 3; this writing is not allowed in the program.

```public class Demo08Operator {
public static void main(String[] args) {
System.out.println(10 > 5); // true
int num1 = 10;
int num2 = 12;
System.out.println(num1 < num2); // true
System.out.println(num2 >= 100); // false
System.out.println(num2 <= 100); // true
System.out.println(num2 <= 12); // true
System.out.println("===============");

System.out.println(10 == 10); // true
System.out.println(20 != 25); // true
System.out.println(20 != 20); // false

int x = 2;
// System. Out. Println (1 < x < 3); / / error! Compile error! It can't be written together.
}
}
```

### 1.5 logical operators

Logical operators Meaning
&&And (and) All true is true; otherwise, it is false
Short circuit feature: the left side of the symbol is false, and the right side is no longer calculated
||Or (or) At least one is true, that is true; all are false, that is false
Short circuit feature: true on the left side of the symbol, no operation on the right side
! not (reverse) Originally true, become false; originally false, become true
• 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

For 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

```public class Demo09Logic {
public static void main(String[] args) {
System.out.println(true && false); // false
// true && true --> true
System.out.println(3 < 4 && 10 > 5); // true
System.out.println("============");

System.out.println(true || false); // true
System.out.println(true || true); // true
System.out.println(false || false); // false
System.out.println("============");

System.out.println(true); // true
System.out.println(!true); // false
System.out.println("============");

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

int b = 20;
// true || ...
System.out.println(3 < 4 || ++b < 100); // true
System.out.println(b); // 20
}
}
```

### 1.6 ternary operator

#### 1.6.1 operator classification

① 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.

#### 1.6.2 definition of ternary operator

• format
```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 left data type (otherwise, an error is reported).
2. The result of a ternary operator must be used (for operation, assignment, or printing).
```
```public class Demo10Operator {
public static void main(String[] args) {
int a = 10;
int b = 20;

// Data type variable name = condition judgment? Expression A: expression B;
// Judge whether a > b is true, if it is true, assign the value of a to max; if not, assign the value of B to max. Choose one of them
int max = a > b ? a : b; // Maximum variable
System.out.println("Maximum:" + max); // 20

// Int result = 3 > 4? 2.5: 10; / / wrong writing!

System.out.println(a > b ? a : b); // Write correctly!

// A > b? A: B; / / wrong writing!
}
}
```

### 1.7 compiler optimization for operation

① 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 supplement us with a (byte)(short)(char).

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. (that is, compiler constant optimization)

```short result = 5 + 8;
// To the right of the equal sign are all constants. No variables participate in the operation and compilation
// The result of the. 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.
```

Note: once variables are involved in the expression, this optimization cannot be performed.

```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;
System.out.println(result);

//short result2 = 5 + a + 8; / / error writing
}
}
```

## 2, JShell scripting tool

When we write very little code, but are not willing to write classes, main methods, and are not willing to compile and run, we can use the JShell tool at this time.

• Start the JShell tool: directly enter the JShell command at the DOS command line (the exe file is in the bin directory of jdk9 and above)

• Exit JShell: Enter / exit, enter.
Published 0 original articles, praised 0, visited 43

Topics: less Java