Hey, tea lovers! Since its inception, Java is dying. It is now 25 years old and it is dying. After 10 years again, people will say Java is dying. Well, it is something I and every Java developer are fed-up with hearing. Let us keep ignoring it and focus on what Java is providing to keep up with the latest trend or requirements. I will be going over the things I think has really improved the productivity of the developer since Java 11. We will talk about Java 11 only since it is LTS (Long Term Support) version and many people are using this only until the next LTS. Make your tea to sip and code with Java 11.
Change is Constant
As you know Java has settled on a unique release strategy, every 6 months a new version will be released. Not only that, but the licensing is also changed. To use Oracle JDK, you need a separate license for production, however, you can use it free for development purposes. No worries, You can also use OpenJDK as an opensource option. I myself is using OpenJDK.
Ok, back to the latest version. At the time of this post latest version is 14 and 15 is in the release phase. But it has improved a lot and many new functionalities get added into the language, both at the user level and JVM. All the release will be supported for 6 months and every 3 years i.e after 6 versions an LTS version will be released. Latest LTS is 11 and next will be 17. So many organizations look for the LTS support, that’s why they are using Java 11 and we are talking about it. To install java 11 please follow Install Latest or Any Java Version in 3 Simple Steps: Windows and simply use Java 11 instead of 13. I will be adding a new post for Mac and Linux as well.
From Previous Java Versions
Java 11 has many new features on its own. But I had like to talk about a few features from the previous versions. One of them is the
var keyword. No need for LHS=RHS for references. Jshell is also a great addition to compete with other language’s REPL. The module system also makes it more concise, I would say. I loved the improvements in the Stream API. I will talk about this in detail in some other post, so stay tuned to CodersTea.com.
Ok then, back to Java 11, here we go.
String in Java 11
Numerous functions were added in the String class in Java 11. But I will stick to the ones I think we use most often. Let’s look at them one by one.
No more using a third-party library to create a string containing the same character n times now. Just do the following.
In my opinion, lines() is
string.split("\n") on steroids. It creates Stream of Strings extracted from given String with multiple lines.
String multiLines = "abc \n bac \n abc \n cab " long count = multiLines .lines() // Stream<String> .filter(s -> s.equals("abc")) // only "abc" is allowed .count(); // count the number of "abc" : 2
strip(), stripTrailing(), stripLeading()
These functions are the boost to trim(). It uses Unicode so it is much better than trim.
stripTrailing() is used for removing the spaces at the end of the string. On the other hand, stripLeading() is for removing the whitespace at the beginning. With strip() both prefix and suffix whitespace gets removed.
String string = " some Text "; // I have put '.' as to indicate // the start and end of the string string.strip(); // .some Text. string.stripLeading();// .some Text . string.stripTrailing();// . some Text.
As the name suggests itself, tells you whether the string is blank or not. It considers string with only spaces as blank.
"".isBlank();// true " ".isBlank(); // true "Some text".isBlank(); // false
All New Files Methods in Java 11
Files is the util or helper class to ease the file operations. Let us see the new methods in the Files class.
Just provide the path of the file, it will return the string containing the file’s data. No need for file streaming classes for such simple operations. It needs the Path as the parameter. It also has an overloaded parameter charset as well for decoding.
Instead of reading, you are writing to the file now. It takes the Path of the file and a String which will get written in the file. It has other parameters as well such as charset and open option.
Files.writeString(Path.of("path/for/file"), "CodersTea.com" );
Are the given Path refers to the same file or not. It checks the location and not content, so don’t get confused.
Path path1 = Path.of("/user/coderstea/java11.txt"); Path path2 = Path.of("/user/coderstea/java11.txt"); Path path3 = Path.of("/user/coderstea/java13.txt"); Files.isSameFile(path1, path3); // false Files.isSameFile(path1, path2); // true Files.isSameFile(path2, path3); // false
At last, a function to erase the existence of !optional.isPresent(). Yes, I also was fed up with writing exclamation marks at the start for checking empty Optional. I wonder why didn’t they added it in the Java 8 itself. If you are not familiar with Optional and its wonders, do check out my post on this Reduce NullPointerExceptions with Optional in Java 8 & Beyond.
Optional<String> nonEmptyOptional = Optional.of("Some string"); Optional<String> emptyOptinal = Optional.empty(); Optional<String> nullValueOptional = Optional.ofNullable(null); nonEmptyOptional.isEmpty(); // false emptyOptinal.isEmpty();// true emptyOptinal.isPresent();// false nullValueOptional.isEmpty(); // true nullValueOptional.isPresent();// false
Conclusion for Java 11
There are many more updates on Java 11 apart from what we mentioned above. I only mentioned which I think people will use most often. There is a lot of improvement under the hood also. You can find all the lists on the official website anyway. You can check out another post of mine.
- Reduce NullPointerExceptions with Optional in Java 8 & Beyond
- Say Good-Bye to Boilerplate Code with Lombok: Part 2-The Code
- TV Series Every Programmer Should Watch
- You Only Need These 7 IntelliJ IDEA Shortcuts to Skyrocket Your Productivity
Code are available on GitHub. See you in the next post.