Skip to main content

Posts

Showing posts from June, 2012

Java Vs Groovy - Enum Demo

 Java 5 introduced enum that solve the problem with enumeration. Groovy also support that feature, as shown here.  Enum example using Java. enum ShirtSize { SMALL,MEDIUM,LARGE,EXTRALARGE } class ShirtImplementation { public void orderShirt(ShirtSize size) { switch(size) { case SMALL : System.out.println("The size of the shirt is small"); break; case MEDIUM : System.out.println("The size of the shirt is medium"); break; case LARGE : System.out.println("The size of the shirt is large"); break; case EXTRALARGE : System.out.println("The size of the shirt is extra large"); break; default : System.out.println("The shirt size is not available"); break; } } } public class EnumExample { public static void main(String args[]) { ShirtImplementation shirtImplementation = new ShirtImplementation(); shirtImplementation.orderShirt(ShirtSize.SMALL); shirtImplementation.o

Groovy vs Java - varars demo

 Java 5 varargs feature allows you to pass a variable number of arguments to methods. Groovy also support this feature, See the below example.  Java way for implementing varargs. public class VarargsDemo { public static void main(String[] args) { int[] numbers = {10,20,30,40,50}; System.out.println("Total is " + VarargsDemo.calculateTotal(numbers)); } public static int calculateTotal(int... numbers) { int total = 0; for(int number : numbers) { total += number; } return total; } }  Groovier way for implementing varargs. def calculateTotal(int... numbers) { def total = 0 for(number in numbers) { total += number } total } println calculateTotal(10,20,30,40,50)

Groovy vs Java - For-Each loop

 The latest version of Java (version 1.5 and later) have for-each loop support. Groovy have many type of powerful loops as well as it support traditional Java loops.  This is very simple example for for-each loop.  Java way for implementing for-each loop public class ForEachLoopDemo { public static void main(String[] args) { String[] countryLists = {"India","US","UK"}; for(String country : countryLists) { System.out.println(country); } } }  Groovier way for implementing for-each loop for(country in countryList) { println country }

Groovy vs Java - Autoboxing

 By default Groovy have autoboxing feature because of its dynamic nature. By default Groovy treats all the primitive types as objects, for example Integer for int, Float for float etc. See the below example. int integerVariable = 10; println integerVariable.class.name float floatVariable = 0.5f println floatVariable.class.name char charVariable = 'c' println charVariable.class.name double doubleVariable = 1.5d println doubleVariable.class.name boolean boolVariable = true println boolVariable.class.name  Here I have created the some variable for primitive types and print the corresponding class name. This result would be, java.lang.Integer java.lang.Float java.lang.Character java.lang.Double java.lang.Boolean   In Java, auto boxing and unboxing involve constant casting. But Groovy automatically treats all the primitive types variable as Object. That's why the print statement prints the corresponding class name for primitive types.

Groovy Vs Java - Exception Handling

 Some times java forces you to handle checked exception. Consider simple case: If you want to create object for FileInputStream, Java forces you to catch "FileNotFoundException".  For catching this exception almost every developers use  empty catch block. That's why normal Java application have lot of empty catch blocks like this. import java.io.FileInputStream; import java.io.FileNotFoundException; public class ExceptionHandling { public static void main(String[] args) { try { FileInputStream fin = new FileInputStream("D://sample.txt"); } catch (FileNotFoundException e) { } } }  But Groovy does not force you handle the exception.  If any exception you don't handle is automatically passed on to a higher level. Groovier way for above example def openFile() { //Here the exception is automatically redirected to caller method. new FileInputStream("D://sample.txt") } try{ openFile() } catch(FileNotFoundException e) { prin

Groovy - Opening system applications

 In Java we have exec() method for opening system applications. In groovy this is more easier than Java. This is groovier way for opening notepad application. "notepad.exe".execute()  When you called the execute() method on String object, Groovy created the instance that extends java.lang.Process ,  just like exec() method of RunTime did in the Java code.   You can verify this by using following code. println "notepad.exe".execute().class.name  This will print " java.lang.Process "

Sencha Touch - Creating custom views

 Sencha allow developers to easily develop MVC based mobile application. Now I am going to explain the steps for creating custom views in sencha application. Before going to start, see my previous post for understanding the folder structure of sencha touch application ( here ).  Inside the view folder I am going to create a MyView.js file with following code. Ext.define('SenchaDemo.view.MyView',{ extend : 'Ext.Panel', xtype : 'myview', config : { fullscreen : true, html : 'This is my own view' } });  Here I have extended a Panel class and included a simple html content, whenever it's render, it's says "This is my own view". The "SenchaDemo.view.MyView" have special meanings.     1. The " SenchaDemo " refer my application name.     2. The " view " refer the view folder present in app folder.     3. " MyView " is my view name.