Skip to content

apulbere/playground

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

About

Java 12+ features

Prerequisites

  • JDK 12 Early-Access Build, JDK 12

[since Java 11]

Enhance the java launcher to run a program supplied as a single file of Java source code, including usage from within a script by means of "shebang" files and related techniques.

chmod +x src/java-shebang-example
./src/java-shebang-example

Extend the switch statement so that it can be used as either a statement or an expression, and that both forms can use either a "traditional" or "simplified" scoping and control flow behavior.

javac command is optional due to JEP-330

javac --source 12 --enable-preview src/SwitchPreview.java

java --source 12 --enable-preview src/SwitchPreview.java

Teeing Collector

collector which merges results of two other collectors JDK-8209685

java src/TeeingCollectorProblem.java 

Stream.of('5', 't', 'o', '9', 'p', '1', 'h')
        .collect(Collectors.teeing(
                Collectors.filtering(Character::isLetter, Collectors.toList()),
                Collectors.filtering(not(Character::isLetter), Collectors.toList()),
                List::of
        ));

[why it was drpped], [restarting the discussion]

change java to 12 EA (as JEP 326 was removed in final version)

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-12.jdk/Contents/Home

run

java --source 12 --enable-preview src/RawStringLiteralsPreview.java

[detailed doc on pattern matching]

Enhance the Java programming language with pattern matching for the instanceof operator.

if (obj instanceof String str) {
    str.trim();
} else {
    // can't use str here
}

@Override 
public boolean equals(Object o) { 
    return (o instanceof CaseInsensitiveString cis)
        && cis.equalsIgnoreCase(s); 
}

Future work: enhance the Java programming language with pattern matching for other language constructs, such as

String formatted =
    switch (obj) {
        case Integer i -> String.format("int %d", i); 
        case Byte b    -> String.format("byte %d", b); 
        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        -> String.format("Object %s", obj);
    };

BiFunction<Integer, String, String> biss = (i, _) -> String.valueOf(i);
  • Shadowing of lambda parameters
Map<String, Integer> msi = new HashMap<>();

String key = computeSomeKey();
msi.computeIfAbsent(key, key -> key.length()) //error
  • Better disambiguation for functional expression
class Foo {
   static boolean g(String s) { return false }
   static boolean g(Integer i) { return false }
}

m(Foo::g) //ambiguous

JDK-8220715

This JEP proposes to enhance the exception text to tell what was null and which action failed.

source code: a.to_b.to_c.to_d.num = 99 // a is null
  thrown msg: 'a' is null. Can not read field 'to_b'

source code: nullInstanceField.testNullMessages()
  thrown msg: 'this.nullInstanceField' is null. Can not invoke method 'NullPointerExceptionTest.testNullMessages()V'.
  
source code: a.getB().getBfromB().getC().getD().num = 99 // a.getB().getBfromB().getC().getD() is null
  thrown msg: The return value of 'NullPointerExceptionTest$C.getD()LNullPointerExceptionTest$D;' is null. Can not write field 'num'.

Others

About

testing new java features

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages