Archive

Archive for August, 2009

Eclipse tips and tricks – Part 1

August 30th, 2009 5 comments

Eclipse is a pretty good IDE to develop java apps on. However there are some features that you might not be using every day that can save you a lot of time.

Writing your own code templates is one of them. To write your own code template do the following

Go to Window -> Preferences. Select Java -> Editor -> Templates. You can add your own templates here. Templates are nothing but the code that auto completes when you press ctrl+space. For example type syso and ctrl+space. This will automatically fill in System.out.println() into the editor.

Eclipse code templates:
eclipse_code_template
Code template variables:

Eclipse can automatically fill out dynamic code by sniffing out variables in a template. Here are a few you should make yourself aware of

Categories: java Tags: , , ,

Your favorite java IDE

August 27th, 2009 43 comments

Which of these tools do you use to code in java ? I use myeclipse for J2EE coding and Netbeans for swing UI (which I would highly recommend.). I have worked with workshop as well.

Which of these is your favorite java IDE ?

  • Eclipse - J2SE / J2EE (48%, 1,405 Votes)
  • Netbeans (44%, 1,293 Votes)
  • IntelliJ (17%, 495 Votes)
  • Plain text editor (4%, 110 Votes)
  • Myeclipse (3%, 74 Votes)
  • JDeveloper (1%, 40 Votes)
  • RAD / WSAD (1%, 35 Votes)
  • Other (1%, 34 Votes)
  • Weblogic workshop (0%, 8 Votes)

Total Voters: 2,926

Loading ... Loading ...





Categories: java Tags: , ,

Java shutdown hooks

August 24th, 2009 4 comments

Java allows you to add shutdown hooks to your code. A shutdown hook is simply a thread that has been left in the initialized state. When your JVM is about to shutdown, the shutdown hook thread kicks in. The finalization processes of java objects run after the shutdown hooks complete. The JVM allows you to register more than one shutdown hook.

Let us take a look at how this is done

public class Task
{
    public static void main(String[] args)
    {
        Runtime runtime = Runtime.getRuntime();
        Thread thread = new Thread(new ShutDownListener());
        runtime.addShutdownHook(thread);
        someProcess();
    }
 
    private static void someProcess()
    {
        try
        {
            System.out.println("I am busy");
            Thread.sleep(2000);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }
}
 
class ShutDownListener implements Runnable
{
    @Override
    public void run()
    {
        System.out.println("I am shutting down");
    }
}

The Task class is a simple class that runs a process. Once this process finishes up, we want the JVM to run a shutdown hook to notify us that the JVM is shutting down. When this program is run, the output is

I am busy
I am shutting down

Nice !

Lets experiment a little more. How robust is a shutdown hook ? After the someProcess() method is called let us analyze what will happen when one of the following statements are added

Categories: java Tags: ,

Free uml tools

August 22nd, 2009 22 comments

Here are 5 free UML modelling tools you can use. I use the JUDE community version. I found the user interface easy to learn and the product was also feature rich and simple. The products are not necessarily arranged in any specific order and the list is certainly not exhaustive.

Have fun designing

JUDE – community: http://jude.change-vision.com/jude-web/product/community.html

jude

UMLet: http://www.umlet.com/

umlet-screenshot

Argo UML: http://argouml.tigris.org/

argo-screenshot

BOUml: http://bouml.free.fr/download.html

boUML-screenshot

Visual paradigm – community: http://www.visual-paradigm.com/product/vpuml/editions/community.jsp

visual-screenshot




Categories: General Tags: , ,

Autoboxing / Unboxing gotchas

August 20th, 2009 9 comments

Java introduced the concept of autoboxing and unboxing since JDK 5. It has been used quite liberally by developers ever since. But those that do not understand the difference between primitives and Wrapper types can end up misusing it. They can especially become dangerous when coupled with a framework that provides some sort of type mapping, such as entity EJBs that map Wrappers to table columns.

Let us look at some of the gotchas

Gotcha 1:

public static void main(String[] args) throws Exception
{
    Test test = new Test();
    int autobox = test.autobox();
}
private Integer autobox()
{
    return null;
}

What could possibly be the output of this program ? Well, let me not leave you guessing

Exception in thread "main" java.lang.NullPointerException
	at com.tests.Test.main(Test.java:4)

As a java programmer, when I see this exception stack trace I immediately think ‘Hmm… the test reference must have become null somehow’ without realizing that unboxing the null to a primitive failed. I could end up wasting time analyzing something that is perfectly all right, if I do not have a look at the autobox() method first.

Categories: java Tags: , , ,