Java basic syntax (singleton, final, inner class, Lambda, method reference)

1. Singleton mode (lazy man style and hungry man style)

/**
 * 懒汉式单例模式
 */
public class Person {
    // 私有的静态的实例变量
    private static Person instance = null;
    // 构造方法私有化
    private Person() {}
    // 公共的静态方法,返回唯一的那个实例
    public static Person getInstance() {
        if (instance == null) { //这里线程不安全
            instance = new Person();
        }
        return instance;
    }
}

/**
 * 饿汉式单例模式,线程安全
 */
public class Person {
    // 私有的静态的实例变量
    private static Person instance = new Person();
    // 构造方法私有化
    private Person() {}
    // 公共的静态方法,返回唯一的那个实例
    public static Person getInstance() {
        return instance;
    }
}

Two, the final keyword

  • Classes modified by final: cannot be inherited
  • Methods modified by final: cannot be overridden
  • Variables modified by final: can only be assigned once
    // 常量的基本写法:一般名字大写
 
    public static final double PI = 3.1415926;
    public  static final int NOT_FOUND = -1;
    private static final int DEFAULT_SIZE = 10;

3. Internal class 

public class Person { //外部类 ,顶级类
   
    // 非静态嵌套类(内部类)
    class InnerClass {
        public int age = 10;
    }
}
  1. Like instance variables and instance methods, inner classes are associated with instances of outer classes
  2. The outer class instance must be created first, and then the outer class instance is used to create the inner class instance 
  3. Inner classes cannot define any static members other than compile-time constants
  4. The inner class can directly access all members of the outer class (even if it is declared as private)
  5. The outer class can directly access the instance member variables and methods of the inner class (even if it is declared as private)
 public static void main(String[] args) {
        Person person = new Person();
        Person.InnerClass innerClass = person.new InnerClass();
        System.out.println(innerClass.age);
    }

 4. Static nested classes

  • Static nested class: nested class modified by static
  • A static nested class is a top-level class in behavior, but the defined code is written in another class
  • Compared with general top-level classes, static nested classes have some special permissions
  1. You can directly access other members in the external class except instance variables and instance methods (even if they are declared as private)

public class Person { //外部类 ,顶级类
    // 静态嵌套类
    static class StaticClass {

    }
}
 Person.staticClass staticClass = new Person.staticClass(); // 不报错
public class Person { //外部类 ,顶级类
    
    private static int count = 1;
    private static void run() {}
    
    public static class staticClass {
        public void run() {
            Person.count = 2;
            Person.run();
        }
    }
}

Five, Lambda expression

  • Functional Interface (Functional Interface): an interface that contains only 1 abstract method
  • The @FunctionalInterface annotation can be added to the interface to indicate that it is a functional interface
  • An anonymous class has only one abstract method to be shorthanded by lambda
  • The parameter list can omit the parameter type
  • When there is only one statement: braces, semicolons, return can be omitted
  • When there is only one parameter: parentheses can be omitted
  • When there are no parameters: parentheses cannot be omitted
   Integer[] array = {12, 34, 34, 67};
        Arrays.sort(array, (o1, o2) -> {
            return o1 - o2;
        });

    // 只有一条语句的时候
      Arrays.sort(array, (o1, o2) -> o1 - o2);

6. Method Reference 

If the content in Lambda is just to call a certain method, you can use method reference to simplify

Referencing a static method: ClassName::staticMethodName

Refer to an instance method of a specific object: ObjectName::instanceMethodName

Refer to any object instance method of a specific type: ClassName::methodName

@FunctionalInterface
public interface Testable {
    int test(int v1, int v2);
}
       Testable test = new Testable() {
            @Override
            public int test(int v1, int v2) {
                return v1 - v2;
            }
        };
        Testable testable = ((v1, v2) -> v1 - v2);
        Testable testable1 = (v1, v2) -> {
            return Math.max(v1, v2);
        };
        Testable testable2 = (v1, v2) -> Math.max(v1, v2);
        Testable testable3 =  Math::max;
        int result = test.test(12,45);
        System.out.println(result);

Guess you like

Origin blog.csdn.net/weixin_45689945/article/details/127049807