{{announcement.body}}
{{announcement.title}}

The Things I Love and Use from Java 11

DZone 's Guide to

The Things I Love and Use from Java 11

Java 11 with Improved productivity and less repetition. No need for external libraries for small things. Functions on steroids.

· Java Zone ·
Free Resource

Hey, tea lovers! Let us see what Java is providing to keep up with the latest trend or requirement. 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 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 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.

repeat(n)

No more using a third-party library to create a string containing the same character n times now. Just do the following.

Java

lines()

In my opinion, lines() is string.split("\n") on steroids. It creates Stream of Strings extracted from given String with multiple lines.

Java

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.

Java

isblank()

As the name suggest itself, tells you whether the string is blank or not. It considers string with only spaces as blank.

Java

All New Files Methods

Files are the util or helper class for the File class. One thing I have observed is that a particular class' helper class has the same name but with the 's' at the end, i.e plural of the class is its helper class. Such as Objects for Object, Files for File. Let us see the new methods in the Files class.

readString(Path)

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.

Java

writeString(Path, String)

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.

Java

isSamefile(Path1, Path2)

Are the given Path refers to the same file or not. It checks the location and not content so don't get confused.

Java

Optional#isEmpty()

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.

Java

Conclusion

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. I think as a programmer you can do many things, such as,  TV Series Every Programmer Should Watch, and you can reduce the null pointer with the help of Optional and say goodbye to boilerplate code with Lombok.

The code is available on GitHub. See you in the next post.

Topics:
change, constant, java, java 11, latest

Published at DZone with permission of Imran Shaikh . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}