Java study notes -04

Operators java

Operator is used to indicate the operation mode for the operands

  • In accordance with the number of operands to be classified
    monocular binocular trinocular
    a ++ a + b (a> b) x:? Y;

  • Operator functions according to classify

    1. Arithmetic Operators
    • [+ - * /% (Remainder modulo)]

      int x = 5;
      x/2 = 2;
      x%2 = 1;
      
    • [++ --]

      int x = 1;  
      x = x+1;  x空间内的值,自己增加了一个
      x++;  x空间内的值 自增一个
      ++x;  对于x空间内的值来讲,都是一致,最终的结果都自增了一个
      

    examp1

    int x = 1;  在内存空间栈中划一块空间x,从存储区拷贝一份常量1赋值给x
    int y = x++;  ++在后 先赋值 后自增(先进行计算,后赋值)
    x==2  y==1
    

    1. [assignment] in a stack memory space designated space x, a copy of the constants from the storage area assigned to 1 x
    2. [computing] in the stack memory space to create a temporary copy of the x (because the right of x + so back up again increment),
    a good backup do increment, the value of x deity space changes from 1 to 2, and finally the value of copy space assignment y, then x copy space will be destroyed.

    x++;//x=x+1;
    1. 将x变量空间的内容先取出,常量区取出1,进行计算 ,再次存回x空间
    2. x在想要做值交换的时候,会产生一个临时的副本空间(备份)
    3. ++在变量的前面,先自增后备份
    4. ++在变量的后面,先备份后自增
    5. 最终会将副本空间内的值赋给别人
    

    examp2:

    int x = 1;
    int y = ++x;
    x==2  y==2
    

    examp3 :

    int a = 1;  
    a = a++;   
    a==1
    

    examp4:

    int a = 1;
    for(int i=1;i<=100,i++){
    a = a++;
    }
    a=1
    

    examp5 ;

    int m = 1; //2,1,0
    int n = 2; //3,2,1
    int sum = m++ + ++n - n-- - --m + n-- - --m;
    1   +  3  - 3   -  1  + 2   -  0
    sum==2
    
    1. Assignment operator
      within the assignment symbol = equal right of the contents (value, a reference) into the left operand variable space
      + = - = = * /% = = complex assignment symbol

      examp1:

    int x = 1;
    x += 2; //3
    x = x+2; //3
    

    examp2:

    byte x = 1; //常量32bit =自动转化
    x += 2; //+=算作一个运算符号,自动类型提升为3 =自动转化
    x = x+2; //编译出错 类型从int转化成byte可能有损失
    x变量空间的值取出,从常量区取过来2,加法运算,结果重新存回x变量空间内
    x空间-->1  8bit
    常量区-->2 32bit
    00000001 + 00000000 00000000 00000000 00000010
    +自动类型提升 8bit --32bit
    00000000 00000000 00000000 00000001
    00000000 00000000 00000000 00000010
    +00000000 00000000 00000000 00000011 ==3
    
    1. Relational operators (comparison)
    > >= < <= != == (对象 instanceof 类)-判断对象是不是某个类型
    比较运算符最终结果是true false  boolean
    
    1. logic operation
      • & Logical AND | Logical OR
      • ^ XOR logic expressions just before and after the two results are not the same, the result is true
      • ! Logical NOT
      • && short-circuit and
        • 1. Short circuit short circuit can occur under what circumstances?
          The current face value of the result is false when a short circuit occurs
        • 2. What in the end short circuit is short circuit?
          Short-circuiting all the calculation process after &&
        • 3. If a short circuit occurs
          a short circuit and, slightly better performance than &
        • 4. & && Logical AND shorted and there is no difference from the point of view of the final result of execution
        • 5. Short-circuit and short-circuit does not necessarily improve the performance of the current surface is only false when will occur, will improve performance
      • || short circuit or
        if the first condition is true will eventually be true

60
00000000 00000000 0,000,000,000,111,100 - Binary

00000000 000 trillion 111 100 000
0 7 4 - Eight-ary

0000 0000 0000 0000 0000 0000 0011 1100
0X 3 C - Hex


  • Bit (bit) calculation
    & Bitwise AND | Bitwise OR ^ bitwise exclusive OR ~ bitwise
    << bitwise left displacement >>> >> bitwise right shift bit right shift (unsigned)

    examp1:

3 & 5 ?
1. 将3和5转化成二进制表示形式
2. 竖着按照对应为止进行&|^计算(1-->true  0-->false)
3. 将计算后的二进制结果转化为十进制

            00000011   --3
            00000101   --5

    3 & 5   00000001   --1

    3 | 5   00000111   --7 
    3 ^ 5   00000110   --6
##### original code anti-code complement binary number that represents
  • 1-6 =? Positive number of anti-code complement the original code is the same as
    the original code 00000000 00000000 0,000,000,000,000,110
    anti-code 00000000 00000000 0,000,000,000,000,110
    complement 00000000 00000000 0,000,000,000,000,110

  • To -6 =?
    1,000,000,000,000,000 0,000,000,000,000,110 original code - Symbolic original code bits changes
    the inverted 1,111,111,111,111,111 1,111,111,111,111,001 - Keep the sign bit does not move, the other the inverse
    complement 1,111,111,111,111,111 1,111,111,111,111,010 - trans code plus a

Summary : computer either integer or negative, are stored in the form of 2's complement stored in
the inverted representation of a calculation process is negated (inverted every position)

  • Positive number 按位 HidariiUtsuri <<

-3 -2 -1 <-->0 1 2

<< 1 6 =? Corresponds to a power of 2 times multiplied by the displacement
00000000 00000000 00000110--6
00000000 00000000 00001100--12 left one
00000000 00000000 00011000--24 then left one

  • Positive Bitwise right shift >>

6 >> 1 = -? 2 times the equivalent displacement divided by the power of
00000000 00000000 00000011--3 right one
00000000 00000000 00000001--1 then right one

  • Negative bitwise right shift (unsigned) >>>

-6>>1 =?

! >> 1 reserved symbols fill the position 1

! >>> symbol is not retained no matter what turns out to be both and is 0
1,111,111,111,111,111 1,111,111,111,111,010
1,111,111,111,111,111 1,111,111,111,111,101 >> right one
0,111,111,111,111,111 1,111,111,111,111,101 >>> right one

Guess you like

Origin www.cnblogs.com/kknote/p/12670580.html