Thursday, September 18, 2014

How to Convert Array to String in Java with Example

Array and String are very closely related, not with anything else but with popularity. Many times we need to convert an array to String or create a array from String, but unfortunately there is no direct way of doing this in Java. Though you can convert array to String by simply calling their toString() method, you will not get any meaningful value.  If you convert an integer array to String, you will get something like I@4fee225 due to default implementation of toString() method form java.lang.Object class. First I shows the type of the array and content after @ is hash code value in hexadecimal. How valuable that is? This is not what I want to see, I was interested on contents rather than hashcode. Fortunately Java provides a utility class called java.util.Arrays, which provides several utility method for arrays in Java. For example, here we have method to sort array, search elements using binary search, fill array, methods to check if two arrays are equal or not, copy range of values from one array to another,  and much needed toString() and deepToString() method to convert both one dimensional and multi-dimensional array to String. These method provides content view of array, for example, when you convert an integer array {1, 2, 3, 4, 5, 6, 7} to String, you will get [1, 2, 3, 4, 5, 6, 7] instead of  [I@2ab6994f . In this article, we will see examples to convert different types of array to String e.g. int, char, byte, double, float, Object and String array itself.  We will also learn how to convert two dimensional array to String in Java.

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.

Java67 Headline Animator