Archive

Archive for the ‘java’ Category

Coding with JDK7

November 16th, 2009 CertPal 13 comments

jdk7 codeThe JDK7 milestone 5 update is available for download. Developers now have a chance to try coding with the new language semantics and see for themselves what it is like. The 4 major changes that affect the way one codes in java as of JDK 7 are

  • Using underscores in numerals.
  • Diamond syntax used to work with collections + generics.
  • Using Strings in switch statements.
  • Making use of binary literals

Here is a short code sample that you can use to check the new features out. Use a plain text editor and your old friends javac and java, to test it out. IDEs will not support the new syntax and will most likely complain.

Sample JDK 7 Code:

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
public class Jdk7Tests {
 
    public static void main(String[] args) {
        Jdk7Tests jdk7Tests = new Jdk7Tests();
        jdk7Tests.integersWithUnderscores();
        jdk7Tests.stringSwitch();
        jdk7Tests.binaryLiteral();
        jdk7Tests.diamond();
    }
 
    private void integersWithUnderscores()
    {
        int i = 1_2;
        System.out.println(i);
        i*=10;
        System.out.println(i);
        int j=2_0;
        System.out.println(i-j);       
    }
 
    private void stringSwitch()
    {
        String key = "akey";
        switch (key)
        {
        case "":
        {
            System.out.println("Nothing");
            break;
        }
 
        case "akey":
        {
            System.out.println("Matched akey");
            break;
        }
        default:
        break;
        }
    }
 
    private void binaryLiteral()
    {
        byte aByte = (byte)0b001;
        short aShort = (short)0b010;
        System.out.println(aByte + " " + aShort);
    }
 
    private void diamond()
    {
        Set<String> set = new TreeSet<>();
        set.add("c");
        set.add("b");
        set.add("a");
        for (String val : set)
        {
            System.out.println(val);
        }
    }
}

Underscores and numerals:

Categories: java Tags:

Interview filter question

November 9th, 2009 CertPal 2 comments

Developers interview candidates every now and then. There are days when you need to interview 4 candidates, and finish your work too. Then there are times you go to work on a Saturday to support an interview drive. Whatever the case, it really pays to have a few filter questions that should tell you whether a candidate is full of gas or if they are worth their salt. These questions can be a real time saver. Here are 2 questions that many developers use.

1. Class hierarchy:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class A
{
    public void doSomething()
    {
        System.out.println("A");
    }
}
 
class B extends A
{
    @Override
    public void doSomething()
    {
        System.out.println("B");
    }
}

Give a candidate a class hierarchy like the one above and ask them something like, ‘How should you instantiate these objects so the output is ‘A’ / ‘B”. The you can drill down and ask them about the different instantiation combination. Anyone that says B b = new A(); will instantiate, is out of here.

2. Name them:

Categories: java Tags:

Commons email

November 3rd, 2009 CertPal No comments

There are many apache commons projects out there that save your time either directly or indirectly. Some of the well known commons projects are

  • Commons beanutils – Services wrapped around java beans.
  • Commons digester – XML to java mappings
  • Commons lang – Helper utilities for the java.lang API
  • Commons logging – A generic logging implementation

There are other commons implementations that can save your time, but you are probably not using them. One of them is commons email. Commons email provides a facade to the underlying java mail services that come from mail.jar. Sending an email using mail.jar is not that difficult, but when you use commons email it is downright simple

Check out a sample program that sends a simple email

1
2
3
4
5
6
7
SimpleEmail email = new SimpleEmail();
email.setHostName("mail.myserver.com");
email.addTo("jdoe@somewhere.org", "John Doe");
email.setFrom("me@apache.org", "Me");
email.setSubject("Test message");
email.setMsg("This is a simple test of commons-email");
email.send();

Thats it. Use the HTMLEmail class to send HTML based emails. You can read more about the commons email API from their user guide. The project is active and should simplify the parts of your application that send email.

Categories: java Tags: , ,

Google wave java robot overview

October 22nd, 2009 CertPal 3 comments

appspotAs most of you are aware by now, developers can write java robots that can aid a conversation that happens in google wave. A conversation is a wavelet and each reply in this wavelet is called a blip. There are some ‘getting started’ tutorials available out there that are of great help. These links should help you

Official google wave guide
Google wave getting started – Sort of an abridged version of the official guide written by Vogella.

Grasping the overall picture of a java robot is a little difficult. This is because there are no flow or architecture diagrams (at least none that I know of) that show you the sequence of events. Given below is a diagram that does that. Assume that you wrote a java robot that is meant to edit blips in a wavelet. The robot should provide a profanity filter service which will delete objectionable words from the wave. This is how the series of events happen.

Categories: java Tags: , , ,

Java Interviews and trick questions

September 28th, 2009 CertPal 15 comments

java_interviewI have taken many java interviews over the last few years. As time passed by, I learned from mistakes I have made. One of them being to ask candidates trick questions or questions that do not necessarily have an obvious answer.

I read a blog post recently that detailed such a question. I will highlight the question here along with the answer

1
2
3
4
5
6
7
8
9
10
public class JavaPuzzler{
    public static void main(String[] args) {
        HashSet<Short> s = new HashSet<Short>();//1
        for(short i = 0; i<100;i++){//2
            s.add(i);//3
            s.remove(i-1);//4
        }
        System.out.println(s.size());//5
    }
}

Can you guess the answer to this question ? Simply drag and select the text near the spoiler to see the answer.

Spoiler: The answer to the question is 100. The gotcha is that the statement s.remove(i-1); at //4 will autobox to an Integer and not a Short. Equals comparison between an Integer and Short fails.

Categories: java Tags: , ,