[Java | Basics] Super detailed explanation of operators

1. What is an operator

Operators are used to perform program code calculations that operate on more than one operand item.
Function: A symbol for operating on literals and variables

2. Arithmetic operators

Arithmetic operators are加减乘除取余

operator describe
+ add
- reduce
* take
/ Divide (round)
% Take the remainder

Example:

public class Demo3 {
    
    
    public static void main(String[] args) {
    
    
        int a = 5;
        int b = 2;
        System.out.println("a+b="+(a+b));
        System.out.println("a-b="+(a-b));
        System.out.println("a*b="+(a*b));
        System.out.println("a/b="+(a/b));
        System.out.println("a%b="+(a%b));
    }
}
/*
输出结果:
a+b=7
a-b=3
a*b=10
a/b=2
a%b=1
*/

Notice:

  • In the code, if there are decimals to operate, there may be problems with inaccurate precision (in a nutshell, the reason is that the computer stores data in binary, and decimals are not easy to represent. If necessary, you can watch this article ➡ in the computer data storage rules
  • When doing division and modulo, the right operand cannot be 0

implicit type conversion

There are two data type conversion modes in Java: one is implicit type conversion, and the other is mandatory type conversion.
First introduce implicit type conversion
. First look at the following code:

    public static void main(String[] args) {
    
    
        int a = 1;
        double b = 2.1;
        System.out.println(a + b);
    }

The result of the above code is 3.1 which is the implicit type conversion
数字在进行运算时,数据类型不一样是不能进行运算的,需要转成相同的数据类型才能够进行运算

隐形类型转换是 将取值范围小(精度小)的数据类型转成取值范围大(精度大)的数据类型

When performing operations, the data type with a small value range is automatically changed to a data with a large value range, and then the operation is performed

byte char short 类型的数据在进行运算时,都会类型提升为int类型的数值 再进行运算

cast

强制类型转换是 将取值范围大(精度大)的数据类型转成取值范围小(精度小)的数据类型
Grammar format: target data type variable name = (target data type) the data to be forced

Example:

    public static void main(String[] args) {
    
    
        double a = 12.3;
        // 目标数据类型 变量名 = (目标数据类型) 被强转的数据
        int b = (int) a;
    }

This is the cast
需要注意的是:在强制类型转换时,数据不能超过目标数据类型的取值范围

concatenation of strings

When talking about arithmetic operators, I used string concatenation.
For example:

    public static void main(String[] args) {
    
    
        int a = 5;
        int b = 2;
        System.out.println("a+b="+(a+b));
    }
    // 输出结果
    // a+b=7

当"+"操作中出现字符串时,那么此时这个"+"就不是算术运算符了,而是字符串的拼接,它会将字符串和数据拼接起来,变成一个新的字符串

character addition

Look at the following code:

    public static void main(String[] args) {
    
    
        char a = 'a';
        char b = 'b';
        int ret = a + b;
        System.out.println(ret);
    }
    // 输出结果: 195

As I said before byte char short 类型的数据在进行运算时,都会类型提升为int类型的数值 再进行运算
, the addition of characters and characters is converted into int type data and then added, then the numbers corresponding to the characters need to use the ASCII code table
If you want the ASCII value corresponding to the character, you can see ➡ ASCII code list, ASCII Code Comparison Table

在 字符与字符 或 字符与数字 进行相加时,字符会变为ASCII码表对应的数字在进行相加

Increment and Decrement Operators

symbol effect
++ Increment the value of the variable by 1
Decrements the value of a variable by 1

这里的自增自减分的符号 可以放在变量名的前面也可以放在后面

Example:

    public static void main(String[] args) {
    
    
        int a = 10;
        a++;
        System.out.println("a = "+ a);
        ++a;
        System.out.println("a = "+ a);
    }
    //输出结果
    //a = 11
	//a = 12

From here we can see that regardless of whether ++ is in front of or behind the variable, the value of the variable is +1
of course++在变量前面还是后面还是有区别的

Look at the code first:

    public static void main(String[] args) {
    
    
        int a = 10;
        int ret1 = ++a;
        System.out.println("ret1 = "+  ret1);
        int b = 10;
        int ret2 = b++;
        System.out.println("ret2 = "+ ret2);
    }
	// 输出结果:
	//ret1 = 11
	//ret2 = 10

This is the difference between ++ before or after the variable
在使用这种方式进行赋值时,++在前面时会变量的值会先自增1然后再进行赋值,如果++在后面则会先赋值然后再自增1

3. Assignment operator

The assignment operator is "=", which is to assign one data to another variable
. Among them, there are some compound assignment operators

operator example
+= b+=a is equivalent to b=b+a
-= b-=a is equivalent to b=ba
*= b*=a is equivalent to b=b*a
/= b/=a is equivalent to b=b/a
%= b%=a is equivalent to b=b%a

4. Relational operators

Relational operators compare the size of the data on both sides

operator describe
== Determine whether the content is equal
!= Determine whether the content is not equal
> Determine whether the value on the left of the greater than sign is greater than the value on the right
< Determine whether the value on the left of the less than sign is less than the value on the right
>= Determine whether the value on the left of the greater than sign is greater than or equal to the right
<= Determine whether the value on the left of the less than sign is less than or equal to the right

关系运算符的结果都是boolean类型的 结果要么为true 要么为false
注意: 不要将 "==" 写成 "="了

5. Logical operators

symbol significance effect
& logical AND and both sides are true, it is true
I logical or Or if both sides are false, it is false
^ XOR Same as false, different as true
! logical NOT reconciliation

Short-circuit with (&&) and short-circuit or (||)

  1. 对于 && , 如果左侧表达式值为 false, 则表达式结果一定是 false, 无需计算右侧表达式.
  2. 对于 ||, 如果左侧表达式值为 true, 则表达式结果一定是 true, 无需计算右侧表达式.
  3. The expressions on both sides of & and | must be calculated, using short-circuit and short-circuit or can improve efficiency

6. Ternary operator

语法格式: 关系表达式? 表达式1:表达式2

Example:

    public static void main(String[] args) {
    
    
        int a = 10;
        int b = 20;
        int c = a > b ? a : b;
        System.out.println(c);
    }
    // 输出结果:20

This is the use of the ternary operator to find the maximum of two values.
The calculation here is divided into three steps:

  1. First calculate the value of the expression
  2. If expression is true, take the value of expression1, if false, take the value of expression2
  3. and then assign

7. Bitwise operators

There are four main bitwise operators:& | ~ ^ ,除 ~ 是一元运算符外,其余都是二元运算符

Bitwise operators operate on a binary basis

  1. 按位与 &: 如果两个二进制位都是 1, 则结果为 1, 否则结果为 0.
  2. 按位或 |: 如果两个二进制位都是 0, 则结果为 0, 否则结果为 1.
  3. 按位取反 ~: 如果该位为 0 则转为 1, 如果该位为 1 则转为 0
  4. 按位异或 ^: 如果两个数字的二进制位相同, 则结果为 0, 相异则结果为 1.

8. Shift operation

Shift operations are also performed on a binary basis

  1. Left shift <<: The leftmost bit is not needed, and the rightmost bit is filled with 0.
  2. Shift right>>: The rightmost bit is unnecessary, and the leftmost complement sign bit (positive numbers complement 0, negative numbers complement 1)
  3. Unsigned right shift >>>: The rightmost bit is not needed, and the leftmost bit is filled with 0.

The first bit in binary is the sign bit, 0 for positive numbers and 1 for negative numbers
For details, you can take a look at: ➡Comprehensively analyze the storage of data in memory

    public static void main(String[] args) {
    
    
        int a = 10;
        System.out.println("a << 1 = "+(a<<1));
        System.out.println("a >> 1 = "+(a>>1));
        System.out.println("a >>> 1 = "+(a>>>1));
    }
    // 输出结果:
    //a << 1 = 20
	//a >> 1 = 5
	//a >>> 1 = 5
  • 左移 1 位, 相当于原数字 * 2. 左移 N 位, 相当于原数字 * 2 的N次方.
  • 右移 1 位, 相当于原数字 / 2. 右移 N 位, 相当于原数字 / 2 的N次方.

Thank you for watching! I hope this article can help you!
The Java column is constantly being updated, welcome to subscribe!
"Wish to encourage you and work together!"
insert image description here

Guess you like

Origin blog.csdn.net/m0_63463510/article/details/129198836