Home Java The Things I Love and Use from Java 11

The Things I Love and Use from Java 11

Published: Last Updated on 0 comment

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 have 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 sip and code with Java 11.


I would be happy to connect with you guys on social media. It’s @coderstea on TwitterLinkedinFacebook, Instagram, and YouTube.

Please Subscribe to the newsletter to know about the latest posts from CodersTea.


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 for free for development purposes. No worries, You can also use OpenJDK as an open-source option. I am 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 to the language, both at the user level and JVM. All the releases 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 the next will be 17. So many organizations look for LTS support, that’s why they are using Java 11 and we are talking about it. To install java 11 please follow the 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 languages’ 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 posts, so stay tuned to CodersTea.

Ok then, back to Java 11, here we go.

String in Java 11

Numerous functions were added to 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.

strigToRepeat.repeat(noOfTimesYouWantToRepeat)Code language: CSS (css)

lines()

In my opinion, lines() is string.split("\n") on steroids. It creates a Stream of Strings extracted from a 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" : 2Code language: JavaScript (javascript)

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.Code language: JavaScript (javascript)

isblank()

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(); // falseCode language: JavaScript (javascript)

All New Files Methods in Java 11

Files are the util or helper class to ease the file operations. Let us see the new methods in the Files class.

readString(Path)

Just provide the path of the file, and 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.

Files.readString(Path.of("path/for/file"));Code language: JavaScript (javascript)

writeString(Path, String)

Instead of reading, you are writing to the file now. It takes the Path of the file and a String that 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.in" );Code language: JavaScript (javascript)

isSamefile(Path1, Path2)

Are the given Path refers to the same file or not? It checks the location and not the 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); // falseCode language: JavaScript (javascript)

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 add it in 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();// falseCode language: JavaScript (javascript)

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.

The code is available on GitHub.

See you in the next post. HAKUNA MATATA!!!


I would be happy to connect with you guys on social media. It’s @coderstea on TwitterLinkedinFacebook, Instagram, and YouTube.

Please Subscribe to the newsletter to know about the latest posts from CodersTea.


Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Ads
Ads
Ads

@2023 All Right Reserved. Designed and Developed by CodersTea

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More