Thinking in Java learning Miscellany (Section 1-4)

Program flow control

Shift operator

Shift operator-oriented binary operand bits, which can be treated with a single type integer. Left shift operator (<<) operator can move the left side of the left operand of the operator median operator specified to the right (in the low complement 0). "Signed" using a right shift operator "sign extension": if the value is positive, 0 is inserted at a high level; if the value is negative, then a high insertion. Java also add a "unsigned" right shift operator (>>>), which uses "zero extend": regardless of the sign, are inserted in the high 0. If for char, byte, or short shift processing, the before shift takes place, automatically converted to an int. Only needed with five lower right. This prevents us an unrealistic move digits in a number in int. If the shift to a long value, the final result is long type. At this point only you use the right side of six low to prevent movement of the median value over long ready. But during the "unsigned" when the right shift may run into a problem, if for byte and short values ​​and shift operations may not get the correct results. They are automatically converted to an int, and a right shift. But the "zero expansion" will not happen, so in those cases the result will be -1.

Shifting may use the equal sign (= >> or << >>> = or =) compositions. In this case, the calculation value of the left side of the operator moves to the right by the value of the specified number of bits, the results obtained and then back to the left of the value assignment.

public class Test {
    public static void main(String[] args) {
        int i = 8;
        i >>= 2;
        System.out.println(i);
        i <<= 1;
        System.out.println(i);
        i <<= 1;
        System.out.println(i);
        i >>>= 1;
        System.out.println(i);
        i >>>= 1;
        System.out.println(i);
    }
}
// output
/*
2 4 8 4 2
*/

Ternary operator

? Boolean expression 0: Value 1

public class Test {
    public static void main(String[] args) {
        int i = 10;
        System.out.println(i < 10 ? 5 : 11);
        System.out.println(i == 10 ? i / 2 : 0);
    }
}
// output
/*
11 5
*/

Expression operators priority
unary operators> Arithmetic (shift) operator> relational operators> Logical (bitwise) operator> conditional operator> Assignment

  • Unary operators: +, -
  • Arithmetic (shift) operators: *, /,%, +, -, <<, >>
  • Relational operators:>, <,> =, <=, ==,! =
  • Logical (bitwise) operators: &&, ||, &, |, ^
  • Condition (Triad): A> B X:? Y
  • Assignment: = (assignment, and a composite, such as * =)

Goto label use in Java

Despite the overuse of goto can cause unreadable program, but in some cases, goto is the best way to structure control flow, so a lot of the language more or less still retains some of its usage for Java, the only tag is used in place before the loop.

public class Test {
    public static void main(String[] args) {
        int i = 0;
        outer:
        for (;true;){
            inner:
            for (;i < 10;i++){
                System.out.println("i = " + i);
                if (i == 2){
                    System.out.println("continue");
                    continue ;
                }
                if (i == 3){
                    System.out.println("break");
                    i++;
                    break ;
                }
                if (i == 7){
                    System.out.println("continue outer");
                    i++;
                    continue outer;
                }
                if (i == 8){
                    System.out.println("break outer");
                    break outer;
                }
                for (int k = 0; k < 5;k++){
                    if (k ==3) {
                        System.out.println("continue inner");
                        continue inner;
                    }
                }
            }
        }

    }
}
// output
/*
i = 0
continue inner
i = 1
continue inner
i = 2
continue
i = 3
break
i = 4
continue inner
i = 5
continue inner
i = 6
continue inner
i = 7
continue outer
i = 8
break outer
*/

Note that when you use switch ... case statement, after encountering a case to meet, if not after the break, will continue with the next statement after the break without tube does not meet the conditions, until it encounters a break statement or program segment is finished. Among the general should switch ... case statement, the last place a default condition to perform tasks under conditions of no alternative.

public class Test {
    public static void main(String[] args) {
        int i = 5;
        switch (i){
            case 3:
                System.out.println("case3: " + i);
            case 5:
                System.out.println("case5: " + i);
            case 7:
                System.out.println("case7: " + i);
            default:
                System.out.println("default: " + i);
        }
    }
}
// output
/*
case5: 5
case7: 5
default: 5
*/
public class Test {
    public static void main(String[] args) {
        int i = 5;
        switch (i){
            case 3:
                System.out.println("case3: " + i); break;
            case 5:
                System.out.println("case5: " + i); break;
            case 7:
                System.out.println("case7: " + i); break;
            default:
                System.out.println("default: " + i); break;
        }
    }
}
// output
/*
case5: 5
*/

Note that the required selection switch factor must be an integer value of the int or char. And if a string or alternatively float factor used, then they will not work in the switch statement.

Math.random()0-1 will produce a value, which is in the range [0, 1).

When we use the constructor, if we ourselves do not make any definitions in the class, it will create a default no-argument constructor for us, and when we define any constructor, with or without parameters, The system will no longer continue to help us to create a no-argument constructor.

Note: Although we can access static methods and static variables in non-static method, but the reverse is not enough. Because static data structure to be non-static methods take precedence.

Java is used by the garbage collector reclaim objects no longer in use occupy memory. But the garbage collector only knows to release that memory allocated by new, so I do not know how to release the object of "special" memory. To solve this problem, Java provides a method called finalize (), you can define it for our class. In the ideal case, should it works like this: Once the garbage collector is ready to release the memory occupied by objects, it first calls finalize (), but only once in the next garbage collection process, will really be recovered memory . So if you use finalize (), you can do some important work during the clearing and cleaning of garbage collection.

Garbage collection is only about memory with memory that is the only reason for the existence of the garbage collector is to recover programs you no longer use.

One of the most useful place finalize () is to observe the process of garbage collection.

In one class, the sequence is as defined in the order determined by the class variables. Even if a large number of variable definitions are found in the middle of the methods defined, those variables will get initialized (even before the builder calls) before calling any method.

Guess you like

Origin www.cnblogs.com/zhhfan/p/12600360.html