RSS Feed for ProgrammingCategory: Programming

Java: Beware of Immutable Objects »

Have a look at the following code 1: BigInteger value1 = new BigInteger(“1000″); 2: BigInteger value2 = BigInteger.ZERO; 3:  4: value2.add(value1); 5: System.out.println(value2.toString()); The result is “0” since for as BigInteger instance is immutable. To get the correct result, you should do the following 1: BigInteger value1 = new BigInteger(“1000”); 2: BigInteger value2 = BigInteger.ZERO; […]

Java: toString and valueOf »

In Java there are many ways to convert data to different data types. Here are some common tips and observations. Always prefer Object.toString rather than String.valueOf. Internally String.valueOf is calling Object.toString.  E.g. String.valueOf(int) calls Integer.toString and String.valueOf(double) calls Double.toString Use primitive data types rather than boxed primitives Use Object.valueOf instead of new Object. E.g. use […]

Java: Always Use java.util.Date in iBATIS instead of java.sql.Date »

It is clearly stated in the documentation The use of java.sql. date types is discouraged.  It is a best practice to use java.util.Date instead Some more java.sql.Date has no time component.

Java 1.5 Bugs: java.lang.StackOverflowError during Compilation »

This is a bug when I upgraded my projects from JDK 1.4 to JDK 1.5. After switching to JDK 1.6, it is okay.

Java: Log Exception Correctly with Log4j »

This is simple but often overlooked. There are numerous code like this 1: try { 2: …. 3: } 4: catch (Exception e) { 5: e.printStackTrace(); 6: } printStatckTrace prints out to “stderr”. If you are using log4j and you should do this 1: try { 2: … 3: } 4: catch (Exception e) { […]

Java: Always Refer to Objects By Interfaces »

In whatever OO programming languages, you should always refer to objects by interfaces. E.g. List<Msisdn> msisdnList = new ArrayList<Msisdn>();   List<Msisdn> msisdnList2 = new Vector<Msisdn>();   Map<String,String> msisdnLookup = new HashMap<String,String>();   Map<String,String> msisdnLookup2 = new Hashtable<String,String>(); By doing this, you program will be more flexible. If you decide that you want to switch implementations, […]

Java: Be Careful of Autoboxing »

For program written with JDK 1.5,  I have seen code written similar to the followings public static void main(String[] args) { Long calculatedValue = 100; for (int i = 0; i < 100; i++) { calculatedValue += i; } System.out.println(calculatedValue); } The code compiled and run perfectly for JDK 1.5. The only thing is that […]

Design of a Java Application Framework Part 3 – DAO Layer using iBATIS »

This is a sequel to my previous posts on AppFuse. For AppFuse, for the persistence layer you can have the flexibility of using Hibernate, iBATIS or JPA. Whether to use Hibernate or iBATIS depends on whether you have full control over the data model, as described in many articles. For iBATIS support, all DAO interfaces […]

JPA with Eclipse Dali Java Persistence Tool »

Starting Eclipse 3.4, you can use the Dali Java Persistence Tool to create JPA project. The requirements are Eclipse 3.4 ( Java Runtime Environment (JRE) 1.5 ( Eclipse Web Tools Platform (WTP) 3.0 ( Java Persistence API (JPA) for Java EE 5. The reference implementation can be obtained from from Glassfish   Using Dali, you […]

Essential Eclipse Plugins »

There are quite a lot of Eclipse plugins that I used and have posted here before. Besides those I have talked about and those provided by Eclipse, here are a list of other plugins that I used amateraside provides the following plugins EclipseHTMLEditor is an Eclipse Plugin for HTML / JSP / XML /CSS editing. […]

Useful Eclipse Shortcuts II »

Quick Assist (Ctrl+1) Create getter and setters: Available on fields. Extract to method: Available on expressions and statements. Extract to local variable without replacing all occurrences: Available on expressions. Use StringBuilder for string concatenation: Available on string concatenations. StringBuilder is used in Java projects with a 1.5 or higher compliance, StringBuffer is used in projects […]

Make Use of Eclipse Code Template »

By defining your own Eclipse code template, you can greatly simplify your daily programming work E.g. go to Windows->Preferences->Java->Editor->Templates, and add a template called “log” with the following pattern private static final Log log = LogFactory.getLog( ${enclosing_type}.class ); Then in our editor, just type “log” and then press “Ctrl-Space”, then statement will be generated automatically. […]

Java: Performance Issue with Serialization »

Java serialization allows us to marshal and unmarshal objects. It is a mechanism automatically, at runtime, converts class objects into metadata so instances can be serialized with the least amount of programmer work. In some of my Java applications, RMI is used quite frequently. Serialization is the mechanism used by RMI to pass objects between […]

Design of a Java Application Framework Part 2 – DAO Layer using Hibernate »

This is the sequel of my previous post on the AppFuse source code, a good MVC framework. Thanks to Matt Raible and the team for this wonderful framework. By reading the code, I can understand the framework better in order to use it correctly. For AppFuse, for the data access layer you can use Hibernate, […]

Copying Values Between HashMap and Hashtable »

What is the problem with the following code? String userName = null;   if (……) { userName = <some value>; } Map<String,String> hm = new HashMap<String,String>(); hm.put(“username”, userName); …..   Map<String,String> ht = new Hashtable<String,String)(hm); There is a bug in this code. What the the userName remains NULL when you are copying the value to […]

Design of a Java Application Framework Part 1 – Model Layer »

I have been using AppFuse in several projects. It is a excellent framework which implemented a proper MVC architecture. In order to better understand the framework, I spent sometime browsing through the source code, I also tried looking for some local SEO services where I could help fast. In Part I of this articles I […]

JMeter Problem for Web Service using SOAP 1.2 with MTOM »

Look like JMeter does not support load testing against web service using SOAP 1.2 with MTOM. Here is the response data that I get whenever I used the Web Service Sampler. application/xop+xml; charset=UTF-8; type=”application/soap+xml; charset=UTF-8″ JMeter can’t seem to parse the response data correctly even though it manages to send the request to the web […]

IntelliJ IDEA toString Plugin »

This is a very useful plugin for IntelliJ. Generate toString() is a plugin to IDEA. The plugin adds a new toString() action in the generate menu (alt + ins). The action generates a toString() method that dumps the classes fields. Java body code is generated using Velocity and you can fully customize this. The plugin […]

Shutdown Hook in Tomcat using ServletContextListener »

Normally in my standalone Java program I can add a shutdown hook to the JVM runtime easily. To achieve the same thing in Tomcat, I use ServletContextListener.contextDestroyed method. If you implemented ServletContextListener, implementations of this interface receive notifications about changes to the servlet context of the web application they are part of. To receive notification […]

iBATIS: Extending Your ResultMap »

Here is an useful feature of the ResultMap. In your iBATIS ResultMap, the optional extends attribute can be set to the name of another parameterMap upon which to base this parameterMap. All properties of the super parameterMap will be included as part of this parameterMap, and values from the super parameterMap are set before any […]