Category: java

  • The best way to use Optional in Java

    Null pointer exceptions. They have often been a headache to java developers. Your application breaks out of nowhere and throws null pointer exceptions. You rub your forehead then and add that null check you missed out. Java wanted to solve this problem. And they introduced the Optional keyword. They described it as a “Container” that…

  • How to move a file/folder to trash/Recyclebin in Java?

    Let’s say you want to move a file or a folder to the recycle bin using java and not delete it permanently. How can you do it? Starting Java 9 , you can do it using a single line of code: Here is a demo: I created a file under C:/files location using the below…

  • Java 16 features every Java developer should know

    Java 16 features every Java developer should know

    Java 16 just released. Though a lot of new features were introduced only six of them need to be understood by day to day java developers. Here are those features: Records Pattern matching for instanceof Sealed classes (Second preview) jpackage packaging tool Stream.toList() method Day Period support added to java.time Formats 1.Records: If you want…

  • How to use Pattern Matching for instanceof operator in Java?

    Let’s say you have a generic method which accepts any object as a parameter. Based on the type of the object , the method performs custom operations. Traditionally to implement this you need to do the following steps: Check the passed parameter using instanceof operator for the specific type If it matches , cast the…

  • How to replace accented characters with the original characters in Java?

    Let’s say you have written an application which processes regular text. If user enters an accented word like “tête-à-tête” your application would break! How to remove the accents in the above characters? That is you want the word “tete-a-tete” and not “tête-à-tête”. Java provides a way. Below are the steps: STEP1: Normalize the word Normalizing…

  • How to watch a folder/directory for changes using Java?

    Let’s say you want to watch a folder in your computer or server for any changes. If a new file gets added you want to be notified. If an existing file is modified you want to be notified. If a file is deleted you want to be notified. And how do you do this without…

  • Java 15 features every Java developer should know

    Java 15 just arrived. There were in total 14 features introduced in this release. But not every feature needs to be understood from a developer perspective. Only 4 of the 14 features are must to know features for java developers to utilize in their java applications. Here are those four: Sealed classes Text blocks Records…

  • How and when to use computeIfPresent() and computeIfAbsent() methods of Map interface in Java?

    Let’s assume you are developing a java app to keep score of goal scores of football players. Every time a player scores a goal , you call a method with the name of the player and the score gets incremented for that player. What data structure would you choose for this? Ideally a map with…

  • Introducing JCli – A command line tool for creating Java applications

    Imagine you could create your java applications from command line using a tool. Just like you would use ng tool for Angular and npm for node js. This would save a lot of developer time. With this in mind I experimented with creating a command line tool called “jcli” (java client) using Java and picocli…

  • How to use Optional keyword to avoid NullPointerException in nested objects in Java?

    Let’s say you have a nested object in your application . And you want to retrieve the value of a field in the deepest nested object. We will see how to do this using pre-Java 8 and post-Java 8 code (using Optional keyword) Lets consider an Employee object, Let’s say the Employee object has an…