[Java operator]


1. Operators and expressions

operator

Symbols that operate on literals or variables

expression

Use operators to connect literals or variables, and the formula that conforms to the Java syntax can be called an expression

2. Arithmetic operators

1. Addition, subtraction, multiplication and division, modulo, remainder

insert image description here
Code demo:

public class test1 {
    
    
    public static void main(String[] args) {
    
     
        // +
        System.out.println(3+2);//5
        // -
        System.out.println(5-1);//4
        // *
        System.out.println(7*9); //63
        // 除法
        System.out.println(10 / 2);//5
        System.out.println(10 / 3);//3
        System.out.println(10.0 / 3);//3.333333333335
        // 取模
	    System.out.println(10 % 2);//0
        System.out.println(10 % 3);//1
    }
}

If decimals are involved in the calculation, the result may be inaccurate.
Integers are involved in the calculation, and the result can only be integers.
Decimals are involved in the calculation, and the result may be inaccurate

practise:
insert image description here

2. Implicit conversions and forced conversions

Addition of numbers
When numbers are used for calculation, the data types cannot be calculated if they are not the same, and they need to be converted into the same one before the calculation can be performed.

  1. Implicit conversion: small value range - large value range
  2. Mandatory conversion: large value range - small value range

Implicit conversion rules:

  • The value range is small, and the value range is large, and the small value will be upgraded to a large value, and then the operation is performed
  • The three types of data, byte short char, will be directly promoted to int before operation.
  • Value range: byte < short < int < long < float < double

coercion rules

  • If you want to assign a value with a large value range to a variable with a small value range, you need to add mandatory conversion
  • Format: target data type variable name = (target data type) data to be converted
  • results may be wrong

Addition of strings
When a string appears in the "+" operation, + is a string connector, not an arithmetic operator. It will splice the data before and after and generate a new string

  • “123” + 123 —— “123123’
  • 1+99+ "string" - "100 string"
    insert image description here
    characters added
  • When [character + character/character + number], the character will be queried through the ASCII code table to find the corresponding number and then calculated
    insert image description here

3. Self-increment and self-decrement operators

  • auto increment operator
  • The self-decrement operator
    insert image description herecan be written before or after the variable. On
    a single line, the result is the same.
    If it is involved in the calculation, the result is different.
    insert image description here

Fourth, the assignment operator

insert image description here

Five, relational operators

Relational operators are also called comparison operators.
insert image description hereThe results of relational operators are boolean types, either true or false

6. Logical operators

insert image description here& and |
insert image description here

short circuit logical operator

When the expression on the left can determine the final result, the right side will not participate in the operation
insert image description here

  • && The left side is false, and whether the right side is true or false, the result of the entire expression must be false
  • || The left side is true, and whether the right side is true or false, the result of the entire expression must be true

Seven, ternary operator and operator precedence

ternary operator

Ternary operator, also called ternary expression
format: relational expression? Expression 1: Expression 2;
Calculation rules:

  • First evaluate the relational expression
  • If true, the value of expression1 is the result of the operation
  • If false, the value of expression 2 is the result of the operation
    insert image description here

operator precedence

insert image description here

Guess you like

Origin blog.csdn.net/qq_64451048/article/details/127592788