Tuesday, November 16, 2010

Java Beans - getters and setters- how you see them

When we talk about Java Beans. Thing comes to the mind is an object having only getters and setters to access and change its instance variables. A POJO. Ever thought why do we use this or are they really useful in your project? Maybe we are using them because its a standard but not a need. Maybe we do need them. Let me dive into its depth.

If my service layer has to transfer some data between data layer and other layers. I would go for a DTO which is a class with private instance variables and respective setter and getters which will change them when needed. Some time I as a question to my self that if I am providing getter and giving the object to the caller and setter which will change the instance variable, then why do I keep it private. Don't you think I should make that object with all public fields without any setters or getters. This will also reduce the lines of code and easy to acess the instance variables.
If this is the case, then why am I providing getXXX() and setXXX (). Is it just that its a standard and I have to follow?

Thursday, September 9, 2010

java.lang.OutOfMemoryError with StringBuilder or StringBuffer

StringBuilder or StringBuffer are used for string manipulations. Yet they fail some times.
Both the final classes are the extension for packaged scoped abstract class named "java.lang.AbstractStringBuilder". These builder/buffer classes holds character array and append or manipulate this char[] each time we perform operations on it. The default char[] size is 16 although this can be specified while creating the object. When append() method is called upon StringBuilder/StringBuffer, the capacity of this char[] is checked if the String/char/any other object can be added to ongoing char[] buffer. If the character buffer size is less. I.e. not sufficient enough to append more, the capacity is doubled . Lets say its capacity was 16, and not sufficient to add more, than the the capacity is increased by doubling the size of current buffer length. This keeps happening when you keep on appending stuffs to it. Common case is while reading a file and storing it inside memory in form of a StringBuilder. You never know what the size of file could be. when size isn't sufficient for the StringBuilder, buffer size is doubled and this keeps growing. A point will reach when System is not able to

Monday, August 23, 2010

Parsing complex nested XML with SAXParser and collections

In my previous example, I have explained how to parse simple XML with on and multiple elements or objects. This is beyond that. Have you ever encountered and XML which have nested elements and the nested elements again have same nested objects and the tree grows. I happen to encounter such huge XML of size in few MBs. Sometimes collections like java.util.Stack or java.util.Map are helpful and very useful in this case. While parsing, you have to add and object as a child to another object, but it becomes difficult to determine to which parent object the child has to be added. The image below shows a basic tree structure of the XML file in this demo example.

Friday, August 20, 2010

XML parsing with SAX Parser Demo

Java provides two ways of parsing XML. Namely SAXParser and DOM Parser. With DOM Parser complete XML document object is held in emory and uses the DOM approach with DOM elements. This approach is used when you need to get hold of elements from XML in any order. But using DOM parser is costly in term of memory. When you have a XML document and want to read it once and create in memory java object (POJO), go for SAXParser approach. SAXParser parses the document(XML) from top to bottom, tag by tag.
Using Sax parser is simple. All we need to do is create a Handler class which extends DefaultHandler class of org.xml.sax.helpers package (part of JDK). In this Handler class, we define how tags are processed and stored into in memory object.
Once the Handler is developed, Create a Parser class which uses SAXParser abstract class from javax.xml.parsers package (also part of JDK). Use this Parser class and parse the document with help of the Handler class which we created. Current example demonstrate simple XML, for parsing nested XML with hierarchy, read a another article as how to parse nested XML.
Below is a snippet of simple XML file which needs to be parsed.