Java has come a long way since Java 8. The most used Java version nowadays is Java 11, which is a Long-Term-Support (LTS) release. After Java 8, Oracle released Java 9 and Java 10, making them Non-Long-Term-Support (Non LTS) releases. What this means is that instead of releasing them as major updates to Java 8, Oracle decided to classify the releases as Non-LTS releases—whereas LTS releases represent major improvements to the language.
In general, LTS releases are not announced within a 3-year period. Even though the current Java version available is Java 16, Java versions 12, 13, 14, 15 and 16 were all Non-LTS releases. Non-LTS releases of Java don’t receive wide acceptance in the Development world, though this may change in the future. This blog post explores some of the features added after Java 8 (though not specifically added in Java 11).
No Need for Javac
Shebang files or Single-file programs are very popular in UNIX-based OS systems and other modern-day languages (e.g., Python, Ruby). These single-file programs are used to create utility functions (e.g., an executable).
Java 11 includes this single-file program feature, which is useful for creating a utility if you’re unfamiliar with Linux-based utilities or scripting. Single-file programming was released as part of “JEP 330: Launch Single-File Source-Code Programs.” (JEP stands for JDK Enhancement Proposals)
Most of us would be using IDEs (e.g., Eclipse or Intellij Idea) which has a built-in compiler. Beginners might not be familiar with a compiler like javac.
Using javac command (e.g., javac <<java-filename>>) to compile the Java program will result in a .class file in the same location as the Java file. The java command that will execute the Java program from the compiled .class file is java <<class-name>>.
With this new feature, we no longer need to use the javac command. You can write your utility in Java and run it, using just java <<class-name>>. The screenshot below illustrates an example.
New String Methods
Java 11 introduced some cool methods, including: repeat (), lines (), strip (). See the documentation and examples below.
Repeat () method takes an integer as count for the number of times a string must be repeated. In Java 8, you may have ended up creating a utility function that will loop through the count and appending the string one by one using arrays. Apart from that you would also have to handle conditions like negative count, out of memory situations, etc. Repeat () will throw IllegalArgumentException if a negative count is passed. Repeat () will throw OutOfMemoryError if “Integer.MAX_VALUE / count < String length”.
Lines () returns a stream of Strings: one per line for a multi-line string. In Java 8 and earlier, you may have created a utility to split a multi-line String using StringReader—which may be error-prone and not a performance-optimized solution.
Strip () removes the empty space in the leading and trailing of a string. In Java 8, we would have used trim () method. However, strip () was implemented with Unicode awareness and an optimized solution compared to trim (). So, strip () is several times faster than trim ().
New Collection Method
A default method toArray () was added to the Collection interface. In Java 8 and earlier, converting a list of elements into an array required a two-step process: one for creating the array with a length of the size of the list, and then invoking the toArray() method to copy the contents from the List to Array. With this new default toArray () method, these steps are reduced to one.
Var keyword is a popular addition to Java 10. This is a powerful feature that other modern-day languages have. In Java 8, every variable needed to be defined by its type, resulting in boilerplate code with little readability. With the introduction of var keyword, Java code is more readable. Additionally, prior to Java 11, you could not use var in a lambda. This, too, has been taken care of in Java 11.
The next LTS release is Java 17, which is scheduled to be released in September of 2021. Stay tuned for all the cumulative enhancements and features of Java 12, 13, 14, 15 and 16. With all these enhancements, Java is becoming forever young. In the meantime, if you have any questions about any version of Java, please feel free to reach out to us at any time. We’d love to help you get started.