Skip to main content

Posts

iAccelerator

iAccelerator is an initiative by The Centre for Innovation Incubation and Entrepreneurship at IIM Ahmedabad. It is a startup assistance program for first time technology entrepreneurs in India. Accepted applicants will receive upto 5 Lacs in funding. Selected applicants will be expected to stay for 4 months in Ahmedabad to work on their startup. Here they will also receive help in various other aspects (legal, management, technical) of building and running their startup. The last date for applications is April 1st.

Groovy Lists

In a previous post I talked about Groovy numbers. In this post we discuss Lists in Groovy. One thing that strikes me is the use of operators in Groovy. Groovy has reduced verbosity wherever possible with the use of operators. We create a List in Java like this: List list = new ArrayList (); and in Groovy like this: def list = [] Why do you think we do not use generics in Groovy? You can reply on Twitter . Groovy allows us to apply several operators like +, -, *, and << //Lets create an empty list. Notice, we can create a list with the [] //operator def aList = [] //size is also an attribute in Lists (we cannot invoke methods //without the ()) println 'size if aList is: ' + aList.size println 'Lists created with the [] operator are of type: ' + aList.class.getName() //What if I want a linked list? def lList = [] as LinkedList println 'lList is of type: ' + lList.class.getName() //We can also create a LinkedList by specifically instantiating it //li...

Groovy numbers

I was very pleasantly surprised when I learned how to work with numbers in Groovy. The first difference between Groovy and Java (while dealing with numbers) is that in Groovy numbers are always objects. There are no primitive numbers like in Java. This may have the disadvantage of increased memory usage, but it also has the advantage that working with large numbers is much easier. If you have tried doing high precision calculations or working with really large numbers in Java, then you know how difficult it is to work with BigDecimal and BigInteger to perform calculations. We have to call methods instead of using operators. Let's see this with a small example. If I want to print the value of Long.MAX_VALUE * 2, I have to use BigInteger and then invoke it's multiply method as shown in the example below: System.out.println(Long.MAX_VALUE * 2); //Incorrect: prints -2 //The right way to work with large numbers in Java BigInteger bi1 = new BigInteger("9223372036854775807")...

String handling in Groovy

This blog post shows through a simple example, how to manage strings in Groovy. Here are a few things which are different in Groovy strings: Groovy strings can be enclosed within single quotes: 'this is a string' double quotes: "this is a string" triple (single or double) quotes: '''this is a string''' and """so is this""" Groovy has something called GStrings which uses replacement variables Groovy makes it easier to use strings in regular expressions The == operator in Groovy actually invokes the equals() method Groovy overloads the +, -, and * operators in Strings With these concepts in mind the example below should be self explanatory. Try running this example yourself and see the output. After that change the source and see how it affects the output. //Groovy allows single quoted strings def string1 = 'hello world' println string1 //Groovy allows double quoted strings (GStrings have to be double quoted)...

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> () ;  ...

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 Stackoverflow.com , 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...