A scripting language that runs on Java. It is the result of adding the necessary functions to Java.
Hello Groovy World
println("HelloWorld");
Also try GUI (AWT Frame). Below is the execution screen of Hello Groovy World (AWT).
Example) HelloGroovyWorld(AWT)
/* Hello World (GUI)script*/
import java.awt.*;
import java.awt.event.*;
/**
*MainFrame class
*/
class MainFrame extends Frame
{
def _menuBar;
def _labelBody;
/**
*constructor
*/
MainFrame() {
/*Initialize title*/
setTitle("Hello World")
/*Initialize menu*/
_menuBar = new MenuBar();
def _menu = new Menu("File");
def _menuItem = new MenuItem("close");
_menuItem.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e) {
closeFrame(e);
}
});
_menu.add(_menuItem);
_menuBar.add(_menu);
setMenuBar(_menuBar);
/*Initialize in-frame display content*/
_labelBody = new Label("Hello World");
this.add(_labelBody);
/*Initialize size*/
setSize(new Dimension(300, 200));
}
/*Frame close operation*/
def closeFrame(e)
{
this.dispose();
}
}
/*Entry point*/
new MainFrame().show();
Reference: Apache Groovy --Differences with Java Reference: Code Zine --What kind of language is Groovy? Introduction to Groovy for Java Programmers
--Some of the class packages that are commonly used are imported by default, so you can use them without declaring them.
clodure = {println ("$ {it} passed. ");}; Clodure ("test");
,clodure = {name, age-> println ("$ {name} is $ {age" } I'm old. ");}; Clodure ("Alice", 10);
--Closers can be specified as arguments. Therefore, by giving it to the argument of each method etc., access to all the elements of the array can be easily described without using the control syntax. sum = 0; (1..10) .each {sum + = it}; println (sum);
--Closers can access variables of the declared class. Therefore, event-driven operation can be easily defined.
--The initialization format of the array is different. (Because {} is reserved by the closure) ʻint [] array = [1,2,3]; --When the access controller is omitted when defining a variable, it is recognized as a private field and Getter / Setter is automatically defined. If you want to define it as a valid package-private in the package, describe @PackageScope. (Although it is written as private, it seems that access control is not performed) --Getter / Setter method names are
getXXX (),
setXXX (). * If an error occurs with a method name that you have not declared at compile time, this behavior may be the cause. --Automatic Resource Management (ARM) cannot be used. Equivalent processing can be done using closures.
new File ('C: \ path \ to \ file.txt'). eachLine ('Shift-JIS') {println it;}--The behavior of anonymous inner classes follows Java guidelines, but the behavior is different. See Groovy Documentation for more details. --Lambda expressions defined in Java 8 can be recognized as anonymous inner classes, but Groovy gives a syntax error. Use closures instead. --String literals enclosed in double quotes are treated as GString type values. --String literals enclosed in single quotes are treated as String unless explicitly declared as char. Casting a string of two or more characters as char treats it as char with only the first character. --Since the data type is recognized as an Object and auto-trapped to a primitive type etc., it may differ from the result of widening / narrowing conversion processing. --The behavior of the comparison operator
== is promiscuous comparison in Java, but in Groovy it is replaced by ʻa.compareTo (b) == 0
or ʻa.equals (b) . Use ʻa.is (b)
to verify identity.
--The behavior of implicit type conversion in Groovy is different from Java. See the Groovy Documentation for more details.
--In addition, there are ** as **, ** def **, ** in **, ** trait **. (Reserved character)--Loose source code writing rules compared to Java (opposite of Strict). For example, the colon at the end of the line is unnecessary and you do not have to declare the data type. You can also write statements that do not belong to any class. (Loose statement)
-Declaring a variable with def dynamically changes the appropriate type according to the value assigned to the variable.
--Map type is available. map = ["Alice": 10, "Bob": 20, "Charlie": 30]; println (map ["Alice"]);
--In the control syntax, it is implicitly false if the evaluation result is 0 or null, and true otherwise.
--The switch statement can be divided into cases by evaluating other than numerical values.
--The format of the for statement is different. for (m in list) {print m.toString ();};
--A dynamic class definition (Expando) function is provided. def e = new Expando (); e.msg =" Hello World "; e.show = {println (msg);}; e.show ();
--The java.sql package makes it easy to write SQL query processing.
--Builder allows you to easily describe structured data such as HTML / XML / Swing. ʻImport groovy.xml.MarkupBuilder; new MarkupBuilder (). html {body {div {mkp.yield ('Hello World')}; div (style:'style.css') {mkp.yield ('Groovy')} }} `
Recommended Posts