1. Mathematical operators

Concept:

  • Basic operations: including addition (+), minus (-), a (*), in addition to (/).
    • In a division operation, if the operands are integers, the result is also an integer.
  • Mod operation: the mod operator (%), any number mod 0 is equal to 0, and the plus and minus signs only agree with the operands on the left-hand side of the equal sign.
  • Divide by zero problem: any number divided by 0 will throw an exception, any number divided by 0.0 result is infiniteinfinity.

Source: / javase – start /

  • src: c.y.calculation.MathOperatorsTest.base()
/ * * *@author yap
 */
public class MathOperatorsTest {

    @Test
    public void base(a) {
        System.out.println(0 % 2);
        System.out.println(5 % 2);
        System.out.println(5 % -2);
        System.out.println(-5 % 2);
        System.out.println(-5 % -2);
        System.out.println(1 / 0.0);
        System.out.println(1 / 0); }}Copy the code

1.1 since the operation

Concept:

  • Since the operation++ / --I’m adding or subtracting 1 to myself.
  • The automatic operation has the highest priority.
  • Self-operation can only be applied to variables, constants have no self-operation.
  • Self-operation is divided into use and then add (a++) and add after (++a);

1.2 Numerical overflow problem

Concept:

  • As we all know, a variable has its own storage range, for example, the storage range of byte is between -128 and 127, so if there is a value overflow during calculation, the phenomenon will occur:
    • The maximum plus 1 becomes the minimum
    • The minimum minus 1 becomes the maximum
  • The principle of binary computing: the source code is converted into a complement code, the complement code is used for calculation, and the result is converted back to the source code.
  • Simulate the process of 2147483647 + 1
    • Source code: [01111111111 11111111 11111111]
    • Reverse code: [01111111 11111111 11111111 11111111], the same as the original code
    • Complement code: [01111111 11111111 11111111 11111111], the same as the original code
    • Add one: [10000000 00000000 00000000 00000000], the calculation will affect the symbol bit, the toBinaryString() method prints this value
    • Inverse code: [1111111111 11111111 11111111 11111111], the symbol bit is not moving, the rest of the bit is inverse
    • Complement code: [10000000 00000000 00000000 00000000], inverse code + 1, symbol bit does not participate in the complement carry
    • True value: -2147483648 (int minimum)
  • Analog – 2147483648-1 process
    • Source code: [10000000 00000000 00000000 00000000]
    • Inverse code: [1111111111 11111111 11111111 11111111], the symbol bit is not moving, the rest of the bit is inverse
    • Complement code: [10000000 00000000 00000000 00000000], inverse code + 1, symbol bit does not participate in the complement carry
    • Minus one: [01111111 11111111 11111111 11111111], the calculation affects the sign bit, and the toBinaryString() method prints this value
    • Reverse code: [01111111 11111111 11111111 11111111], the same as the original code
    • Complement code: [01111111 11111111 11111111 11111111], the same as the original code
    • True value: 2147483647 (int Max)

Source: / javase – start /

  • src: c.y.calculation.MathOperatorsTest.numericalOverflow()

    @Test
    public void numericalOverflow(a) {
        int maxValueOfInt = 2147483647;
        int result = maxValueOfInt + 1;
        System.out.println(result);
    }

Copy the code

2. The assignment operator

Concept: Assignment is represented by an equal sign in Java and is understood backwards:

  • + =Additional,a += bIs equivalent to thea = a+bThat will bea+bThe result of the reassignment toa
  • - =: after reduction,a -= bIs equivalent to thea = a-bThat will bea-bThe result of the reassignment toa
  • * =: after take,a *= bIs equivalent to thea = a*bThat will bea*bThe result of the reassignment toa
  • / =: in addition to the track,a /= bIs equivalent to thea = a/bThat will bea/bThe result of the reassignment toa
  • % =: Pursue the remainder,a %= bIs equivalent to thea = a%bThat will bea+bThe result of the reassignment toa

3. Compare operators

Concept: Comparison operations must return Boolean values, including: == / > / < / >= / <= /! =.

4. Logical operators

Concept: logic operations including logic and (&), logic or (| |) and logic (!) .

  • Formula: and in false (false), or in true (true).
  • Logical operation priority: Not > and > or.

5. Bit operators

Concept: bit operation occurs between the complement of the operands, and the true value can be obtained after the calculation result of the complement is completed again and returned to the original code:

  • Short circuit: Logical operation will have short circuit, but bit operation will not.
  • Bitwise and$If there are 0’s, then 0’s, then 1’s.
  • Bitwise or|: if there are 1, then 1, then 0.
  • According to the not~: 0 becomes 1, and 1 becomes 0, including the sign bit.
  • The bitwise exclusive or^: equal to 0, different to 1, equivalent to bitwise without carry addition:
    • Xor operations satisfy commutative and associative laws, i.e. a^(b^c) is equal to (c^b)^z.
    • Rule: N^0=N, N^N=0.
  • Move the left<<: Overflow is ignored when moving to the left, and 0 is used to fill the right bit:
    • A moved b bits to the left is the same thing as a times 2 to the b.
  • Move right with sign>>: Overflow is ignored when moving to the right with a sign, and the left is filled with sign bits:
    • If A moves b to the right, that’s the same thing as a over 2 to the b.
  • Unsigned move to the right>>>: When bitwise unsigned right movement, overflow is ignored and 0 is used to fill the left bit.

Source: / javase – start /

  • src: c.y.calculation.MathOperatorsTest
    /** * 

4 & 6 Process analysis

**

00000000.. 00000100:4 Source code * < P > 00000000.. 00000100: inverse get 4 inverse code, same as the original code * < P > 00000000.. 00000100: add 1 to get 4 complement, same as the original code *

00000000.. 00000110:6 Source code * < P > 00000000.. 00000110: get 6 inverse code, same as the original code *

00000000.. 00000110: add 1 to get 6 complement, same as original code *

00000000.. 00000100:4&6 result, 0 then 0 *

00000000.. 00000100: get the result of the inverse code, the same as the original code * < P > 00000000.. 00000100: Add 1 to get the result's complement, the same as the original code *

4: true value */

@Test public void andOfBit(a) { System.out.println(Integer.toBinaryString(4 & 6)); System.out.println(4 & 6); } / * * * < h1 > 4 | 6 process analysis < / h1 > * * < p > 00000000. 00000100:4 Source code * < P > 00000000.. 00000100: inverse get 4 inverse code, same as the original code * < P > 00000000.. 00000100: add 1 to get 4 complement, same as the original code *

00000000.. 00000110:6 Source code * < P > 00000000.. 00000110: get 6 inverse code, same as the original code *

00000000.. 00000110: add 1 to get 6 complement, same as original code *

00000000.. 00000100:4 | 6 as a result, one is 1 * < p > 00000000. 00000110: get the inverse of the result, the same as the original code * < P > 00000000.. 00000110: Add 1 to get the result's complement, the same as the original code *

6: true value */

@Test public void orOfBit(a) { System.out.println(Integer.toBinaryString(4 | 6)); System.out.println(4 | 6); } /** *

~4 Process analysis

**

00000000.. 00000100:4 Source code * < P > 00000000.. 00000100: inverse get 4 inverse code, same as the original code * < P > 00000000.. 00000100: add 1 to get 4 complement, same as original code *

11111111.. 11111011: ~4 result, all bits are reversed, including symbol bit * < P > 10000000.. 00000100: get the inverse of the result * < P > 10000000.. 00000101: Add 1 to get the result's complement *

-5: true */

@Test public void notOfBit(a) { System.out.println(Integer.toBinaryString(~4)); System.out.println(~4); } /** *

4 ^ 6 Process analysis

**

00000000.. 00000100:4 Source code * < P > 00000000.. 00000100: inverse get 4 inverse code, same as the original code * < P > 00000000.. 00000100: add 1 to get 4 complement, same as the original code *

00000000.. 00000110:6 Source code * < P > 00000000.. 00000110: get 6 inverse code, same as the original code *

00000000.. 00000110: add 1 to get 6 complement, same as original code *

00000000.. 00000010:4 ^ 6 result, no carry sum *

00000000.. 00000010: reverse the result of the inverse code, the same as the original code *

00000000.. 00000010: Add 1 to get the result's complement, same as the original *

2: true */

@Test public void xorOfBit(a) { System.out.println(Integer.toBinaryString(4 ^ 6)); System.out.println(4 ^ 6); } /** *

-2 << 3 Process analysis ** < P > 10000000.. 00000010: -2 Source code * < P > 11111111.. 11111101: inverse to get -2 inverse *

11111111.. 11111110: Add 1 to get -2 complement *

11111111.. 11110000: -2 << 3 result, move 3 left, overflow ignored, fill with 0. *

10000000 .. 00001111: get the inverse of the result *

10000000.. 00010000: Add 1 to get the result's complement *

-16: true */

@Test public void leftMoveOfBit(a) { System.out.println(Integer.toBinaryString(-2 << 3)); System.out.println(-2 << 3); } /** *

-2 >> 3 Process analysis ** < P > 10000000.. 00000010: -2 Source code * < P > 11111111.. 11111101: inverse to get -2 inverse *

11111111.. 11111110: Add 1 to get -2 complement *

11111111.. 11111111: -2 >> 3 Result, move 3 to the right, overflow ignored, fill with sign bits. *

10000000 .. 00000000: the inverse of the result * < P > 10000000.. 00000001: Add 1 to get the result's complement *

-1: true */

@Test public void rightMoveOfBit(a) { System.out.println(Integer.toBinaryString(-2 >> 3)); System.out.println(-2 >> 3); } /** *

-2 >>> 3 Process analysis ** < P > 10000000.. 00000010: -2 Source code * < P > 11111111.. 11111101: inverse to get -2 inverse *

11111111.. 11111110: Add 1 to get -2 complement *

00011111.. 11111111: -2 >>> 3 Result, move 3 to the right, overflow ignored, fill with 0. *

00011111 .. 11111111: get the inverse code of the result, the same as the original code * < P > 10000000.. 00000001: Add 1 to get the result's complement, same as the original * < P > 536870911: true */

@Test public void noSignRightMoveOfBit(a) { System.out.println(Integer.toBinaryString(-2 >>> 3)); System.out.println(-2 >>> 3); } Copy the code

6. Ternary operators

Concept: ternary operators, also known as trinary operators, also known as ternary operators.

  • Formula:X ? Y : Z, where X is a Boolean expression and returns Y if the result of X is true, and Z otherwise.
  • The return type of Y and Z must be the same.
  • The ternary operator must accept its return value, and its return value type is your return value type Y and Z.

7. Math Utility Class

The Math utility class provides a large number of methods for Math operations. The Math class is final and therefore cannot be inherited from the Math class. Methods in the Math class are static, so you can use the methods of the Math class directly without having to create Math objects.

  • Math.abs(-10): Absolute value.
  • Math.sqrt(16): square root.
  • Math.cbrt(8): cube root.
  • Math. Ceil (2.1): Round up.
  • Math. Floor (2.9): Round down.
  • Math.max(1, 6): Maximum value.
  • Math.min(1, 6): Minimum value.
  • Math.pow(2, 3): A to the b power.
  • Math. Round (2.4): Round off.
  • Math.random(): random number: a number between [0-1] is generated at random each time.

New Random().nextint (5) is more recommended for Random number generation code; , can directly generate a random int value in the range of 0 to 5.