"A little progress everyday adds up to big results"

Sunday, 16 September 2012

Clear explanation of Java's Ugly Syntax

Introduction:
               Most of the Java programmers might have worked with the (Ugly) syntax of an anonymous class. This post will cleanly explain the Ugly syntax (especially with Anonymous classes) of Java. For example,  consider the button click event in Java Swing.

class EventHandler implements ActionListener
{
         public void actionPerformed(ActionEvent e)
         {
                     // Code that handles the event
         }
}

// Somewhere else
ActionListener e = new EventHandler();
button_object.addActionListener(e);


This is a very straight forward way, where each and every process is done step by step. Now, before moving on to the equivalent ugly syntax code, we must be knowing 5 Java concepts.

1. Interface object concept:
                                 An interface can have an object to which, an object of one of the classes implementing it, can be assigned. For example, in the above code, ActionListener is an interface and e is an object of it. We have instantiated e by invoking the new operator on the EventHandler class (which implements the ActionListener interface).

2. Anonymous object concept:
                                 The synonym for the term 'Anonymous' is 'nameless'. There might be some situations when we would require the object of a particular class, in just one statement. In such cases to avoid 2 lines of code, we may go for an Anonymous object. 

For example:

ActionListener e = new EventHandler();

button_object.addActionListener(e);


could be written as

button_object.addActionListener(new EventHandler());

                           This is done purely to reduce the lines of code; however doing so affects the readability of the code, whereas the efficiency of the code remains unaltered.

3. Method implementation specific to an object:
                                While writing multi-threading code in Java, one might have written code something like this.

Thread t = new Thread()
{
          public void run()
          {
                   // Thread code
          }
};


One can say that this code implements the method run(), which is specific to the object t.

4. Method implementation specific to an instance of an interface:
                          This is nothing but the combination of the concepts 1 and 3. This concept will be well understood with the following examples.

Runnable t = new Thread()
{
     public void run()
     {
          // Thread code

     }
};


Using this concept with the ActionListener interface,

ActionListener e = new EventHandler()
{
          public void actionPerformed(ActionEvent e)
         {
                     // Code that handles the event
         }
};


5. Anonymous class concept:
                              Bit tricky to understand, but would be simple with an example. The syntax of an anonymous class is
new <Implemented interface/Super class>()
{
           // Code inside the class
}

For example,
class A
{
       // block of A
}
interface B
{
       // block of B
}
class C extends A implements B
{
       // block of C
}
class Main
{
        B b = new B()
        {
               // methods of the interface B are implemented here
        };
}

                
Putting them all together:                        
                            Now, we are going to put them all together. So, the click event code (mentioned in the second paragraph) can be written as follows.

button_object.addActionListener ( new ActionListener()
{
        public void actionPerformed(ActionEvent e)
        {
            // Code that handles the event
        }
} );


                Well...! Hope you might have understood. If not, please don't worry, you will understand it when you use these concepts in your code.

Now tell me, isn't Java interesting and challenging ?

Sunday, 9 September 2012

Running Java Applet in free website domain

                           As a Java programmer, everyone will know that Java can be embedded in Web pages as Applets. This post will show how an Applet can be embedded in a Web page, which is hosted in a free domain.

Step 1: Make the Java applet source file ready.
Step 2: Compile the Java applet to get the class file.
Step 3: Write an HTML code, embedding the Applet.
Step 4: Create a free domain using one of the free hosting services like 000webhost
Step 5: Upload the .class and the .html files into the same directory

Share the URL with your friends and enjoy!

Sunday, 2 September 2012

Effective utilization of resources


                Initially I would like to inform that this post is not to hurt anybody. The only intension of this post is to make people to utilize the available resources efficiently

             We, the homosapiens, keep telling that certain resources are not abundantly available. Now-a-days, electricity is very scarce in India. This is very evident as most of us might have been facing many problems because of frequent and unannounced power cuts.

                  Do you think that electricity is really insufficient ? That might be true to some extent, but, what I feel is that some people are overusing (and thereby wasting) it. The variations are very drastic in the country. When there is a group of people suffering because of power cut, there is a group which consumes electricity without any use. There are some people who are waiting for the power to type their documents in a computer. There are also people who keep their computer turned on the whole day whether or not they are using it.

                   Petrol hike keeps persisting because of the inefficient usage of it. People start preserving a thing, only when there occurs a scarcity. If a certain resource is abundantly available, we use it to a greater extent. Why not we preserve that particular resource inspite of its abundant availability ? We expect certain things as a rule. For example, some of us use helmets for the sake that it's compulsory and not for our own safety. Another example is the rain water harvesting scheme of Tamil Nadu government a few years ago.

                     I don't mean that we should limit the usage of resources. My point is that don't use any resource unless it is needed (or in other words, don't unnecessarily use any resource).

"It's our duty to leave behind all the resources to our future generations"