Java learning summary from a rookie-Java operators

1. Arithmetic operators

①Operation of character "+"

When there are multiple values ​​of different basic data types in an arithmetic expression, the type of the entire arithmetic expression will be automatically promoted

Code test description:

public class Demo {
    
    
    public static void main(String[] args){
    
    
        int i = 10;
        char a = 'a';
        int ans = i + a;
        System.out.println("int  " + ans);
        char o = '0';
        double d = i + 3.14159;
        int j = i + o;
        System.out.println("int  " + j);
        System.out.println("double  " + d);
    }
}

Test Results:

int  107
int  58
double  13.14159

The promotion rule is

a. byte type, short type and char type directly converted to type int

b. the whole expression is automatically promoted to the same number of the highest level of expression in the type of operation,

Level order: byte, short, char → int → long → float → double

② "+" operation of string

a. When the right and left "+" operation is a string, the result is the string concatenation operator

b. When the "+" operator in about digital, the result is an addition to digital (I do not know why write such a sentence)

2. Assignment operator

    x=y;
    一个简单的赋值语句
    "="执行顺序是先执行"="的右边的语句再执行赋值功能(在程序运行阶段)
    其中在执行"="右边的语句时,右边的语句按照从左往右的顺序执行语句
    
    而对于"="两边的内容也有不同的表述和含义
    其中"="左边的被称作左值,右边的被称作为右值
    "="右边的语句通过执行产生一个“值”,再执行"="符号赋值给左边的变量
    其中我们可以确认"="右边即为一个“值”,而"="左边即为一个“容器”
        例如:
           int a = 125;
           int b = 125;
           int c = a + b;
    可以知道a、b、c均是在定义后由系统分配地址和空间产生的“容器”,而"="右边即使一些可以产生“值”的语句

3. Increment and decrement operators

The difference between ++i and i++

It's almost the same when used alone

Code display :

public class Demo {
    
    
    public static void main(String[] args){
    
    
        int ans1 = 1;
        System.out.println("ans1  "+ans1);
        ans1++;
        System.out.println("ans1  "+ ans1);
        int ans2 = 1;
        System.out.println("ans2  "+ans2);
        System.out.println("ans2  "+ ++ans2);
    }
}

Output result:

ans1  1
ans1  2
ans2  1
ans2  2

Comparative use:

a、

Code display:

public class Demo {
    
    
    public static void main(String[] args){
    
    
       int i = 10;
        System.out.println("i  "+i);
       int j = i++;
        System.out.println("i: "+i);
        System.out.println("j: "+j);
    }
}

Output result:

i  10
i: 11
j: 10

b、

Code display:

public class Demo {
    public static void main(String[] args){
       int i = 10;
        System.out.println("i  "+i);
       int j = ++i;
        System.out.println("i: "+i);
        System.out.println("j: "+j);
    }
}

Output result:

i  10
i: 11
j: 11

That is to say, when "++" is in an assignment statement, its appearance before and after the variable is different, and the result of the statement is also different.

When "++" is behind the variable, that is, int j = i++; the execution sequence of the entire statement is to first assign the value of i to j, and then perform i++ operation on i. My understanding is to execute the statement content where i++ is located first. After executing the self-addition to the i variable;

When "++" is in front of the variable, that is, int j = ++i; the execution order of the entire statement is to execute the ++i statement first, and then assign the value of i to j. My understanding is to execute the addition of the i variable first, and then execute the statement where ++i is located.

Among them, a situation occurred during debugging. Fortunately, the teacher also mentioned it in class

public class demo {
    public static void main(String[] args){
        int ans = 1;
        System.out.println(ans++);//输出的结果是ans=1;
    }
}
   当然通过上述的理论视乎也能解释的通
   下面讲一下课堂上吸收到的思想
    还原函数println();
   假定函数返回值为void
       即:void println(int arg){}
   上述Java代码实际操作为:
       int ans = 1;
       int arg = ans++;
       void println(int arg){}
       System.out.println(ans++);//输出的结果是ans=1;

About "+=, -=, =, /=,^=..."

Note that the statement i+=1; under some variables is not the same as i=i+1;

public class Demo {
    public static void main(String[] args){
      byte i = 1;
      i = i + 1;//报错,不兼容的类型,1从int型转换为byte型会丢失精度
      i+=1;//未报错,等价与i = (byte)(i+1),改内容涉及到强制转换,整型常量默认未int型,浮点型常量默认为double型
        System.out.println(i);
    }
}

4. Logical operators

Logical operators: & |! && || == != >= <=

Among them, "&" is called logical AND, "&&" is called short-circuit AND ;

//(语句1 & 语句2)———>boolean值
//逻辑与"&"运算结果为:
//true & true ---> true
//true & false ---> false
//false & true ---> false
//false & false ---> false

//(语句1 && 语句2)———>boolean值
//短路与"&&"运算结果为:
//true && true ---> true
//true && false ---> false
//false && true ---> false
//false && false ---> false

//其中逻辑与"&"和短路与"&&"的区别是:
//逻辑与即使在判断语句的过程中遇到了语句内容产生的值为false,也会将整个"&"存在的语句执行完毕,输出最终值;
//短路与则是在判断语句的值出现false后终止判断语句,输出最终值。
//故在实际操作中短路与使用较多,且在使用短路与的同时应该考虑到将容易产生false的语句放到靠前位置(当然也并非所有情况都是如此,从考虑到程序运行效率和速度的时候可以这样)

There is also the difference between "|" and "||":

//(语句1 | 语句2)———>boolean值
//"|"运算结果为:
//true | true ---> true
//true | false ---> true
//false | true ---> true
//false | false ---> false

//(语句1 || 语句2)———>boolean值
//"||"运算结果为:
//true | true ---> true
//true | false ---> true
//false | true ---> true
//false | false ---> false

5. Ternary operator

表达式1 ? 表达式2 : 表达式3
   例如:int a = 100;
        int b = 200;
        int c = (a > b) ? a : b;


   public class Demo {
    public static void main(String[] args) {
        int a = 100;
        int b = 200;
        int c = (a > b) ? a : b;
        System.out.println(c);//结果为 200
    }
}

Guess you like

Origin blog.csdn.net/weixin_45698813/article/details/112944637