Java based interfaces and internal classes

Interface and inner class

This paper mainly sorts out the difficulties encountered by some authors in Chapter 6 of Java core technology volume and their thinking. Welcome to point out the mistakes in time!

1. Lambda expression

1. About lazy calculation

In java8, the supplier interface is provided to implement lazy computing

The principle is based on the following three principles

We can observe objects Requirenonull method will throw an exception when the parameter is null. The content of the exception is related to the second parameter we passed

This method has three overloads, and we mainly focus on the overloads of two methods

public static <T> T requireNonNull(T obj,String message) {
    if (obj == null)
        throw new NullPointerException(message);
    return obj;
}

Here, a message of string type is directly passed in. Although there is nothing wrong with this, imagine that if our null is not a frequent result, and our string is obtained by calling a method, so every time we perform non empty judgment, we will call the method we write to the message, for example, we pass in a time

new LocalDate(1970,1,1);

In this way, if a large number of processes call this judgment and there are not so many nulls, it will cause performance waste

public static <T> T requireNonNull(T obj,supplier<String> messagesupplier) {
    if (obj == null)
        throw new NullPointerException(messagesupplier.get());
    return obj;
}

Different from the above, the second parameter here is a supplier interface. We can know from the first point that since the interface has only one abstract method, it is a functional interface, and we can use lambda expression; According to our second point, the lambda expression will be executed only when it is called. If we don't have so many empty judgments, this method will not be executed. When our second parameter is very complex (for example, to query data from the database), we can save a lot of performance. We can write the lambda expression of the second parameter like this

() -> new LocalDate(1970,1)

Similarly, the optimization method similar to the design idea of lazy computing is lazy loading, that is, the elements of the page (such as pictures or videos) are loaded only when they are called (such as when we turn the page down), which greatly relieves the pressure of the server and the network. After all, not everyone will see to the end

2. Predict interface

@FunctionalInterface
public interface Predicate<T> {

    boolean test(T t);

    default Predicate<T> and(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) && other.test(t);
    }

    default Predicate<T> negate() {
        return (t) -> !test(t);
    }
    
    default Predicate<T> or(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) || other.test(t);
    }

    static <T> Predicate<T> isEqual(Object targetRef) {
        return (null == targetRef)
                ? Objects::isNull
                : object -> targetRef.equals(object);
    }

It can be seen that this interface also has only one abstract method, so it is also a functional interface. This functional interface is very useful because it can return a Boolean value, which can be judged when we pass in a method

3. About method reference

s -> s.length() == 0

4. About constructor references

5. Scope of variables

2. Internal class

1. Local internal class

2. Anonymous inner class

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>