Thursday, October 16, 2008

Aggregate data when calling methods

A mistake that we all do is, when calling a method we don't aggregagte data.

Eg:

If we have a method to save a customer with Customer Name, Address, Telephone No etc... Please avoid writring the code as follows:

public void saveCustomer(String customerName, String address, String telephoneNumber){}

In the above case, it requires to create three memory objects. Instead use an aggregated object to improve local and distributed performance.

public void saveCustomer(Customer customer){}

Tuesday, October 14, 2008

J2EE Design Patterns

Friday, October 10, 2008

Java Reflection

Its a mechanism that enables dynamic discovery & binding of classes, methods, fields & all ther elements. Reflection can do more than just simply list of classes, fields & methods. Through reflection, we can actually create instances, invoke methods & access fields.

Reflection allows us to examine the object at hand and act accordingly during runtime. 

Avoiding NullPointerException when Comparing string values

Compare strings/constants by putting them on the left side of the comparison

You can avoid many NullPointerExceptions by simply always placing hardcoded string and constants on the left side of an object comparison. This works well for any object constants (not just strings).

For example, this code can generate a NullPointerException if myString is null (bad):

if(myString.equals("hardcodedstring")) {
// dosomething
}
This example code can never generate a NullPointerException (good):
if("hardcodedstring".equals(myString)) {
// dosomething
}

Tuan Izzath Abdeen
Senior Solutions Developer

Tuesday, October 7, 2008

Fail-Safe concept in collection

1. Difference between Hashtable and HashMap.
  • Hashtable is synchronized, HashMap is not.
  • Iterator in the HashMap is fail-safe (meaning, it throws ConcurrentModificationException when try to add a value to the collection while iterating), but the enumeration for Hashtable is not fail-safe.
  • HashMap points null values, while Hashtable doesn't.

If you want to synchronize a HashMap write the following code.
Map map = Collections.synchronizedMap(new HashMap());

2. Vector vs ArrayList.
  • Vector is synchronized, ArrayList is not
  • Iterator that are returned by both classes are fail-safe. Enumeration returned by Vector are not.
If you want to synchronize an ArrayList write the following code.
List list = Collections.synchronizedList(new ArrayList());

Thursday, October 2, 2008

EJBHandle