Tuesday, September 16, 2014

How to Read, Write XLSX File in Java - Apach POI Example

No matter how Microsoft is doing in comparison with Google, Microsoft Office is still the most used application in software world. Other alternatives like OpenOffice and LiberOffice have failed to take off to challenge MS Office. What this mean to a Java application developer? Because of huge popularity of MS office products you often need to support Microsoft office format such as word, Excel, PowerPoint and additionally Adobe PDF. If you are using JSP Servlet, display tag library automatically provides Excel, Word and PDF support. Since JDK doesn't provide direct API to read and write Microsoft Excel and Word document, you have to rely on third party library to do your job. Fortunately there are couple of open source library exists to read and write Microsoft Office XLS and XLSX file format, Apache POI is the best one. It is widely used, has strong community support and it is feature rich. You can find lot of examples of how to do with Excel using Apache POI online, which means you will never feel alone and has instant Google support if you stuck there. In this article, we will learn how to read and write excel files in Java. As I said, Excel files has two popular format .XLS (produced by Microsoft Officer version prior to 2007 e.g. MS Office 2000 and 2003) and .XLSX (created by Microsoft Office 2007 onwards e.g. MS Office 2010 and 2013). Fortunately Apache POI supports both format, and you can easily create, read, write and update Excel files using this library. It uses terms like workbook, worksheet, cell, row to keep itself aligned with Microsoft Excel and that's why it is very easy to use. Apache POI also provides different implementation classes to handle both XLS and XLSX file format.

Friday, September 12, 2014

Why Constructor is Important in Java

In simple word, Constructor is a method like block of code which is called by Java runtime during object creation using new() operator. Constructor are special in sense that they have the same name as the Class they are part of. They are also special in a sense that they are called by JVM automatically when you create object. Have you ever thought about Why do you need constructor? What benefits it provide? One reason is to initialize your object with default or initial state, since default values for primitives may not be what you are looking for. One more reason you create constructor is to inform the world about dependencies, a class needs to do it's job. Anyone by looking at your constructors should be able to figure out, what he needs in order to use this class.  For example, following class OrderProcessor needs a Queue and Database to function properly.

Wednesday, September 10, 2014

Right way to check if String is empty in Java

What do you most of us do while using String in Java? checking whether String is null or empty right? I am sure you know couple of way to test whether String is empty or not, but do you know the right way to do it? When we talk about Strings in Java, we can imagine them as arrays of characters, and they are, but in Java they are also object. An empty Java String, is considered as the not null String that contains zero characters, meaning its length is 0. However, a Java String that might only contain the white-space character is not considered as empty, it is considered to contain one character and its length is equal to 1. One of the most popular way of checking whether String is empty or not is String class' isEmpty() method, this looks perfect right, it's readable and returns boolean if String is empty otherwise returns false, but problem is you can not call this method without checking whether String is null or not. In another word, this is not null safe and it will throw NullPointerException if String is null. Another popular and faster way to check if String is empty or not is by checking it's length, e.g. if String.length() = 0 then String is empty, but this is also not null safe. Third common way of checking emptiness of String in Java is comparing it with empty String literal e.g. "".equals(str),this method is not as fast as previous two but it is null safe, you don't need to check for null, in case of null it will return false. So in my opinion, this is the right way to check if String is empty or not.  If you definition of empty String also includes null then you can also use Apache Commons Lang's StringUtils class. It has methods like isEmpty() which return true for both null and empty String literal. Again this is also null safe and will not throw NullPointerException.

Java67 Headline Animator