2 Make it More Simple and Powerful

As dynamic programming language, groovy can be used to minimize java code into more simple and powerful code. From the previous article, I write Employee.java to be Employee.groovy that is still with the same code but different extension. But now, we will make that code to be more simple one.


public class Employee{
String name;
String address;
String jobDescription;

public static void main(String [] args){
List<Employee> employees = new ArrayList<Employee>();
employees.add(new Employee(name:"Achmad", address:"Jember", jobDescription:"Programmer"));
employees.add(new Employee(name:"Fauzi", address:"Bangkalan", jobDescription:"Artist"));
employees.add(new Employee(name:"Fauzi Achmad", address:"Jember", jobDescription:"Web Designer"));

for(Employee employee:employees){
println "${employee.name} ${employee.address} ${employee.jobDescription}";
}
}
}

From the code above, we can analyze that import, setter, getter and constructor does not need to be written again. Not only that, check out the output command : we don’t need to write System.out.println() again, but only simple “println” code which is capable to combine between variable and string in one apostrophe sign. It is so great, isn’t it ? On the other hand, we can change for command to be


public class Employee{
String name;
String address;
String jobDescription;

public static void main(String [] args){
def employees = [
new Employee(name:"Achmad", address:"Jember", jobDescription:"Programmer"),
new Employee(name:"Fauzi", address:"Bangkalan", jobDescription:"Artist"),
new Employee(name:"Fauzi Achmad", address:"Jember", jobDescription:"Web Designer")
]

employees.each{
println "${it.name} ${it.address} ${it.jobDescription}"
}
}
}

As the native List and ArrayList class groovy can convert it into their own mode by using def command. Def means anything, we can make variable or method using that command. The next explanation is keyword it. That command same as this keyword in java. So, the conclusion is, we can make java program more simple and more powerful using groovy

Tagged: , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: