• Home
  • About
    • Thoughts To Pen photo

      Thoughts To Pen

      My thoughts on Computer Programming || Psychology || Personal Finances || & much more...

    • Learn More
    • Twitter
    • Instagram
    • Github
    • StackOverflow
  • Posts
    • All Posts
    • All Tags
  • Projects
  • Portfolio
  • Resources
  • About

Java 21 Features

07 Oct 2023

  • String Templates (Preview Feature)

    String Templates allow us to embed expressions (variables, methods or fields) into Strings, which are evaluated at runtime.

     private static String name = "T2P";
      private static String message = STR."Greetings from \{name}!";	//Greetings from T2P!
    
  • Virtual Threads

    Virtual Threads are JVM managed lightweight threads. Lighter than the traditional Threads.

    Executors.newVirtualThreadPerTaskExecutor();
    
  • Sequenced Collections

    New interface Sequenced Collection is introduced in the Collections framework which provides new functionality of defined encounter order, which means, we will have a well-defined order for first element, second element, upto the last element.

      List<Integer> integerList = new ArrayList<>();
      integerList.add(100); //[100]
    	
      integerList.addFirst(50); //[50,100]
      integerList.addLast(150); //[50,100,150]
    	
      integerList.getFirst(); //50
      integerList.getLast(); //150
    
  • Record Patterns

    Record Patterns allow you to deconstruct (unpack) record values directly in pattern matching. This eliminates the need for casting and calling accessor methods, making code more concise and readable.

      record Point(int x, int y) {}
        
      if (obj instanceof Point(int x, int y)) {
          System.out.println(x+y);
      }
    
  • Pattern Matching for switch

    This feature enhances the switch statement to support patterns in case labels. It allows testing against types (replacing if-else chains), handling null correctly, and using guards (when) for refined matching.

      return switch (obj) {
          case Integer i -> String.format("int %d", i);
          case String s  -> String.format("String %s", s);
          default        -> obj.toString();
      };
    
  • Unnamed Classes and Instance Main Methods (Preview)

    Designed properly for beginners, this feature reduces the “Hello World” boilerplate. You can write programs without explicitly declaring a class, detailed main method signature, or access modifiers.

      void main() {
          System.out.println("Hello, World!");
      }
    
  • Structured Concurrency (Preview)

    If you use Virtual Threads, this feature is for you. It simplifies concurrent code by treating groups of related tasks as a single unit. If one sub-task fails, the others are automatically cleaned up, preventing resource leaks and making error handling robust.

      try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {
          scope.fork(() -> findUser());
          scope.fork(() -> fetchOrders());
          scope.join(); 
          // ...
      }
    
  • Unnamed Patterns and Variables (Preview)

    Allows you to use the underscore _ to denote unused variables or record components. This removes clutter from your code and makes your intent clear (“I am intentionally ignoring this value”).

      if (obj instanceof Point(int x, _)) {
          System.out.println("X is " + x);
      }
        
      try { ... } catch (Exception _) { ... }
    


programmingjavajava21 Share Tweet Msg