Archive

Author Archive

Autoboxing / Unboxing gotchas

August 20th, 2009 CertPal 3 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:

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.

Categories: java Tags: , , ,

Code too large for try statement ?

August 20th, 2009 CertPal 3 comments

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 ?

Categories: java Tags: , ,

Can google detect swine flu ?

August 19th, 2009 CertPal 1 comment

Apparently it might be able to. Google has launched a flu trend service that can be used to determine the trends behind a flu pandemic / seasonal flu.

The flu trends faq hints at being able to detect pandemic flu. The quote from the faq reads

How is Google Flu Trends useful for pandemic flu?
Google Flu Trends models are built based on historic flu surveillance data. When a new flu virus causes the same symptoms as seasonal flu, Google Flu Trends can detect if overall flu rates are significantly increasing. Some search queries tend to be popular exactly when flu is happening, and are therefore good indicators of flu activity.

Google tries to understand search data and its related patterns and then determines if the search was related to a flu in any way. It then maps this search to geo data to find out how the flu spreads.

Java – Console

August 17th, 2009 CertPal 14 comments

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.

Categories: Java certifications Tags: , ,

Sun java certification pitfalls

August 17th, 2009 CertPal 1 comment

Before you can go ahead and take the real Sun Java exams, you need to know some of the pitfalls that have plagued several test takers. Here are a few pointer

Do not buy a voucher before you are prepared to take the exam:

Often some candidates like to follow this approach hoping that the purchase of the voucher would force them to learn within a given time period. On the contrary this adds additional pressure to your preparation, since you need to study the objectives before the expiry date. Some candidates have lost money misinterpreting the expiry date on the voucher. Be wary of the MM/DD/YYYY Vs DD/MM/YYYY formats. Which one does your voucher display ?

When the candidates finally find that they have no more time to learn, they post a message on a forum that reads something like ‘I have voucher for exam X for a discount of Y%. Please call me to buy’. Remember, you are a candidate, not a voucher reseller :)