Skip to main content

Posts

Showing posts from March, 2009

Groovy types

Java is a statically typed language , which means we have to declare the type of a variable at compile time. Python is dynamically typed. which means that references can never to bound to types at compile time. Groovy on the other hand has optional typing. What this means is Groovy references can be statically typed like in Java, or dynamically typed like in Python. String staticStr = new String("this is a statically typed String"); staticStr = 8; println """The type of staticStr is: ${staticStr.class.getName()} """ println """It's value is: $staticStr""" The output form running the above program is: String staticStr = new String("this is a statically typed String"); staticStr = 8; println """The type of staticStr is: ${staticStr.class.getName()} """ println """It's value is: $staticStr""" As you can see, staticStr remains a string even a

Groovy Ranges

Groovy Collections offer us a class in addition to Java Collections, called Range. Range is a subclass of java.util.List and is used for creating lists of sequential values. Some ranges can be: 1..3 a..d Notice that we use the .. operator to create a range. However ranges are not limited to numbers and characters. Any object which implements the methods compareTo(), next(), and previous() can be used in a Range. Ranges typically have a left and right border, and they can be either inclusive (containing border elements) or exclusive (sans the right border element). Ranges are usually used to extract a subset of elements from a list or used in switch statements to match a range of cases together. The code below is self explanatory: //A simple Groovy Range def inclusiveRange = 1..10 println 'The size of inclusiveRange is: ' + inclusiveRange.size() //This range does not include the right border element def exclusiveRange = 1..<10 exclusiverange1 =" 1<..10" negativep

Groovy closures

Java does not have closures , but till sometime back it was a highly debated topic on various Java forums. Groovy is a language which compiles to Java bytecode and does have closures. Closures are very prominent in many programming languages, such as Groovy, Python, Ruby, Javascript, and probably many more. So what are closures? A closure is a block of code which can be passed to a function. Closures in Groovy are normal objects, which do not have any visible state. Closures are most often used in iteration logic and resource handling. Whenever we iterate across a collection, we normally do so to perform some logic on all or some members of the collection. Closures make it easy to represent that logic in a very concise and readable way. Closures can also be used for resource handling. Very often we open a file resource do something with the file (read from it, write to it, or both), and then close the file. The first and last parts, that is opening and closing the file are really plumb

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)