Java efficient programming

The early part of this article will be based on the Second Edition of Effective Java. Priority will be given to the points that have been contacted more frequently in the usual projects, and some insights in the usual projects will be supplemented on the basis of refining the books. Later, I will leave the book with suggestions to give myself ideas for efficient java programming. This article will continue to be updated.

If there is any infringement, please contact the blogger.

1. Consider using a builder when encountering multiple constructor parameters

Static factories and constructors have a common limitation, neither of them can scale well to a large number of optional parameters. How to have a class with many fields, every time the user uses different fields to initialize it will become troublesome. Generally speaking, for a class with a small number of fields, only a small number of construction methods can be used to solve it. When the field is greater than 4 , It is recommended to use the builder. E.g:

public class Exam {
    private int chinese = 0;
    private int math = 0;
    private int english = 0;
    private int science = 0;
    private int history = 0;

    public static class Builder {
        private int chinese = 0;
        private int math = 0;
        private int english = 0;
        private int science = 0;
        private int history = 0;

        public Builder(int chinese, int math) {
            this.chinese = chinese;
            this.math = math;
        }

        public Builder setEnglish(int english) {
            this.english = english;
            return this;
        }

        public Builder setScience(int science) {
            this.science = science;
            return this;
        }

        public Builder setHistory(int history) {
            this.history = history;
            return this;
        }

        public Exam build() {
            return new Exam(this);
        }
    }

    public Exam(Builder builder) {
        this.chinese = builder.chinese;
        this.math = builder.math;
        this.english = builder.english;
        this.science = builder.science;
        this.history = builder.history;
    }

    public static void main(String[] args) {
        Exam exam1 = new Builder(100, 90).setEnglish(80).build();
        Exam exam2 = new Builder(90, 93).setScience(70).build();
    }
}

2. Minimize the scope of local variables-Effective Java Article 45

In earlier languages ​​(such as C language), local variables must be declared at the beginning of a code block, but in Java, it is more advisable to declare variables as close as possible to the place where they are used for the first time. On the one hand, early declaration will bring difficulties to reading, on the other hand, it will also expand the scope of the variable. If other modules that should not use this variable are used incorrectly, it will not be easy to detect, which will increase the difficulty of debugging.

Almost every variable declaration should be initialized, but sometimes some variables are not initialized (for example, for some variables generated by factory methods that require special input parameters), the variable declaration should be postponed until it can be declared. There is a special case, for try-catch, when this variable is initialized by a method, an exception may be thrown, then this variable must be initialized inside the try block. But if this variable needs to be used outside the try block, then the variable needs to be declared in advance and meaningfully initialized in the try block.

Loops provide a special opportunity to minimize the scope of the variable or even not (for-each for example), the main purpose is that other blocks will not be mistakenly called to variables that only act on itself.

3. Use native methods carefully-Effective Java Article 54

Java Native Interface (JNI) allows Java applications to call native methods. Native methods refer to methods written in other languages ​​such as c or c++.

The current uses of native methods are mainly concentrated in 1. Native methods provide the ability to access legacy code libraries. Many early library functions are written in C or C++, and you need to use them once you want to access them. 2. The calculation speed of C and C++ is faster than that of Java, so many complex algorithms are implemented by C or C++, and the calling algorithm needs to use local methods. 3. Storing some data in the c-layer code can increase the difficulty of cracking by malicious users. In the current job, there may be more contacts in the next two days.

In addition to the above advantages, the local method has some serious disadvantages. Because C++ and so on are platform-related languages, there is no virtual machine as an intermediate platform, so the portability of software using native methods is poor. At the same time, it is more difficult to debug the local method. Therefore, if it is not similar to calling an algorithm that is not implemented by java or other necessary scenarios, it is not recommended to use JNI to call local methods.

 

Guess you like

Origin blog.csdn.net/weixin_48968045/article/details/107970526