Category Archives: Java

How to format Java date using SimpleDateFormat

Introduction Strings are very easy to manipulate, and the String class itself provides many methods to play with data types and use them in different ways. We can convert Java date into String using the SimpleDateFormat class that provides many different formats. We can represent the date in various formats like mm-dd-yy or dd-mm-yyyy etc. How to format… Read More »

How to determine Java version from Program

Introduction Java has many versions so far, and with each version, some significant features are released. There are several differences between the APIs and how the code is written from version to version. That’s why to use some of the functionalities, we need to know which version our codebase is. For example, unboxing/boxing was introduced only in Java5,… Read More »

How to use static imports in Java?

Introduction My mentor always told me that it is a bad practice to use System.out.print statement, and we should rather use log4j to write messages to log files. System.out prints messages to the console. However, while we do our debugging and testing, especially on IDE, it is easy to add System.out statements. Eclipse IDE has a shortcut –… Read More »

How to open a file using Java FileReader

Introduction Both FileReader and BufferedReader are used to read data from a text file and display them. We will see how to open a file and then read text from it using simple Java methods. Be sure to be familiar with the Java while loop and for loop to appreciate the below examples fully. How to open a… Read More »

Generics Example in Java

Introduction Generics were introduced in Java 5 and gave a lot of flexibility. Generics is available for all the collection types and makes creating ArrayList, HashMap, Set, etc., straightforward and strongly-typed. We can create our own generics classes too. We can simply define variables on the fly by doing so. It is easy to create a Generic class… Read More »

How to populate predefined static data in HashMap (Map) in Java

Introduction A map is used to store key-value pairs. For example, {“name”, “john”} is a key-value pair where name is the key, and ‘john’ is the value. It is easy to populate static data in Map. Today, we will see the implementation of HashMap in Java to initialize or populate a static HashMap. How to populate predefined static… Read More »

How to Replace Blank Characters in a String

Introduction There are many ways to replace blank characters in a String in Java. Regex is one of the most popular methods to replace blank characters in Java Strings. In today’s article, we will see different approaches. How to replace blank characters in a String There are many classes like StringBuilder, StringBuffer etc., but the replaceAll method in… Read More »

How to square a number in Java

Introduction There are many ways to square a number in Java, the simplest being multiplying the number by itself. There are also utility methods to do the same. If your project needs to call this often, you can build your own function and call the utility as well. We will also see how this utility method can do… Read More »

What is a Java IOException?

Introduction IOException means any input-output exception that can occur while reading a file or writing into a file, basically accessing the file system. There can be many ways in which this exception can occur. The most common I/O exceptions are FileNotFoundException, InterruptedIOException, SocketException, UnsupportedDataTypeException. What is Java I/O Exception Java IOException extends the Java Exception class. To get… Read More »