Sunday, April 13, 2008

Do I really understand Java? or Computer Science, for that matter?

Like I mentioned in my previous post, I was with Javaground being interviewed for the Developer position. It was an overall amazing and a great learning experience. I am so much thrilled for having interviewed with them that I have been thinking a lot on how I could be like them. The java we write is so lame. I gave a talk on optimizations and now I feel embarassed that I am not eligible to give such a talk. And I did cover a lot of material and left out a lot. But talking with the Javaground team made me realize that I am not as good I felt I was.

I was honored to be in their guest house along with a developer from their Canada team. He was Nigel and I should say that he was amazing and very impressive, very pleasant to talk and very down to earth. He told me simple things that no one would ever find in a book, I bet. So the aspect he told me was "comparisons with a 0(zero) is much faster than with any other numbers". This was just one of the quiet a lot of information he shared with me. Though not always "java" we talked about, he told me various other things - about life as a mobile developer, how mobile game works, stuff like that.

Anyway, I decided I would look into the "comparison with zero" to understand more. So just now I wrote two simple programs - Hello and Hello2. The Hello.java looks something like this:

public class Hello{
        public static void main(String[] args) {
                 int i = 0;
                 if(i < 1) System.out.println("Test");
        }
}

The result of running javap is shown. (After compiling with javac)
javap -c -private -verbose Hello

Let me show you the output ....

Compiled from Hello.java
public class Hello extends java.lang.Object {
    public Hello();
        /* Stack=1, Locals=1, Args_size=1 */
    public static void main(java.lang.String[]);
        /* Stack=2, Locals=2, Args_size=1 */
}

Method Hello()
   0 aload_0
   1 invokespecial #1 <Method java.lang.Object()>
   4 return

Method void main(java.lang.String[])
   0 iconst_0
   1 istore_1
   2 iload_1
   3 iconst_1
   4 if_icmpge 15
   7 getstatic #2 <Field java.io.PrintStream out>
  10 ldc #3 <String "test">
  12 invokevirtual #4 <Method void println(java.lang.String)>
  15 return

I have made the lines of interest bold. Now my Hello2.java looks like this:

public class Hello2{
        public static void main(String[] args) {
                 int i = 0;
                 if(i < 0) System.out.println("Test");
        }
}

Now let us look at the javap output shown in the listing.

Compiled from Hello2.java
public class Hello2 extends java.lang.Object {
    public Hello2();
        /* Stack=1, Locals=1, Args_size=1 */
    public static void main(java.lang.String[]);
        /* Stack=2, Locals=2, Args_size=1 */
}

Method Hello2()
   0 aload_0
   1 invokespecial #1 <Method java.lang.Object()>
   4 return

Method void main(java.lang.String[])
   0 iconst_0
   1 istore_1
   2 iload_1
   3 ifge 14
   6 getstatic #2 <Field java.io.PrintStream out>
   9 ldc #3 <String "test">
  11 invokevirtual #4 <Method void println(java.lang.String)>
  14 return

As you can see the highlighted section again, we would notice an operation missing. So let me put the areas of interest together, side by side.

i < 1

i < 0

   0 iconst_0
   1 istore_1
   2 iload_1
   3 iconst_1
   4 if_icmpge 15
   0 iconst_0
   1 istore_1
   2 iload_1
   3 ifge 14

Frankly, I do not understand the bytecode so well yet, but I do can make out that in comparison with a 1, there is an instruction that creates the constant 1. Where as for comparison with 0, that instruction is missing. I could recall something similar that we learning during Computer Architecture class and the Embedded Systems course that I attended. I wish I invested more time in those classes. How foolish I was back then?

This is not a big deal when you have server class machines or a laptop for that matter. Saving a couple of instructions - no big deal. But in a mobile environment, it makes sense. So out of 200000 odd lines that we write, if we could only save 2 instructions per 100 lines, that would save us 2000 instructions !!! and it is a lot given the limited resources of a mobile device.

I am very interested in gaining such knowledge and it is only possible, at the moment, when we work at companies like Javaground. I could get a job as a web developer or as a C# developer at any point of time in my life but opportunities like Javaground does not often come. And I have worked very hard for this opportunity. I gave 3 rigorous online tests, attended around 6 interviews and after all I wish if only I could prepare better. "If" i am lucky and "if" I get an offer from Javaground which is good enough for me to make a proper living, then I would see myself learning a lot from the immense talent that Javaground possess and hopefully one day see myself as one of their core people. I know there are a lot of such firms but at the moment, the only such firm that has interviewed me is Javaground. 

The two days made me realize, there is so much I should learn!

No comments: