Java 15 and IntelliJ IDEA

Java 15 introduced sealed classes and sealed interfaces , with the help of which it became possible to constrain the hierarchy of classes and interfaces at the level of language syntax. Possible hierarchies are now defined declaratively. This functionality is still presented in preview mode





Also in Java 15, there are changes to Records that were introduced in Java 14. And pattern matching for instanceof came in Java 15 as a second preview unchanged. Text blocks from Java 13 are included in Java 15 as a standard language construct. There are no changes in them compared to Java 14. 





In this article, I'll walk you through all of the new and updated Java 15 language constructs, how they can be useful to you, and how to use them in IntelliJ IDEA. Let's start. 





Sealed classes and interfaces

sealed, , . , , , —  . ?





, . , , . ( ): 





class Plant {}
class Herb extends Plant {}
class Shrub extends Plant {}
class Climber extends Plant{}
class Cucumber extends Climber {}
      
      



, Gardener



() : 





public class Gardener {
   int process(Plant plant) {
       if (plant instanceof Cucumber) {
           return harvestCucumber();
       } else if (plant instanceof Climber) {
           return sowClimber();
       } else if (plant instanceof Herb) {
           return sellHerb();
       } else if (plant instanceof Shrub) {
           return pruneShrub();
       } else {
           System.out.println("Unreachable CODE. Unknown Plant type");
           return 0;
       }
   }

   private int pruneShrub() { .. }
   private int sellHerb() { .. }
   private int sowClimber() { .. }
   private int harvestCucumber() { .. }
}
      
      



, else , . Sealed- . 





sealed-

sealed- sealed



. , , permits



. final



, non-sealed



sealed



.





Gif' , sealed- :





:





sealed public class Plant permits Herb, Shrub, Climber {
}

final class Herb extends Plant {}
non-sealed class Shrub extends Plant {}
sealed class Climber extends Plant permits Cucumber{}

final class Cucumber extends Climber {}
      
      



, (accessibility) (extensibility). sealed- , . , , package-private . . sealed- .





permitted- (reflection) Class.permittedSubclasses()



. sealed- .





IntelliJ IDEA, , .





IntelliJ IDEA

Java 15 IntelliJ IDEA 2020.2, 2020 . Java 15 "Project SDK" "15", "Project language level" — "15 (Preview) – Sealed types, records, patterns, local enums and interfaces".





Java 15 IntelliJ IDEA. "Project Structure" "Platform Settings" "SDKs", "+" "Download JDK". (Vendor), (Version) JDK.





Plant Gardener

sealed- - . else



process()



Gardener



. - else



- return



.





, Java 14 instanceof, Java switch



. switch



. " " , Plant



:





//      Java 15.
//       Java 
//   type-test-pattern  switch 
int processInAFutureJavaVersion(Plant plant) {
   return switch (plant) {
       case Cucumber c -> c.harvestCucumber();
       case Climber cl -> cl.sowClimber();
       case Herb h -> h.sellHerb();
       case Shrub s -> s.pruneShrub();
   }
}
      
      



Sealed- .   sealed- , . .





sealed-, , .





, sealed-, final



, non-sealed



sealed



. final



, non-sealed



, sealed- , — , .





Sealed- . , .





, , Plant



grow()



. Herb



final-, grow()



. Non-sealed Shrub



grow()



. Sealed- Climber



grow()



:





:





sealed abstract public class Plant permits Herb, Shrub, Climber {
   abstract void grow();
}
final class Herb extends Plant {
   @Override
   void grow() {
   }
}
non-sealed abstract class Shrub extends Plant {}
sealed class Climber extends Plant permits Cucumber{
   @Override
   void grow() {
   }
}
final class Cucumber extends Climber {}
      
      



sealed- , permits



, sealed-. .





Sealed-

Sealed- , , ( ), . , sealed-.





final



( , ) sealed



non-sealed



. permits , sealed-, , . final, sealed non-sealed. , Java 14, final, - :





sealed public interface Move permits Athlete, Person, Jump, Kick {
}
final class Athlete implements Move {}
record Person(String name, int age) implements Move {}
non-sealed interface Jump extends Move {}
sealed interface Kick extends Move permits Karate {}
final class Karate implements Kick {}
      
      



Java 15 – (record).





(records)

(records) - (value object). Java 14, Java 15 — .





IntelliJ IDEA,   Java 14 IntelliJ IDEA. IntelliJ IDEA , . 





Java 15 Java 14.





Java 15 . getTopPerformingStocks()



(stock), , .





List<String> getTopPerformingStocks(List<Stock> allStocks, LocalDate date) {
   // TopStock -   (Record)
   record TopStock(Stock stock, double stockValue) {}
   return allStocks.stream()
              .map(s -> new TopStock(s, getStockValue(s, date)))
              .sorted((s1, s2) -> Double.compare(s1.stockValue(), s2.stockValue()))
              .limit(2)
              .map(s -> s.stock.getName())
              .collect(Collectors.toList());
}
      
      



Java 15 . -.





public void createLocalInterface() {
   interface LocalInterface {
       void aMethod();
   }
   // ,  LocalInterface
}

public void createLocalEnum() {
   enum Color {RED, YELLOW, BLUE}
   // ,  enum Color
}
      
      



. , FOO



BAR



:





void test(int input) {
   enum Data {
       FOO(input), BAR(input*2); // .    input
       private final int i;
       Data(int i) {
           this.i = i;
       }
   }
}
      
      



(pattern matching) instanceof

Java- instanceof



. true, , . : - ifTrue - .





Java 14 instanceof



. , .





instanceof



( Java 14 ).





IntelliJ IDEA Java 14 IntelliJ IDEA. IntelliJ IDEA, if .





Java 15 - Java 14.





IntelliJ IDEA Java 14 IntelliJ IDEA.





Sealed- Java 15 . . . , .





, , , , , Java . API, (deprecated). - , JDK ( ).





IntelliJ IDEA Java JDK. , . , Java SE, . , Switch Expressions Java 12 break , yield



. break



Switch Expressions IntelliJ IDEA.





IntelliJ IDEA Java, .





IntelliJ IDEA 2020.2 Java 15. sealed- , (record), (pattern matching) instanceof



(text block). IntelliJ IDEA .





. IntelliJ IDEA.






OTUS Java Developer. Professional. demo day , , OTUS.





  • DEMO DAY








All Articles