Archive for the ‘Java’ Category





Looping through Collections is fun in Java 5

Posted on May 16th, 2007 in Java | 8 Comments »

Edit: This post was earlier titled “Looping through Collections is fun in Java 6” which has now been renamed to “Looping through Collections is fun in Java 5” due to the accidental discrepancy of mentioning the feature belonging to Java 6. The mistake is rectified. Thanks for the feedback to all.

The traditional looping for construct has served well for many years in Java. Even today, for collections people still use the traditional approach for looping through them. The simple reason being familiarity. Its evident that the powers provided by the revolutionary addition of features in Java upgrades have yet to see their complete establishment.

Coming to the collections frameworks the other leagues of languages like PHP, JavaScript, VB have provided convenient looping constructs for the for syntax namely the for each block. Java programmers have craved long for the same and its time that it was implemented in the favorite language of millions worldwide.

The collections framework comes with a new for construct or the for-each block although the each term is missing from the syntax. Now you don’t need Read the rest of this entry »

Getting started with IoC - A simplified tutorial

Posted on April 26th, 2007 in Java, Tutorials | 2 Comments »

I had mentioned about Inversion of Control (IoC) in my earlier post Inversion of Control - for easy integration. In this post I will show you how you can actually make your integration easy with implementation of a mini application sample based on IoC.

Objective
The main objective of this mini tutorial is to highlight the implementation of an interface based Inversion of Control (IoC) architecture and to state how it could help in the integration and maintenance of the application.

For this mini application I will use the following set of classes

  1. Dependency - This is the interface which is implementation as per my application versions progress and which will be used by the View components in my application. In other words this is my Model component in a MVC architecture
  2. Dependent - This is my View component which uses the Model component to produce the results for displaying. As the name suggests the class is dependent on the Dependency implementation.
  3. Injector - This is most important class in the application and which introduces the aspect of Inversion Of Control into the applications. This class can also be referred as the configurator class.
  4. Finally there can a number of implementations of the Dependency interface according to the needs of the application.

Note: This mini tutorial is based on the concept of Interface based IOC. It also assumes that you import the required classed in your Classes when compiling them.

Lets start with our tutorial. Read the rest of this entry »

Inversion of Control - for easy integration

Posted on April 23rd, 2007 in Concepts, Java, Java EE | 2 Comments »

You may have witnessed some of the J2EE frameworks provide a technique to get dynamically bound plain objects by making them bind to some features of the framework outside of the plain objects. This technique is called as the IoC or Inversion of Control. If we have to know about the meaning of this term let us emphasize on what inversion depicts in aspect of implementing some features in plain objects.

Overview
Suppose I have some functionality of my J2EE application in my model which is nothing but a simple POJO. Now I want this POJO to be incorporated into a web tier which is an MVC (Model View Controller) Architecture. The last thing I would want to do is modify the source of the model, view or controller and put in some additional code which would do this integration. But suppose I choose a framework in such a way that I put what exactly is to be done by the POJO in the MVC architecture in a simple XML config file and then put the config file in the framework and the framework does the rest of the integration work. Read the rest of this entry »

The Great Ant Tutorial - a great jump start

Posted on April 16th, 2007 in Java, Java EE, Tutorials, XML | 34 Comments »

Apache Ant is a powerful way to convert your developmental structures to deployment structures. It is declarative and all the command line tasks used for deploying an application are represented by simple XML elements. Without much details, this tutorial would breeze you through the steps on how to build a web application using a single XML build file and nothing else. If you have not yet understood what is the use of Ant, read my article on Development and Deployment Structures - the perfect way to build web applications.

I would use the same analogy of my development structure as mentioned in the above linked article i.e. my development structure consists of the following directories:
Read the rest of this entry »

Annotations in POJO - a boon or a curse?

Posted on April 7th, 2007 in Java, Java EE, Web Services, XML | 4 Comments »

The purpose of annotations have been well put forward with the recent upgradation of EJB to EJB 3.0 and also in the web services field by JAX-WS. Annotations help in reducing the learning curve associated with these specifications by avoiding the need to learn the deployment descriptors which are necessary to properly deploy an EJB or a JAX-WS web service respectively.

Annotations - a boon or a curse?
Due to the above situation, the influence on the usage of annotations is steadily rising. It is but natural that programmers would try to do their work quickly without having to waste the time in learning the XML Schema’s required for deployment. Annotations are purposely aimed at this programmer’s dilemma. In fact when I was working in JAX RPC, I used to think as to when I would get a tool to code those ugly descriptors in an easier fashion. With annotation based web services creation in JAX-WS, half of my work is done when I put them in the POJO itself.

All of this can be attributed as a boon to the presence of annotations. But the point of worry is, Read the rest of this entry »

Java 5 Generics - Changing for good

Posted on April 4th, 2007 in Issues, Java | No Comments »

I had recently tried some programming in JDK 1.6 which falls in the league of Java 5. The Eclipse environment started behaving strange in someway which I had not expected. The code which was irritating me was the one which included a collection object

List list = new ArrayList();
list.add("someString");

The Eclipse IDE flashed a warning beneath the 2nd line of the code.

“Type safety: The method add(Object) belongs to the raw type List. References to generic type List<E> should be parameterized”

Read the rest of this entry »

Convert Date to String and String to Date in Java

Posted on March 28th, 2007 in Java | 2 Comments »

This is quite a common query which is faced by a Java Programmer while dealing with dates. The problem is when a form value returns back the date value input by the user in the form of a string. The application might be using a Date object to hold the date values in the application.

So in order to convert the String date value to Date object you might be tempted to use the simplified conversion format which is,

String str = "26/08/1994";
Date date = new Date(str);

But the problem with the above step is that the Date() constructor is now deprecated in JDK1.5 onwards and hence won’t be a good practice to use the deprecated ones.

So the solution for this is to use the SimpleDateFormat object is,

Read the rest of this entry »