Richard Pajerski Software development and consulting

Three easy-to-use Java 17 features in Notes/Domino 14

by Richard Pajerski


Posted on Tuesday December 19, 2023 at 03:14PM in Technology


As I mentioned previously, having Java 17 available with the latest release of Notes/Domino has brought welcome changes for Java developers.  Just to scratch the surface a bit, here are three language-level niceties that we can immediately benefit from:


1.  Simplified access to file contents
2.  Local-variable type inference (var identifier)
3.  Text blocks


1.  Simplified access to file contents
Java has been (in)famous for offering a number of ways to open/read and write/close files.  As of Java 11, perhaps the simplest way to get text out of a file is:

Files.readString(Path p);



A two-liner in a local Java agent on a Windows 11 client:


import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import lotus.domino.*;

public class JavaAgent extends AgentBase {

    public void NotesMain() {

      try {

          Path p = Paths.get("C:\\Windows\\System32\\drivers\\etc\\hosts");
          System.out.println(Files.readString(p)); 
      } catch(Exception e) {
          e.printStackTrace();
      }
   }
}

Javadoc:  https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/nio/file/Files.html#readString(java.nio.file.Path)


2.  Local-variable type inference (var identifier)

The var type identifier has been available since Java 10 and is intended to help reduce "boilerplate" code:

import lotus.domino.*;

public class JavaAgent extends AgentBase {

    public void NotesMain() {

      try {

         var s = getSession();
         var db = s.getCurrentDatabase();
         System.out.println("Db title: " + db.getTitle());

                /* Contrast:
                     Session s = getSession();
                     Database db = s.getCurrentDatabase();
                   */

      } catch(Exception e) {
          e.printStackTrace();
      }
   }
}

More: Using the Var Type Identifier


3.  Text blocks
In Java 15, Text Blocks were introduced.  In a more developed application, it's probably a better idea to store strings and text outside of source code, especially if they need to be regularly modified.  But using text blocks can be a time saver for quick testing, demos, etc. and provide improved source readability.

import lotus.domino.*;

public class JavaAgent extends AgentBase {

    public void NotesMain() {

      try {

         var textBlock = """

                Using text blocks in Java source

                code simplifies text formatting

                and improves readability!

                """;


         System.out.println(textBlock);


      } catch(Exception e) {
          e.printStackTrace();
      }
   }
}


Note: To use these features, remember to adjust your compiler levels appropriately:



No one has commented yet.

Leave a Comment

HTML Syntax: Allowed