Java operators

PS: At the lowest level, data in Java is manipulated by using operators.

Use of operators and their precedence

Basic operators in Java and their precedence table
The following is the complete code (1):

package cxcf;
public class Day3 {
    public static boolean fun1(int val){
        System.out.println("val<1");
        return val<1;//true
    }
    public static boolean fun2(int val){
        System.out.println("val<2");
        return val<2;//false
    }
    public static boolean fun3(int val){
        System.out.println("val<3");
        return val<3;
    }

    //void 方法的返回值
    //Max2 方法名称
    //(int a,int b) 形式参数  形参
    public static int Max2(int a,int b){
        if(a>b){
            return a;
            //System.out.println(a);
        }else{
            return b;
            //System.out.println(b);//()函数
        }       
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        boolean b5=fun1(0)&&fun2(2)&&fun3(1);//短路 与,有一个为false,短路
        System.out.println(b5);
        boolean b6=fun1(0)||fun2(2)||fun3(1);//有一个为true,短路
        System.out.println(b6);

        // ! && || 只支持boolean值
        boolean b1=true;
        boolean b2=false;
        if(!b1==true){
            System.out.println("is true");
        }else{
            System.out.println("is false");
        }

        int[] array={1,2,3,4,5};//[] 声明是一个数组
        array[0]=2;//[]属性获取 下标里面的内容
        System.out.println(array);
        System.out.println(array[0]);
        int len=array.length;//方法调用 

        System.out.println(len);
        int a=10;
        int b=20;
        //Max2(a,b);
        int e=Max2(a,b);
        System.out.println(e);
        int c=30;//= 赋值号
        int d=40;//代码冗余 
    }
}


----------

  • Break down the code:
  • 1. Notes on [ ], . , ( ):
    (1) array[ ] declares an array;
    (2) array[0]=2; the attribute gets the content in the subscript;
    (3) "." array.length; Call method;
    (4) ( ) can be used to create a function:
    public void int Max2(int a,int b)
  • 2. Logical operators "AND" && "OR" || "NOT"! , can generate a Boolean value according to the logical relationship of the parameters, and only the Boolean value can be applied, :
    (1) Short circuit
    When using logical operators, a phenomenon will be encountered, that is, once the value of an expression can be determined, The rest of the expression is no longer evaluated.
    boolean b5=fun1(0)&&fun2(2)&&fun3(1);//Short-circuit and, if one of them is false, short-circuit and no longer output;
    boolean b6=fun1(0)||fun2(2)|| fun3(1);//Short-circuit or, if one of them is true,
    the output will not be calculated later; the
    output result:
    write picture description here

The following is the complete code (2):

package cxcf;
public class Day31 {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // ++ 后置++ 前置++
        int a1=10;
        a1++;
        ++a1;//单个写没有任何区别
        int a2=a1++;//tmp=a2; a3=tmp; a2+1;
        int a4=++a2;//a2+1 a4
        System.out.println(a2);
        System.out.println(a4);

        //算余数
        System.out.println(10%3);
        System.out.println(-10%3);
        System.out.println(-10%-3);//取商取在绝对值以内
        System.out.println(10%-3);
        System.out.println("========");

        float f1=5/2;
        float f2=(float)5.0/2;
        float f3=(float)5/2;
        float f4=5/(float)2;
        System.out.println(f1);//5是int类型,取整   2.0
        System.out.println(f2);//2.5
        System.out.println(f3);//2.5
        System.out.println(f4);//2.5

        short sh=1;
        short sh2=(short)(sh+1);
        sh+=1;//sh=sh+1;-=  *=  /=
        //instanceof 比较类型
    }
}
  • Break down the code:
  • Arithmetic operators: addition,
    subtraction, multiplication and division "+ - * / ";
    remainder "%";
    (10%3), (-10%3), (-10%-3), (10%-3) The output of the situation is "1, -1, -1, 1";
    assignment number, assign the value after the symbol to the preceding "=";
    "+=, -=, *=, /=, %=", a+= b; equivalent to a=a+b, the rest are the same;
  • Auto-increment and decrement "++-":
    a1++; and ++a1; do not make any difference in single writing;
    int a2=a1++; assign first and then add and subtract;
    int a4=++a2; add and subtract first and then assign.

  • Bitwise operator
    "&": bitwise AND: 0 in case of 0;
    "|" bitwise OR: 1 in case of 1;
    "~" bitwise negation: unary operator;
    "^" bitwise XOR: both are 0 , the difference is 1;
    Tips: The bitwise operator can be used in combination with (=), "&=, |=, ^=", this is legal, since "~" is a unary operator, it cannot be used with (= ) combined.

  • Shift operator:
    "<<" left shift (low-order zeros):
    ">>" right-shift (if the high-order symbol is 0, insert 0 in the high-order, if it is 1, insert 1 in the high-order);
    ">>>" none Sign right shift operator (no matter whether it is positive or negative, the high bit is inserted with 0);
    write picture description here


Supplement: error-prone points:
float f1=5/2; the output is 2, both of which are int types;
float f2=(float)5.0/2; the output is 2.5;
float f3=(float)5/2; the output is 2.5;
float f4=5/(float)2; output is 2.5;

short sh=1;
short sh2=(short)(sh+1);1 is of type int and must be converted, otherwise an error will be reported.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326542593&siteId=291194637