Difference Java Groovy Program
To move from one language to another, first thing comes in our mind is why should I use another language and what it simplifies. If you are a Java programmer and moving to Groovy then you would like to know first what is the basic difference between them and how Groovy makes programming easy . In this demo first I will show you difference between Java and Groovy using comparison then later through program.
To see more differences please visit groovy-lang.org differences and Groovy program structure
Now let’s see the differences using simple program:
- Java Program:
package com.javahonk; public class JavaTestProgram { private String name; /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } //Define test method public String printFullName(){ return "Java "+ name; } public static void main(String[] args) { JavaTestProgram javaTestProgram = new JavaTestProgram(); javaTestProgram.setName("Honk"); System.out.println("Name is: "+javaTestProgram.printFullName()); } }
- Above Java program converted to Groovy:
package com.javahonk class GroovyTestProgram { def name //Define test method def String printFullName(){ "Java ${name}" } static main(args) { def javaTestProgram = new JavaTestProgram(); javaTestProgram.setName("Honk"); println "Name is: "+javaTestProgram.printFullName() } }
- Groovy also supports scripting language where you don’t need to define well formed class structure and here you could write program with or without defining the class and call its method:
package com.javahonk; class GroovyScriptTest { def name //Define test method def String printFullName(){ "Java ${name}" } } def groovyScriptTest = new GroovyScriptTest(); groovyScriptTest.name = "Honk" println "Name is: "+groovyScriptTest.printFullName()
- Output of all programs:
- For more details please visit Groovy official documentation