3.3.2021 |

Java15

Text Blocks (JEP 368)

private static final String request = """
            {
            "name": "field are 0.05ha",
            "geometry": {
                "type": "Polygon",
                "coordinates": [[
                    [ 9.78480577468872, 52.151607969506735],
                    [ 9.785256385803223, 52.151607969506735],
                    [ 9.785256385803223, 52.151779125539825],
                    [ 9.78480577468872, 52.151779125539825],
                    [ 9.78480577468872, 52.151607969506735]
                ]]
            },
            "farmId": "23328c83-857a-4aba-8949-bae2e3447c86",
            "country": "DE",
            "areaSquareMeters": 586,
            "creatingTool": "DRYMATTER",
            "activeTools": ["FIELD_MANAGEMENT", "VARIABLE_RATE_SOWING"]
        }
        """;
String aaName = "Pat Q. Smith";

String bbName = """
                Pat Q. Smith""";

aaName.equals(bbName) // true
// ORIGINAL
String message = "'The time has come,' the Walrus said,\n" +
                 "'To talk of many things:\n" +
                 "Of shoes -- and ships -- and sealing-wax --\n" +
                 "Of cabbages -- and kings --\n" +
                 "And why the sea is boiling hot --\n" +
                 "And whether pigs have wings.'\n";

// BETTER
String message = """
    'The time has come,' the Walrus said,
    'To talk of many things:
    Of shoes -- and ships -- and sealing-wax --
    Of cabbages -- and kings --
    And why the sea is boiling hot --
    And whether pigs have wings.'
    """;

Switch Expressions (JEP 361)

switch (month) {
          case JANUARY, FEBRUARY, MARCH -> System.out.println("First Quarter");//no break needed
          case APRIL, MAY, JUNE -> System.out.println("Second Quarter");
          case JULY, AUGUST, SEPTEMBER -> System.out.println("Third Quarter");
          case OCTOBER, NOVEMBER, DECEMBER -> System.out.println("Forth Quarter");
          default -> System.out.println("Unknown Quarter");
      }
// using single expressions
String quarter = switch (month) {
          case JANUARY, FEBRUARY, MARCH -> "First Quarter"; //must be a single returning value
          case APRIL, MAY, JUNE -> "Second Quarter";
          case JULY, AUGUST, SEPTEMBER -> "Third Quarter";
          case OCTOBER, NOVEMBER, DECEMBER -> "Forth Quarter";
          default -> "Unknown Quarter";
      };
//using multiple expressions
String result = switch (month) {
          case JANUARY,
                  FEBRUARY,
                  MARCH -> {
              //multiple statements can be used here
              yield "First Quarter";
          }
          case APRIL, MAY, JUNE -> {
              //multiple statements can be used here
              yield "Second Quarter";
          }
          case JULY, AUGUST, SEPTEMBER -> "Third Quarter";
          case OCTOBER, NOVEMBER, DECEMBER -> {
              //multiple statements can be used here
              yield "Forth Quarter";
          }
          default -> "Unknown Quarter";
      };

Records (JEP 384) (PREVIEW Feature)

public record FieldValidateAreaInput(
    Tenant tenant,
    String externalId,

    int fieldAreaSize,
    Set<CultiventTool> cultiventTools
) { 
    public String someLogic() {
        return "test";
    }
}

...

input.fieldAreaSize()
input.cultiventTools()
input.someLogic()

Helpful NullPointerExceptions (JEP 358)

Caused by: java.lang.NullPointerException: Cannot invoke "String.toLowerCase()" because "test" is null
	at com.kws.cultivent.fieldservice.domain.field.validatefieldarea.FieldValidateArea.handle(FieldValidateArea.java:58)
	at org.codehaus.groovy.vmplugin.v7.IndyInterface.selectMethod(IndyInterface.java:234)
	at com.kws.cultivent.fieldservice.domain.field.validatefieldarea.FieldValidateAreaTest.should return when the fieldArea is valid(FieldValidateAreaTest.groovy:35)
Zur Übersicht
Jan Sauer

Mehr vom Devsquad...

Jan Sauer

What would the JavaScript error say?

Rahmi Tufanoglu

Angular - Funktionsaufrufe in Templates vermeiden

Hallo

Wir sind für Sie da und freuen uns auf Ihre Fragen oder Ihr Feedback.

* Pflichtfeld