Chapter 3 - Operator

Think in java study notes

pikzas

2019.03.03

Chapter 3 - Operators

Knowledge Point

Arithmetic operators

"+" Special character string processing time

Conventional arithmetic operators are addition, subtraction modulo et assignment (+ - * / =%), all of these operators are the basic data types can be used directly, but is determined that the object can have memory addresses are equal, whether ranging assignment (==,! =, =) operator can be used. For a reference to this particular String object type, you may also be used splice operator (+), which is a Java provides syntactic sugar, decompile bytecode will find "+" sign will be replaced by the StringBuffer append method.

"=" Special treatment (alias phenomenon) in the object assignment process

This phenomenon occurs because Java is passed by value,
for basic data types, in fact, is a variable value pointing to a particular stack, a = b is performed such assignment, when in fact the value of b is copied to the a variable.
For reference data types, m = n This operation is the variable n pointed object address assigned to the m, at this time if m, n points to the same object to the same memory address, either through m n or any change to the object, all points to the object reference (ie, variable) will be reflected this change.

class Tank{
    int level;
}

public class Demo{
    Tank t1 = new Tank();
    Tank t2 = new Tank();
    t1.level = 12;
    t2.level = 34;
    print("--1--"+t1.level+"---"+t2.level);
    t1 = t2; // t1 变量的指针指向了t2 的所指向的那个对象
    t1.level = 56; // 通过t1 对对象作出改变
    print("--2--"+t1.level+"---"+t2.level);
}

// 打印结果
// --1--12---34
// --2--56---56

Aliasing will appear in the method call


public class Test {

    public static void f(int x) {
        x = 12;
    }

    public static void f2(String args) {
        args = "1234";
    }

    public static void f3(Bean bean) {
        bean.x = 123;
    }

    public static void main(String[] args) {
        int y = 44;
        f(y);
        System.out.println(y);
        String str = "xxx";
        f2(str);
        System.out.println(str);
        Bean bean = new Bean();
        bean.x = 111;
        f3(bean);
        System.out.println(bean.x);
    }
}

class Bean{
    public int x;
}

// 输出结果为
44
xxx
123  //为什么这个值发生了变化,而上面两个没有呢

Division operation (/) returning the java only the integer part is not rounded.

Automatic increment and decrement

  • For prefix increment and decrement (++ a or --a) will first perform an operation, the generated value
  • To increment and decrement the suffix (a ++ or A--) will be a value Mr, the operation is performed
class Demo{
    public static void main(String[] args){
        int i = 1;
        System.out.println("i: " + i); //1
        System.out.println("++i:" + ++i); //2
        System.out.println("i++:" + i++); //2
        System.out.println("i: " + i); //3
        System.out.println("--i:" + --i); //2
        System.out.println("i--:" + i--); //1
    }
}

Relational Operators

Results relational operator returns a Boolean value of true or false

All operators comprises greater than (>), less than (<), greater than or equal to (> =), less than or equal to (<=), equality (==), not equal (! =). More operator can act on all objects except a Boolean value.

  • Only true or false Boolean value than anyone else who does not exist large.
  • If the operator above when the reference data acting type, its address is compared, if required comparison target "value", the object will need to override equals method. The default of the underlying data type of packaging, has to do override equals treatment.
public class TestOne{
    public static void main(String[] args) {
        Integer i1 = new Integer(1);
        Integer i2 = new Integer(1);
        System.out.println(i1 != i2); //true
        System.out.println(i1 == i2); //false
        System.out.println(i1 > i2);  //false
        System.out.println(i1 < i2);  //false
        System.out.println(i1.equals(i2)); //true
    }
}

Logical Operators

Since the java is a strongly typed language, performing the logic operation, when parameters are to be involved in computing the Boolean type, can not run (123 && 123) so as js = true. Note that the short-circuit logic operation process, can be determined once the calculation result, the expression will stop after the operation and then further expression.

Constant directly

Generally in the program are the numbers we use are decimal, under specified circumstances, can also be used in binary, octal or hexadecimal.

class Demo{
    int i = 0x2f;  // 0x或者0X 开头表示该数字为十六进制 
    int j = 0177;  // 0 开头表示该数字为八进制
    int m = 0b101; // 0b 开头表示该数字为二进制
    int n = 100; // 默认情况下表示为十进制
    
    long l = 123123123;
    long l1 = 234234234L;
    long l2 = 345345345L;
    
    float f = 123.234;
    float f1 = 12.333f;
    float f2 = 23.555F;
    
    double d1 = 1.234d;
    double d2 = 2.345D;
    
}

In some special hexadecimal numbers represent more convenient time, such as char type 2byte = 16bits occupied bits in the range 0. - 2 16 1,2 8 th power ✖️2 8 -1 = 256 × 256-1 = 65535-1 = 0xffff-1

Different hexadecimal conversion methods, the number is assumed that such a format if m = 0x mab which is representative of a hexadecimal digit hexadecimal then converted to decimal as ab: A 16 × (2-1) + B × 16 (1-1)

Bitwise operator

Symbol has the bit operation (&), or (|), exclusive OR (^), non (~)

First converted to binary numbers, and then from right to left by one bit takes operation will look as a true, 0 viewed false. The bitwise operator similar to the logical operator, ^ can be regarded as if two identical, then take 0, 1 or take.

class Demo{
    public static void main(String[] args){
      System.out.println(1&1);  // 1
      System.out.println(1&0);  // 0
      System.out.println(0&1);  // 0
      System.out.println(0&0);  // 0
      
      System.out.println(1|1);  // 1
      System.out.println(1|0);  // 1
      System.out.println(0|1);  // 1
      System.out.println(0|0);  // 0
      
      System.out.println((0b11)^(0b11));  // 0
      System.out.println((0b10)^(0b10));  // 0
      System.out.println((0b11)^(0b10));  // 1
      System.out.println((0b11)^(0b00));  // 3
      
      // ~按位去反 输入0得1 输入1得0
    }
}

The shift operators

Signed right shift left, right (<< >>) and unsigned (>>>)

Type Conversion

Then do the variable type conversion when, if converted from a smaller type to a larger type, they can automatically. On the other hand you need to explicitly affirm that shows that you already know the possible risks of information loss.

In the double or float data converted to an int, always taking the logarithmic axis that nearest value from zero (int) (29.5) = 29, (int) (- 29.5) = -29

If you want to achieve rounding, you need to use Math.round (); always to the big turn.

class Demo{
    public static void main(String[] args){
        System.out.println(Math.round(-29.3)); //-29
        System.out.println(Math.round(-29.5)); //-29
        System.out.println(Math.round(-29.7)); //-29
        System.out.println(Math.round(29.3));  //29
        System.out.println(Math.round(29.5));  //30
        System.out.println(Math.round(29.7));  //30
    }
}

Guess you like

Origin www.cnblogs.com/Pikzas/p/12154360.html