Operators textbook series (a) - Java arithmetic operator

Operators textbook series (a) - Java arithmetic operator

More, click here to learn, sign up for

Arithmetic operator
substantially are:
± * /%

Increment decrement
++ - -
Step 1: Basic arithmetic operator
Step 2: Practice - summing
Step 3: Answer - summing
Step 4: calculation means exceeds an arbitrary length int
Step 5: calculation means is less than the length of any int
step 6:% modulus
step 7: increment decrement
step 8: Save increment from before and after the operator facing opposing differences
step 9: exercise - increment
step 10: the answer - increment

Example 1: Basic arithmetic operator
± * /

Basic Math

public class HelloWorld {
    public static void main(String[] args) {
        int i = 10;
        int j = 5;
        int a = i+j;
        int b = i - j;
        int c = i*j;
        int d = i /j;
    }
}

Example 4: computing means exceeds an arbitrary length int
If any arithmetic unit having a length of more than int, then the calculation result is calculated according to the length of the longest
such
int. 5 = A;
long. 6 = B;
A + B -> the result is a long type

public class HelloWorld {
    public static void main(String[] args) {
 
        int a = 5;
        long b = 6;
        int c = (int) (a+b); //a+b的运算结果是long型,所以要进行强制转换
        long d = a+b;
         
    }
}

Example 5: calculation means is smaller than the arbitrary length int
If any arithmetic unit length does not exceed int, then the calculation result is calculated according int
byte. 1 = A;
byte B = 2;
A + B -> int type

public class HelloWorld {
    public static void main(String[] args) {
        byte a = 1;
        byte b= 2;
        byte c = (byte) (a+b); //虽然a b都是byte类型,但是运算结果是int类型,需要进行强制转换
        int d = a+b;
    }
}

Example 6:% modulus
% fetch I, also known as modulo
5 is divided by 2, I 1

public class HelloWorld {
    public static void main(String[] args) {
 
        int i = 5;
        int j = 2;
        System.out.println(i%j); //输出为1
    }
}

Example 7: increment decrement
++

An increase or decrease on the basis of the original 1

public class HelloWorld {
    public static void main(String[] args) {
 
        int i = 5;
        i++;
        System.out.println(i);//输出为6
 
    }
}

Example 8: Save increment from the difference before and after the operator set the counter
to + Example
int. 5 = I;
I ++; the first value, and then calculating
++ I; the first operation, and then the values

public class HelloWorld {
    public static void main(String[] args) {
        int i = 5;
        System.out.println(i++); //输出5
        System.out.println(i);   //输出6
         
        int j = 5;
        System.out.println(++j); //输出6
        System.out.println(j);   //输出6
    }
}
Published 32 original articles · won praise 182 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_44092440/article/details/102970971