Code Monkey home page Code Monkey logo

playground's Introduction

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

playground's People

Contributors

apulbere avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.