Thinking in groovy the first chapter simplifies Java code __java with groovy

Source: Internet
Author: User
thinking in Groovy the first chapter simplifies Java code with groovy

Author: Chszs, reprint should be indicated. Blog home: Http://blog.csdn.net/chszs

1. Groovy's installation is currently in the latest version of Groovy is 2.1.2 version, download address: http://groovy.codehaus.org/Download

After downloading, unzip groovy-binary-2.1.2.zip to a directory, and then set the GROOVY_HOME environment variable to point to Groovy's decompression directory.

Then set the PATH environment variable and add%groovy_home%\bin.

To verify that the above installation is correct, on the command line, execute:

>groovy-v
Groovy version:2.1.2 jvm:1.7.0_09 vendor:oracle Corporation os:windows 7

Indicates groovy installation was successful.

2. Groovy's first exampleLet's write a Java class Todo.java, which reads as follows:
Import java.util.List;
Import java.util.ArrayList;


public class todo{
	private String name;
	private String note;


	Public Todo () {} public


	todo (string name, string note) {
		this.name = name;
		This.note = Note;
	}


	Public String GetName () {return
		name;
	}


	public void SetName (String name) {
		this.name = name;
	}


	Public String Getnote () {return note
		;
	}


	public void Setnote (String note) {
		this.note = Note;
	}


	public static void Main (string[] args) {
		list<todo> todos = new arraylist<todo> ();
		Todos.add (New Todo ("1", "one"));
		Todos.add (New Todo ("2", "two"));
		Todos.add (New Todo ("3", "three"));


		for (Todo todo:todos) {
			System.out.println (todo.getname () + "" + todo.getnote ());}}}

We compile it with Javac, run it in Java, and everything works. Now, we change the name of Todo.java to Todo.groovy, and then execute:
> Groovy Todo.groovy

We'll be amazed to see the same results as Java's execution.

Conclusion: Groovy is compatible with Java.

Running Java takes two steps: Compiling the class file with Javac, and then executing the compiled bytecode file on the JVM using Java.
Groovy, which compiles bytecode files at run time, saves the step of compiling in development.


3. Simplify Java code with groovyBelow, we use groovy to simplify JavaBean for Groovybean.
Import java.util.List;
Import java.util.ArrayList;


public class todo{
	String name;
	String Note;


	public static void Main (string[] args) {
		list<todo> todos = new arraylist<todo> ();
		Todos.add (New Todo (Name: "1", note: "one"));
		Todos.add (New Todo (Name: "2", note: "two"));
		Todos.add (New Todo (Name: "3", note: "three"));


		for (Todo todo:todos) {
			System.out.println (todo.name + "" + Todo.note);}}}

The execution results are still the same.

Visible
1 omitted the getter and setter method of the JavaBean;
2) By default, all class attributes are public;
3 The constructor function is very characteristic, the map way.

4. Use Groovy to further simplify the code
public class todo{
	string name
	string Note Public


	static void Main (string[] args) {
		def todos = new Arrayli St ()
		Todos.add (New Todo (name: "1", note: "One")
		Todos.add (New Todo (Name: "2", note: "two"))
		Todos.add (new TODO (name: "3", note: "three")) for
		(Todo todo:todos)
			println "${todo.name} ${todo.note}"
	}

The execution results are still the same.
Visible
1) List and ArrayList omitted the type;
2) omitting the semicolon;
3 The PRINT statement omits System.out.

5. Further omit the main () function with groovy
public class todo{
	string name
	string Note
}
def todos = [
	new Todo (name: ' 1 ', note: ' One '),
	new TODO (name: "2", note: "Two"),
	new Todo (Name: "3", note: "three")
]
todos.each{
	println "${it.name} ${ It.note} "
}

This code works fine on the Groovyconsole console.

Looking at the above, what do you think of groovy? In the next section, we will describe the key features of the groovy language.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.