Skip to main content


Showing posts from February, 2009

Learning Groovy

I have started learning the Groovy programming language . Looks like dynamic languages are getting really popular these days, and keeping up with my resolution to learn a couple of programming languages this year, I have started learning Groovy. In short Groovy is a dynamic language which fully interacts with Java. Groovy can invoke Java objects, in fact it itself compiles to bytecode, so Java objects can also invoke Groovy. In the next few posts I will share what I learn and my notes comparing Groovy with Java. Let's start with a HelloWorld in Groovy println 'Hello World' That's it. You do not need to enclose your HelloWorld in a class or the main method. This is really cool if you just want to create a simple script file to do something quickly for you. But how does this get compiled to Java bytecode. Java always needs a class and methods to hold code. Well in my case the Groovy plugin in Eclipse automatically compiled the above code and put it in a class with the sa

What is type erasure in Java?

I had read about type erasure in Java a long time back. However, today when a friend asked me a question related to type erasure, I found myself not quite certain of the answer. I read up on it again and here is what I learned. Angelika Langer has an excellent FAQ , where she explains generics, and type erasure. According to the FAQ: A process that maps a parameterized type (or method) to its unique byte code representation by eliding type parameters and arguments. OK, let's understand what that means. Below is a simple class which uses the generified version of Java Lists. package  net.adaptivelearningonline.examples.generics; import  java.util.ArrayList; import  java.util.Iterator; import  java.util.List; public class  GenericsErasure  {    public static  void  main ( String args []) {      List<String> list =  new  ArrayList<String> () ;      list.add ( "Hello" ) ;      Iterator<String> iter = list.iterator () ;      while ( iter.hasNe

Bit manipulation in Java

I have always had a hard time remembering rules of bit manipulation in Java. So, when someone asked this question on , I knew he had to do masking, but I forgot why. I decided to look up bit wise operations on Wikipedia to refresh my memory. However, this time I am also blogging the answer so I don't forget (yet another use of blogging :-) ). There are two types of bit shift operations: arithmetic shift, and logical shift. An arithmetic right shift preserves the sign bit, while logical shifts always insert a zero. Representing -1 as a signed byte we get: 11111111 -1 >> 1 gives us: 11111111 -1 >>> 1 gives us: 01111111 so by this logic (-1 >>> 8) should give us 00000000 which is 0 Well not so: byte b = -1; System.out.println("-1 >>> 8 = " + (b >>> 8)); The output I get is: -1 >> 8 = 16777215 Hmmm. what just happened? Java converted the signed byte into a signed int, and then did an arithmetic right shift of

Automated release tagging with ANT

Sometime back I was researching how to automatically tag a CVS snapshop from an ANT script. Yes, I know this should be a basic thing :-) But for some reason, IT has always taken care of tagging and creating builds. Whenever I needed to release some code, I would tag the CVS snapshot from Eclipse, I would manually creat a MANIFEST.MF file, and then create the distributable. However, this time I tried to automate the task with an ANT script. What I needed was an ANT script which would automatically create a MANIFEST.MF file with a release number, and tag the CVS project with that release number. The first thing I came across was the Ant cvs task . This task allows us to run any CVS command on a repository. However, there is a catch. You need CVS installed on your local machine for this to work. Under the hoods the CVS task invokes cvs.exe (on Windows), or cvs on *nix machines. This was a clear show stopper. I did not want to make all developers install CVSNT on their development mach

Getting interested in polyglot programming

From the last few days I have started learning Groovy by listening to the Groovy series podcasts . While listening to the podcast on numbers ( attached_code ), I couldn't help smiling when I saw this (line 42 in the attached code): assert 1/2 == 0.5 Yes 1/2 is actually 0.5 in Groovy. Isn't that awesome :-) We all know what we get when we write this code in Java: System.out.println("1/2 = " + 1/2); A few other things I found interesting about Groovy numbers are: def bigDecimalObj = 5.12345 assert == 'java.math.BigDecimal' Groovy treats all floating point numbers as BigDecimal (there are exceptions, but I will not get into that here). We all know how difficult it is to implement high precision calculations in Java. We cannot use double, because we may lose precision in calculations involving double. The alternative of using BigDecimal results in unreadable code. However, Groovy can be compiled to bytecode and can coexist with Java. So n

Coding and learning and sharing and earning :-)

Sometime in 2005 I created a website to share practical information on software programming and design. The idea was to share practical programming tips, best practices, howtos, gotchas, etc. The material would come from what I learn writing actual code, and the medium to share it would be a blog, audio, and screencasts. I started this venture with the intention of doing something I enjoyed, and generating revenue. The website grew steadily, though very gradually. After about a year and a half I had about 27,000 page hits a month, and a few comments per post. I know it is very modest, but I believe it was at a point where it would have grown much more rapidly form there on. But due to various reasons, I was not able to dedicate enough time to working on the website, and eventually I shut it down. Even though I shut down the website sometime last year, the desire to be able to work on open source software (that has a social impact), share information, and generate revenue from it, pers

Creating Boolean Objects

Have you ever written this line of code? Boolean b = new Boolean(false); According to FindBugs (and rightly so) should not instantiate Boolean objects in Java as we did above. Instead we must use the creational method provided to us. Boolean b = Boolean.valueOf(false); This is better for performance. Since we can have only two Boolen values, either true or false, the JVM can cache both these objects, and we can reuse them across our application by using the above creational method, instead creating new Boolean objects every time we need them. Discuss this post in the learning forum .