Book Image

Java 11 and 12 ??? New Features

By : Mala Gupta
Book Image

Java 11 and 12 ??? New Features

By: Mala Gupta

Overview of this book

With its new six-monthly release cadence, Java is moving forward faster. In addition to planned version releases, a lot of work is currently being undertaken on various Java projects at Oracle. In order to make best use of the new features in their applications and libraries, you must be well-versed with the most recent advancements. Java 11 and 12 – New Features will take you through the latest developments in Java, right from variable type inference and simplified multithreading through to performance improvements, which are covered in depth to help you make your applications more efficient. This book explains the relevance and applicability of Java's new features, and answers your questions on whether to invest in migrating to new Java versions and when to migrate. You'll also get to grips with platform features, such as AppCDS and new garbage collectors, to tune and optimize your application—from reduced launch time and latency to improved performance and throughput. By the end of this book, you will be equipped with a thorough understanding of the new features of Java 11, 12, and Project Amber, and possess the skills to apply them with a view to improving your application's performance.
Table of Contents (23 chapters)
Free Chapter
1
Section 1: JDK 10
6
Section 2: JDK 11
13
Section 3: JDK 12
16
Section 4: Project Amber

Defining local variables in a switch branch

It is possible to define variables that are local to a switch branch. To do so, a switch branch can define a block of code to execute for a matching case label. To return the value from the branch, it can include a break statement specifying the value to return.

Let's modify the code in the preceding example to define a code block, as follows:

class Planet { 
    private static long damage; 
    public void use(SingleUsePlastic plastic) { 
        damage += switch(plastic) { 
            case STRAW -> 10; 
            case BAG -> 11; 
            case SPOON, FORK, KNIFE -> 7; 
            case PLATE -> { 
                                int radius = 20;  // Local variable           
break (radius < 10 ? 15 : 20); // Using
...