Overview of new features - enumerations, annotations, Lambda expressions, method references

Keywords: Programming Lambda Java JDK

1. Enumeration (actually multiple cases)
Enumerations in java are defined using enum, which is not a new structure. Enumerations are defined using enum
The Enum enumeration class is actually inherited by default.So the enum structure defined by enum is actually a multiple-instance class.

Three commonly used methods for 1.1Enum enumeration classes:

  • ordinal(): Returns the enumerated object subscript, defaulting to 0 for the first object number;
  • name(): Returns the enumerated object name, defaulting to the same object name
  • values(): Returns all enumerated objects

Example: Use of Enum enumeration class methods

enum Color2{
    RED,YELLOW,BLUE;
}
public class UseEnumTest {
    public static void main(String[] args) {
        System.out.println(Color2.BLUE);
        System.out.println(Color2.BLUE.ordinal()+"="+Color2.BLUE.name());
        for(Color2 colors2:Color2.values()){
            System.out.println(colors2);
        }
    }
}

1.2 Enumerations support defining other structures, such as construction methods, common attributes, and common methods
1.3 When an enumeration class contains other structures, the definition of the enumeration object must be placed on the first line
1.4 enumeration implements interfaces, so that each object in the enumeration becomes an interface object

Example: Multiple Case Patterns

package javaSE.bit.EnumClassTest;

/**
 * Multiple Case Mode
 * @author mayanni
 * @date 2019-04-28 17:50
 */
class Color{
    private String name;
    private static Color RED=new Color("red");
    private static Color BLUE=new Color("blue");
    private static Color YELLOW=new Color("yellow");
    private Color(String name){this.name=name;}
    public static Color getInstance(String colorName){
        switch (colorName){
            case "RED":
                return RED;
            case "BLUE":
                return BLUE;
            case "YELLOW":
                return YELLOW;
            default:
                return null;
        }
    }
    public String toString(){
        return this.name;
    }
}
public class Test {
    public static void main(String[] args) {
        Color red=Color.getInstance("RED");
        Color blue=Color.getInstance("BLUE");
        System.out.println(red);
        System.out.println(blue);

    }
}

2. Notes
Three notes built into JDK
2.1 @Override accurate override:
Check that the current override method meets the requirements during compilation (method name is correct, permissions are reasonable)
2.2 @Deprecated Expiration Statement:
In earlier versions of the code (method), this means that the method is not recommended in the current version, but it is used without error.
Just as a warning reminder
2.3 @SuppressWarnings Suppression Warning
Suppress warning message, suppress warning message no longer prompted

  1. JDK1.8 Interface

Earlier versions (before jdk1.8) of the interface already have many subclasses implemented, at which point it is found that the methods in the interface need to be changed or even added, and the interface cannot be modified (global constant + Abstract method)
Extending the interface: normal and static methods that support default definitions
I. Use a common method defined by default, called through subclass objects, and all subclasses have this method. Default cannot be omitted at this time
II. Use static methods defined by static to call directly through the interface name.

4.Lambda Expression (Functional Programming)
Using Lambda expressions requires that there is only one abstract method in the interface.
JDK1.8 new annotation @FunctionalInterface can only be used for interface declarations to detect if the current interface has only one abstract method
4.1 Method body implementation has only one line
() ->Specific implementation code;

4.2 Method body has multiple line implementations
( )-> {
Method Body Implementation Code
}

4.3 Method if return value:
a. Only one line of code, return can be omitted

b. Method body has multiple lines of code, return cannot be omitted at this time

5. Method references:
Usually used in conjunction with Lambda expressions; refers to a specific implementation of the method
5.1 Reference to static methods of a class
valueOf() method in String class

ISubject<Integer,String > is=String :: valueOf;//Make method references
String str=is.switchPara(1000000);//Equivalent to String.valueOf() method
System.out.println(str);

5.2 Common methods of referencing an object
The toUpperCase() method in String is the object method

ISubject<String> is = "hello"::toUpperCase;
System.out.println(is.switchPara());

5.3 Reference to a common method of a class
The compareTo() method in the String class, which is a common method

ISubject<Intgeter,String> is = String :: compareTo;
System.out.println(is.switchPara("Horse","Yang"));

5.4 Reference to a class's construction method

class Person {
   private String name ;
   private int age ;
   public Person(String name, int age) {
      super();
      this.name = name;
      this.age = age;
   }
   @Override
   public String toString() {
      return "Person [name=" + name + ", age=" + age + "]";
   }
}
@FunctionalInterface // Is a functional programming interface that allows only one method
interface IUtil<R,PN,PA> {
   public R createPerson(PN p1,PA p2) ;
}
public class TestDemo {
   public static void main(String[] args) {
      IUtil<Person,String,Integer> iu = Person :: new;
      System.out.println(iu.createPerson("yuisama", 25)); // Equivalent to calling the construction method of Person class
}
}

Posted by frankstr on Sun, 05 May 2019 10:40:37 -0700