Automatic construction of Gradle (I) Groovy

Keywords: Java jvm SDK JDK

Official website: http://groovy-lang.org/

DSL: domain specific language Groovy is a branch of DSL, others are like uml sml xml html
    Features: solve the problems in a specific field, compile the fields not involved in the language, combine the compiled language, and improve the development of the project

Groovy : 
    It is an agile development language based on JVM. groovy can be compiled into a class file to be executed by the JVM, or it can interpret the script written like a scripting language
    groovy is a perfect combination of java and can use all java libraries. Is an extension of java

    Syntactically, it supports dynamic types, closures and other new generation language features
    It supports both object-oriented and process oriented programming

Environment building: download the groovy SDK on the official website. All operating systems use the same SDK
    Then configure. \ groovy-2.5.2\bin to the environment variable, and you can use groovy-v or - version to view the version information and corresponding jdk and operating system (32-bit jdk is not allowed)

    E:\gradle\groovy-2.5.2\bin\groovy and groovyc command correspond to java and javac commands
    E:\gradle\groovy-2.5.2\bin\groovysh commands to interpret groovy scripts

Variable type

// groovy syntax
int x = 10 // Strongly typed definition variable
println x.class // There is no basic data type in class java.lang.integer groovy, only object type

// Weak type def definition variable
def a = 11
println a.class // class java.lang.Integer
a = "abcd" // The def variable can be reassigned with another data type.. DEF is similar to the Object data type. java uses the Object object Object to assign values like this without any error...
println a.class // class java.lang.String
def b = 3.14
println b.class // class java.math.BigDecimal
def c = 'str'
println c.class // class java.lang.String

Character string

def a = 'h\'\"j' // '' or '' wrap string can be used in groovy. Special characters are also escaped with
println a.class

def b = '''\
a b
c
''' // What a string written in three quotes looks like and what the output looks like? Single quotes don't work. In the same way, "'" and "" "" "
/* // Output, the \ in the beginning '\' indicates that the next line is the same as this line, and there can be no spaces after the \. There is a carriage return after c, so a blank line will also be printed when printing
a b
c

*/
println  b // b.class is also class java.lang.String

def c = "android" // The difference between '' and '' is that the string in '' can be changed, while the string in '' cannot be changed
println c.class // class java.lang.String
def d = "hello ${c}" // $c means to take variable value, while ${} means not only variable but also any expression in {}
println d // hello android
println d.class // class org.codehaus.groovy.runtime.GStringImpl
def e = "2 + 3 = ${2+3}"
println e // 2 + 3 = 5

def f = echo(e)
println f // 2 + 3 = 5, org.codehaus.groovy.runtime.GStringImpl and java.lang.String can be converted to each other
String echo(String msg) {
    return msg
}

// -----------------String extension methods ------------ from java.lang.String, DefaultGroovyMethods and StringGroovyMethods
// Parameters of normal type
def g = "groovy"
println g.center(9, "a") // Arroovyaa, center is the filling string, g is the center, and the second parameter is not passed, which means the space is filled
println g.padLeft(9, 'a') // Aaagrovy, fill in the left side of the string, padRight function, fill in the right side
println g.leftShift('a') // groovya, fill string

def h = 'Hello'
println g > h // true, the operator compares the unicode code of the String, which is the same as the String function g.compareTo(h) // h compares the unicode code of G
println h[0] // H. [] operator, equivalent to h.getAt(0)
println h[0..1] // He, 0.. 1 indicates subscript range
def i = 'someHello@Hello'
println i - h // some@Hello, which is equivalent to i.minus(h), subtracts the contained h string from I only once
println i.reverse() // olleH@olleHemos, reverse string
println i.capitalize() // SomeHello, initial capital
def j = '10'
println j.isNumber() // true
println j.toInteger() // 10

// Parameters of closure type

Posted by JUMC_Webmaster on Sun, 05 Jan 2020 08:24:21 -0800