"Java Programming Thought" Notes Chapter 3 Operators

1. Operator types: 

Sequence of operations 1-7

1.1 Unary operators (unary operators) - negative sign, + positive sign, -- decrement, ++ increment

1.2 Arithmetic operators + - * / %

1.3 Shift operator << left shift (the low order is filled with 0), >> right shift (the high order of the negative number is 1, and the high order of the positive number is filled with 0), 

                        >>> unsigned right shift (regardless of positive and negative high-order 0s) (for binary)

(Can be used in combination with = <<= >>= >>>= similar to i += 1) Shift operator details

1.4 Relational operators  > < >= <= == != (generates boolean values)

1.5 Logical operators && || ! (generates boolean value)

1.6 Bitwise operators & and | or ^ XOR ~ NOT (unary) (for binary)

1.7 Ternary Operators 

boolean-exp? value0 : value1 //


2. Prefix and suffix

   2.1 Prefix -- ++ such as --i; ++i; first perform -1, +1 on i, then assign it to i and then perform the following operations;

   2.2 Suffix formula -- ++ such as i--; i++; Immediately after the operation (i--) ends, perform -1 +1 on i;

   2.3 There is a trap in the suffix type, i = i++ , this code has a pit, i will not increase automatically, the detailed reason is here 

int s = 1;
int i =	s++ * 3;
System.out.println(s);//2
System.out.println(i);//3
s = 1;
i = ++s * 3;
System.out.println(s);//2
System.out.println(i);//6
s = 1;
i = s++ + (s * 3);
System.out.println(s);//2
System.out.println(i);//7

   2.4 Different calculation results due to different positions of suffix expressions in expressions

int g = 5; //expression g + (g++ + g) is loaded from left to right in memory, and is calculated by priority
int i = g + (g++ + g); //The first g=5, g++ is 5 The last g is 6, the first g is 5 when loading data into the memory, after the g++ is loaded, the following g is 6
System.out.println(i);//16
g = 5;
i = g++ + (g + g);//g++ is 5 and the last two g are 6
System.out.println(i);//17

3. Aliasing phenomenon: Different references point to the same object

4. Random number generation

		Random rand = new Random() ;
		int k = rand.nextInt(100);
		int j = rand.nextFloat(); // The number of digits between 0.0 and 1.0 is not necessarily

4.1 When creating a Random object, no parameter (seed) is passed in, and the current time will be passed in as the seed of the random number generator

4.2 For the same seed, the same random number sequence will grow, and the current time keeps changing and the random number sequence is also different

4.3 Generate different types of random numbers and call the corresponding method

4.4 The parameters of the nextInt() method determine the upper limit of the random number , and also determine the lower limit of 0 , which is +1 when doing the divisor

5. Short circuit: 

  • When using logical operators , the rest of the expression is not evaluated. For example: test1() && test2() && test3();
  • If test1() is first calculated to be false, then false && test2 must be false, test2() will not be executed, [false && test2] is false and test3() will not be executed, if && becomes || Execute test3().

6. Direct Constant Specification 

  • Use L when assigning the initial value of float or Float, such as float f = 2L; double plus d or D, hexadecimal prefix 0X, octal prefix o

7. Details of type conversion between basic data

7.1  Byte, short, char operation will promote the result to int  

7.2 Type auto promotion Small range type + large range type = large range type    ( byte , short , charint  long   float double increase in turn

7.3 char c = 'a' will convert the integer to the corresponding ASCII code value, char c = 98 will convert the String to the corresponding character

7.4 Float, double are converted to integer and end directly without rounding. The round() method in java.lang.Math is used for rounding.

8. Data Types

7 data types

Guess you like

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