Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 9, 2023 07:06 am GMT

JDK 8 -> 20

Java 8 2014 Java 8 Java JVM Lambda Optional Stearm LocalDateTime

2023321Java 20LTS)Java 17LTSJava 8Java 2011Java 17

Table Of Contents

  • JDK 9
  • JDK 10
  • JDK 14
    • Switch
    • NPE
  • JDK 15
    • ZGC:
  • JDK 16
    • instanceof
    • Record
  • JDK 17
    • Switch ()
  • JDK 18
    • UTF-8

JDK 9

JEPS-269 : Convenience Factory Methods for Collections

JDK 9 Collection FactoriesList, SetMapImmutable Collection

JDK 8 :

Set<String> mySet = new HashSet<String>();mySet.add("yun");mySet.add("yyx");mySet.add("andy");mySet = Collections.unmodifiableSet(mySet);

JDK 9

Set<String> mySet = Set.of("yun", "yyx", "andy");

JDK 10

JEPS-286 : Local-Variable Type Inference

Javascript varsyntax JDK 10 var Javascript var Java var


String str = "https://dev.to/";URL url = new URL(str);URLConnection con = url.openConnection();

JDK 10

var str = "https://dev.to/";var url = new URL(str);var con = url.openConnection();

var Java var

var flag = 0; // intshortbytevar items = new ArrayList<>(); // ArrayList<Object>

OpenJDKLVTI

JDK 14

Switch

JEPS-361: Switch Expression

Switch JDK 12 JDK 14->yeildswitch


String season;switch(month){    case MAR:    case APR:    case MAY:        season = "spring";        break;    case JUN:    case JUL:    case AUG:        season = "summer";        break;    case SEP:    case OCT:    case NOV:        season = "autumn";        break;    case DEC:    case JAN:    case FEB:        season = "winter";        break;    default:        throw new IllegalArgumentException("Not a month: " + month);}return season;

Switch

return switch(month){    case MAR, APR, MAY -> "spring";    case JUN, JUL, AUG -> "summer";    case SEP, OCT, NOV -> "autumn";    case DEC, JAN, FEB -> "winter";}

NPE

JEPS-358: Helpful NPE

JDK 14 NullPointerException(NPE)null JDK 14 exception message null

bnull

a.i = b.j;

JDK 14

Exception in thread "main" java.lang.NullPointerException    at App.main(App.java:5)

JDK 14

Exception in thread "main" java.lang.NullPointerException:     Cannot read field "j" because "b" is null    at App.main(App.java:5)

JDK 15

JEPS-378: Text Blocks

Java JDK 15


String json = "{
"
+ "\"name\": \"" + user.name + "\",
"
+ "\"nickname\": \"" + user.nickName + "\"
"
+ "}";

JDK 15

var json = """        {            "name": "%s",            "nickname": "%s"        }        """.formatted(user.name, user.nickName);

ZGC:

JEPS-377: A Scalable Low-Latency Garbage Collector

JDK 15ZGC SPEC (Standard Performance Evaluation Corporation) ZGC

Image description

JDK

Image description

ZGC G1GC GC GC Java G1GC ZGC GC Java Java GC GC GC Java

JDK 16

instanceof

JEPS-394: Pattern Matching for instanceof

Pattern Matching instanceof


if( obj instanceof String){ // 1)  obj     // 2) declare  String  str3)  cast obj  String     String str = (String) obj;     //  str }

3 1

if( obj instanceof String str ){    //  str }

Record

JEPS-395: Records

JDK 16 record Java LombokrecordLombok@DatahashcodeequalstoString recordfinal classfinalrecordgetter Record Immutable

JDK 16

final class Point {    private final int x;    private final int y;    Point(int x, int y) {        this.x = x;        this.y = y;    }    int x() { return x; }    int y() { return y; }    public boolean equals(Object o) {        if (!(o instanceof Point)) return false;        Point other = (Point) o;        return other.x == x && other.y == y;    }    public int hashCode() {        return Objects.hash(x, y);    }    public String toString() {        return String.format("Point[x=%d, y=%d]", x, y);    }}

Lombok

@Getter@ToString@EqualsAndHashCode@AllArgsConstructorfinal class Point{    final int x;    final int y;}

JDK 16 record

record Point(int x, int y) { }

JDK 17

Java 17 LTS

JEPS-409: Sealed Classes

sealed classinterfacefinalsealedsealedfinalsealednon-sealed


public abstract sealed class Shape    permits Circle, Rectangle, Square, WeirdShape { ... }public final class Circle extends Shape { ... }public sealed class Rectangle extends Shape     permits TransparentRectangle, FilledRectangle { ... }public final class TransparentRectangle extends Rectangle { ... }public final class FilledRectangle extends Rectangle { ... }public final class Square extends Shape { ... }public non-sealed class WeirdShape extends Shape { ... }

Switch ()

JEPS-406: Pattern Matching for switch (Preview)

JDK 16 instanceof JDK 17 switchswitch

instanceof

static String formatter(Object o) {    String formatted = "unknown";    if (o instanceof Integer i) {        formatted = String.format("int %d", i);    } else if (o instanceof Long l) {        formatted = String.format("long %d", l);    } else if (o instanceof Double d) {        formatted = String.format("double %f", d);    } else if (o instanceof String s) {        formatted = String.format("String %s", s);    }    return formatted;}

switch

static String formatterPatternSwitch(Object o) {    return switch (o) {        case Integer i -> String.format("int %d", i);        case Long l    -> String.format("long %d", l);        case Double d  -> String.format("double %f", d);        case String s  -> String.format("String %s", s);        default        -> o.toString();    };}

JDK 18

UTF-8

JEPS-400: UTF-8 by Default

JDK 18 UTF-8 APIs

JDK 17 readability
JDK 8 JDK 17 GC GC Serial GC G1GC


https://www.youtube.com/watch?v=P7SI9mLwiqw


Original Link: https://dev.to/andylow/jdk-8-20-zui-zhong-yong-de-xin-zeng-te-xing-36jc

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To