Java 15 Features – JournalDev

Keeping up with the six-month hertz tradition, after the release of Java 14 on March 17, 2020, we now have Java 15, the adjacent non-LTS version rolling out on September 15, 2020 .

Java 15 Features

here ’ s a quick spirit at features that are a share of Java 15 :

  • Sealed Classes (Preview) – JEP 360
  • Pattern Matching for instanceof (Second Preview) – JEP 375
  • Records (Second Preview) – JEP 359
  • Text Blocks (Standard) – JEP 378
  • Hidden Classes – JEP 371
  • Remove the Nashorn JavaScript Engine – JEP 372
  • Reimplement the Legacy DatagramSocket API – JEP 373
  • Disable and Deprecate Biased Locking – JEP 374
  • Shenandoah: A Low-Pause-Time Garbage Collector – JEP 379
  • Remove the Solaris and SPARC Ports – JEP 381
  • Foreign-Memory Access API (Second Incubator) – JEP 383
  • Deprecate RMI Activation for Removal – JEP 385

Java 15 Installation Setup on Mac OS

  • To get started with Java 15, download the JDK from here.
  • Copy and extract the tar file in the /Library/Java/JavaVirtualMachines as shown below:
$ cd /Library/Java/JavaVirtualMachines

$ sudo cp ~/Downloads/openjdk-15_osx-x64_bin.tar.gz /Library/Java/JavaVirtualMachines

$ sudo tar xzf openjdk-15_osx-x64_bin.tar.gz

$ sudo rm openjdk-15_osx-x64_bin.tar.gz

once that ’ s done, open the bash_profile using any textbook editor. I ’ thousand using vim ~/.bash_profile. Set the way of Java15 to JAVA_HOME, save changes and do a source ~/.bash_profile to reflect the changes .

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

ultimately, you ’ re ready to compile and run programs using Java 15. We ’ ll be using JShell, an interactional REPL command-line instrument for cursorily testing the new Java 15 features. It ’ second important to note that many features released in Java 15 are in preview. This means that though they ’ re in full working right now, things may be modified in the future. Some could be made a standard or just removed in the future release cycles. In order to test preview features, you need to explicitly set --enable-preview when running the JShell or Java Program as shown below :

jshell --enable-preview

javac --release 15 --enable-preview

In the adjacent few sections, let ’ s discuss the significant terminology changes in Java 15.

1. Sealed Classes (Preview)

Sealed classes have been there in Kotlin since a while and Java 15 finally introduces this feature for better control over inheritance. As the list suggests, Sealed classes let you restrict or permit class hierarchies to only certain types. This is fabulously useful for traffic pattern match as you have a specific number of classes to switch between. The take after syntax defines a seal course in Java 15 :

public sealed class Vehicle permits Car, Bike, Truck {

so, the above code means, entirely the classes defined after the keyword permits are allowed to extend the Vehicle sealed class. In case, you ’ ve defined the classes Car, Bike and Truck in the same file as Vehicle, you can omit the keyword permits and the compiler will take manage of it implicitly as shown below :

sealed class Vehicle {...}

final class Car extends Vehicle {...}
final class Bike extends Vehicle {...}
final class Truck extends Vehicle {...}

As you can see above, we ’ ve defined the concluding modifier of each of the classes. now, that ’ s an important dominion of sealed classify that you need to keep in mind : Every permit class must be set with an denotative modifier. It can either be final or sealed or non-sealed. here ’ s how each of the modifiers impact inheritance :

  • A permitted subclass that’s declared final cannot be extended further.
  • A permitted subclass that’s declared sealed can be extended further but only by classes that are permitted by the subclass.
  • A permitted subclass may be declared non-sealed can be extended further by any class. The superclass cannot restrict the subclasses further down this class hierarchy.

Before Java 15, developers could alone use final keyword or oscilloscope modifiers to control inheritance. thus, sealed classes brings the lend tractability for Java developers when defining course hierarchies. Java ’ s Reflection API besides gets two raw methods for dealing with varnish classes :

java.lang.constant.ClassDesc[] getPermittedSubclasses();

boolean isSealed()

2. Records (Second Preview)

Records were introduced as a preview feature of speech in Java 14, in an campaign to reduce boilerplate code when writing POJO based data aircraft carrier classes. This is something that ’ randomness been there in Kotlin for long in the form of data classes. now, with Java 15, Records get their second preview. While there aren ’ deoxythymidine monophosphate any major changes ( precisely some child additions ), placid there are a few major clarifications and restrictions that you should know :

  • Prior to Java 15, one could declare native methods in records(though it wasn’t a good idea). Now the JEP explicitly prohibits against declaring native methods in records. Understandably, defining a native method steals away the USP of records by bringing an external state dependency.
  • The implicitly declared fields corresponding to the record components of a record class are final and should not be modified via reflection now as it will throw IllegalAccessException.

Records are meant to be data carrier wave classes and you should wholly avoid defining native methods in them .

Records with Sealed Types

We know that records are final and can not be extended. gladly, records can implement interfaces. So you can define a sealed interface and implement them on your records in the follow ways :

sealed interface Car permits BMW, Audi { ... }

record BMW(int price) implements Car { ... }

record Audi(int price, String model) implements Car { ... }

Local Records

Records can besides be defined within methods to store average values. Unlike local classes, a local record is implicitly inactive. This means they can not access variables and exemplify members of the enclosing methods which is actually big since it prevents capturing of values by the record. local records are a great boon for Java developers who would early have to create assistant records. See how the introduction of a local records helps perform calculation of values in a stream API with the comply method acting :

List findTopMerchants(List merchants, int month) {
    // Local record
    record MerchantSales(Merchant merchant, double sales) {}

        .map(merchant -> new MerchantSales(merchant, computeSales(merchant, month)))
        .sorted((m1, m2) ->, m1.sales()))


While the above two were the two major language features in Java 15, we besides have Pattern Matching in the moment preview for user feedback, Text Blocks as a standard feature now and most importantly a raw Hidden classes have.

Hidden classes are a JVM feature which is relevant for framework developers. It allows class implementations to be made non-discoverable by defining them using Lookup::defineHiddenClass. In doing so, such classes can neither be found using Class.forName nor by referring them in bytecode. These are the major changes introduced in Java 15 .

source :
Category : interview

We will be happy to hear your thoughts

Leave a reply

GauDay Crypto news and market tracking in real time
Enable registration in settings - general