Monday, March 21, 2016

String vs. StringBuffer/StringBulider

Hi all,

In this post, I’ll demonstrate how memory behave while string type data involving scenario in our JAVA program. Basically, when we execute JAVA program on our PC, Java mange PCs main memory in its own way. For string type operations are handled through following 3type of memory segments.
  • Stack
  • Heap
  • Pool






String data type is immutable, mean when changes happen to literals particular literals never change, instead of that reference variable pointed to new value. This behavior helps to manage literals which have more than one reference.

When we consider above scenario, step 1 to 4 nameList variable reference to new literals in each step of the loop, at the same time previous values of literals are reaming inside the pool without any references, instead of JAVA garbage collector involving to clear pool, String pool will be full of garbage value.

For mitigate these kind of scenario JAVA 5 introduces StringBuffer & StringBulider both of the classes are not immutable. Let’s see how to implement above code segment using StringBuffer class.


Happy Coding,
Regards,
Denuwan Himanga

Saturday, March 12, 2016

Solution for Apache POI Date format Issue

Hi folks,

Today I’m here with some tricky solution, if you are familiar with Apache POI you might be facing this problem before. I’ll explain in fundamental level.

What is Apache POI

POI is the one of the common open source libraries introduced by Apache for interact (Inserting/Deleting/Reading) with applications in MS office package. Using POI library, we can easily create an interconnection with MS office application and our JAVA app.  

But most of the time, Apache POI doesn’t support for large Excel files. Because of that, most of developers are useing another third partition library call Streaming Reader for filtering specified excel sheets from large excel file.  But when we use this library some time reader doesn’t identify the exact cell type in excel file. Most of the time the cells, which we change the cell type as Date, Streaming reader, read this cell without any exception, but then we can notice, it displays a totally different number when we compare with exact value in the Excel cell.

Actually the reality is Streaming reader little bit intelligent than our eye, usually excel assign some formulated value for each date formatted cell for their calculation purpose. So what happens is streaming reader read that value instead of actual displayed value in excel cell.  

The reading value is not a random value, 01/Jan/1900 excel numbering as 01. So using a small conversion method we can easily convert these numbers in to actual date.

Here's the code, or you can simply use Convert Serial To Date library for that conversion process,


Regards, 
Denuwan Himanga