[Learn the basics of JAVA operators]

IDEA project structure introduction

Create in order
Insert image description here

arithmetic operators

Insert image description here

package com.itheima.arithmeticoperator;

public class ArithmeticoperatorDemo1 {
    
    
    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(1.1 + 1.01);
        System.out.println(1.1 - 1.01);
        System.out.println(1.1 * 1.01);
    }
}

operation result
Insert image description here

  • The results of decimal operations in the code may be inaccurate. Accurate results require more operations
  • The result of integers participating in the calculation can only be integers.
package com.itheima.arithmeticoperator;

public class ArithmeticoperatorDemo2 {
    
    
    //主入口
    public static void main(String[] args) {
    
    
        //除法
        System.out.println(10/2);//5
        System.out.println(10/3);//3
        System.out.println(10.0/3);//3.3333333333333335

        //取模,取余。实际是做除法运算得到余数
        System.out.println(10%2);//0
        System.out.println(10%3);//1

    }
}

Running results:
Insert image description here
Modulo and remainder application scenarios

  • Can be used to determine whether A is divisible by B
    A%B
  • You can determine whether A is an even number
    A%2. If the result is 0, it is an even number, and if it is 1, it is a base number. Exercise:
    Enter
    a three-digit number on the keyboard and split it into units, tens and hundreds. When performing operations on numbers
    Insert image description here
    based on the formula , the data type is different.
    Insert image description here
    are the same, they need to be converted into the same values ​​before they can be calculated.

implicit conversion

(Automatic type promotion)
is to assign a data or variable with a small value range to another variable with a large value range. There is no need for us to write additional

practice results at this time
Insert image description here

cast

If you want to assign a data or variable with a large value range to another variable with a small value range. Direct operation is not allowed. When you must do this, you need to add a cast. (You need to write the code yourself)
Format:

​Target data type variable name = (target data type) the data being forced;
forced conversion may cause data errors
Insert image description here

String and string addition operations

  • When a string appears in the + operation, it is the concatenator of the string, which will splice the preceding and following data and generate a new string.
  • When + operations are performed continuously, they are executed one by one from left to right.
    Insert image description here
    Example
    Insert image description here
    applications Insert image description here
    make the results easier to understand
    Insert image description here

Character + operation

When a character appears in the + operation, the character will be taken to the computer's built-in ASCII code table to look up the corresponding number, and then the calculation will be performed.
Insert image description here

increment and decrement operators

++: Just add 1 to the value in the variable

— —: It means to change the value in the variable to -1
, whether it is ++ first or ++ last. When written on a single line, the operation results are exactly the same .
Insert image description here

assignment operator

Insert image description here

Relational operators (comparison operators)

Insert image description here

  • The final result of a relational operator must be of Boolean type. Either true or false
  • When writing ==, never write =

Logical Operators

Insert image description here

// &  //两边都是真,结果才是真。
System.out.println(true & true);//true
System.out.println(false & false);//false
System.out.println(true & false);//false
System.out.println(false & true);//false

System.out.println("===================================");

// | 或  //两边都是假,结果才是假,如果有一个为真,那么结果就是真。
System.out.println(true | true);//true
System.out.println(false | false);//false
System.out.println(true | false);//true
System.out.println(false | true);//true

short circuit logical operator

Insert image description here

Logical core:

​ When the result of the entire expression cannot be determined on the left side, the right side will be executed.

​ When the result of the entire expression can be determined on the left side, the right side will not be executed. This improves the efficiency of the code.

ternary operator

Format:

​ Relational expression? Expression1:Expression2;

Calculation Rules:

  • Evaluates a relational expression.
  • If the relational expression evaluates to true, then expression 1 is executed.
  • If the relational expression evaluates to false, then expression 2 is executed.
  • The final result of the ternary operator must be used, either assigned to a variable or printed directly.

operator precedence

Insert image description here
() has the highest priority. If you want to calculate it first, just add ().

Guess you like

Origin blog.csdn.net/Luohuasheng_/article/details/131636790