Java knowledge point accumulation (2)

  • 4. Conditional operator (ternary operator):

String type = score<60?"不及格":"及格";


int i = (string=="hello"? 10:-1);

int j = (x>0 ? 1:(x>10 ? 2:-1));//多重嵌套

  • 5. Static import is a newly added function in JDK1.5. Its function is to import the static properties of the specified class, so that the static properties can be used directly;

import static java.lang.Math.PI;    //导入Math类的PI属性

  • 6. The equals() method provides the logic to define "object content is equal"


  • 7. When the parameterized constructor is defined in the class, the parameterless constructor no longer exists. If you want to use it, you must declare it explicitly in the class;

  • 8. Logical operators:

operator symbol illustrate
logical and &(and) The result is true only if both operands are true, otherwise it is false
logical or |(or) One of the two operands is true, the result is true
short circuit with &&(与) As long as one is false, return false directly
short circuit or || (or) As long as one is true, return true directly
logical not ! (No) Negative: !false is true, !true is false
logical XOR ^ (exclusive or) Same as false, different as true
  • Short-circuit and and short-circuit or use the short-circuit method to calculate from left to right. If the value of the logical expression can be determined only by the operand on the left side of the operator, the operand on the right side of the operator will not continue to be calculated, which improves efficiency.

  • 9. Recursive structure

    • Recursion is a common problem-solving method, that is, to gradually simplify the problem. The basic idea of ​​recursion is to "call itself", a method using recursion will call itself directly or indirectly.
    • Using recursion can solve some complex problems with simple programs. For example: calculation of Fibonacci sequence, Tower of Hanoi, quick sort, etc.
    • The recursive structure consists of two parts:
      • (1) Define the recursive header. Answer: When not to call its own method. If there is no head, it will fall into an infinite loop, which is the end condition of the recursion .
      • (2) Recursive body. Answer: When do you need to call your own method.
public class Test22{
    public static void main(String[] args) {
        long d1 = System.currentTimeMillis();
        System.out.printf("%d阶乘的结果:%s%n",10,factorial(10));
        long d2 = System.currentTimeMillis();
        System.out.printf("递归费时:%s%n",d2-d1);   //耗时:32ms
    }
    /** 求阶乘的方法*/
    static long factorial(int n) {
        if(n == 1) {//递归头
            return 1;
        }else{//递归体
            return n*factorial(n - 1);  //n! = n*(n - 1)!
        }
    }
}
    • Recursive flaws:
      • Simple programs are one of the advantages of recursion, but recursive calls will occupy a lot of system stacks and consume a lot of memory. When there are many levels of recursive calls, the speed is much slower than loops, so be careful when using recursion.
    • Precautions
      • Any problem that can be solved recursively can also be solved iteratively (loops). Recursion can be used when the recursive method can increase the natural reflection of the problem, is easy to understand and debug, and does not emphasize the efficiency problem;
      • Try to avoid using recursion when high performance is required. Recursive calls take time and memory.

Guess you like

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