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 ...
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
| 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
1
2
| 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
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:
1
2
3
4
5
6
7
8
9
| 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
1
2
| 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.
If you didn’t already know it, the java language specification has limits on the size of a method. That limit is 65535 (number of bytes that the code occupies).
This so called limitation has never really bothered me. After practicing object oriented programming, modularization and good design principles you can only ask yourself ‘Who is crazy enough to write a method whose size is greater than 65535 !?’. But you know what ? It can happen. There are so many code gen tools out there and common libraries that will write code for you. Rules engines, JSP compilers, scripting/template engines will write dynamic code for you. If they do not write it withing the 65535 bytes limit, your code will not compile or will give a cryptic error and die.
Code suffering from this phenomenon will see this message – “Code too large to compile” or “Code too large for try statement“. So umm.. where did the try statement come from ?
To get sensitive user input without echoing it to the console/terminal, the Java SDK has introduced the java.io.Console class. The Console class is quite unique in the way it handles its data. It uses the native encoding of the system instead of the using the JVM’s default encoding.
The Console class provides methods to access the console/terminal, if any is associated with the JVM.
Using the Console class:
So how do you use the Console class ? Its pretty simple really. First get a reference to the Console class from the System class
1
| Console con = System.console(); |
It is not always possible to get a reference to the console. Some scenarios prevent the developer from gaining access to the console. This method may return null in these cases. For example a batch process running on a server will not expect user input and usually does have a console. Your code must be capable of handling such situations.