Groovy study notes two basic grammar

1. Default Import
The following import list is always hidden in the Groovy code:
groovy.lang.*;
groovy.util.*;
java.lang.*;
java.io.*;
java.math.BigDecimal;
java.math.BigInteger;
java.net.*;
java.util.*;
 
If the need to introduce other java class can be used directly in a java import statement import, such as java.math *.;
2. Digital Processing
Groovy in underlying representation of floating-point numbers in Java BigDecimal, but it will try to ensure that the behavior of BigDecimal meet the expectations of developers.
3. variable, dynamic and static type, scope,
(1) dynamic and static type
Groovy is a dynamic language, it is not necessary to specify the type of the type of a variable, the variable is determined when the statement (or return).
If you declare a static type variable, with no incorrect type when assigning a value to it, Groovy can check it out, run the following code, will get an error
a=new Date()
a=1;
Date b=new Date();
b=2.0;
Exception thrown
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '2.0' with class 'java.math.BigDecimal' to class 'java.util.Date'
 at ConsoleScript4.run(ConsoleScript4:4)
That is, b = 2.0; it performs an error, it can not be assigned the value 2.0 Date type variable b.
(2) Groovy in scope
For Groovy in the class, its scope and Java, classes, methods, variables circulation scope, their scope is related to your thought. But Groovy script is different.
Simply put, Groovy script, there are two scopes
  1. Binding domain, the binding domain is the global scope of the script.
  2. This area, this area is limited to the scope of a variable declaration code blocks thereof. For variables (such as the top of the script) declared within the script code block, if it is defined over variables, whose scope is defined in its local domain.
Defined variables are declared as static type, or with a special keyword def defined variables (that it is undetermined type of variable definitions).
    Methods declared in the script can not access this area.
 String hello="Hello World";
    void printHello()
    {
        System.out.println(hello);
    }
    printHello();
    It will appear the following exception:
Exception thrown
    groovy.lang.MissingPropertyException: No such property: hello for class: ConsoleScript6
   at ConsoleScript6.printHello(ConsoleScript6:4)
   at ConsoleScript6.run(ConsoleScript6:6)
    But if you remove the String, then the normal output Hello World.
(3) lists and maps grammar
    Groovy中的列表和映射在底层是由Java ArrayList和LinkedHashMap实现的。
    Groovy用方括号[]指定和使用列表结构.
jvmLanguages=["Java","Groovy","Scala","Clojure"];
println(jvmLanguages[0]);
println(jvmLanguages.size());
println(jvmLanguages[0..2])
println(jvmLanguages[-1])
jvmLanguages=[];
println(jvmLanguages);
    输出为:
Java
4
[Java, Groovy, Scala]
Clojure
[]
Groory是动态类型语言,所以可以把不同类型的只保存在列表和映射中。
 jvmLanguages=["Java",2,"Scala",new Date()];
 println(jvmLanguages);
    输出为:
[Java, 2, Scala, Mon Jan 05 14:22:22 CST 2015]
    Groovy处理映射也差不多,用[]符号,并用冒号(:)来分开键值对,以映射.键(map.key)的方式去引用映射中的值。
    
languageRatings=[Java:100,Groovy:99,Clojure:"null"];
println(languageRatings["Java"]);
println(languageRatings.Clojure)
languageRatings["Clojure"]=75;
println(languageRatings["Clojure"]);
println(languageRatings);
languageRatings=[:];
println(languageRatings);
    输出为:
100
null
75
[Java:100, Groovy:99, Clojure:75]
[:]
   4.与Java差异
    (1)Groovy有大量可以省略的语法:
  1.      语句结束处的分号;
  2.     返回语句(return);
  3.     方法参数两边的括号;
  4.     public访问限定符。
 Demo demo=new Demo();
println(demo.doSomething());
public class Demo
{
     public Integer doSomething()
     {
          def x=1
          def y;def String z="Hello World"
          println "the doSomething method be invoked!"
          x=5
      }
}
    输出为:
the doSomething method be invoked!
5
   如果把上面代码中的public都去掉,输出还是一样
   (2)异常处理
    Groovy不区分检查异常和未检查异常。Groovy编译器会忽略方法签名中的所有throws语句。
    (3)相等操作符
    遵循最小以外原则,Groovy把==当做Java中的equals()方法。
    
Integer x=new Integer(2);
    Integer y=new Integer(2);
    Integer z=null;
    if(x==y)
    {
        println "x==y"
    }
    if(!x.is(y))
    {
        println "x is not y"
    }
    if(z.is(null))
    {
        println "z is null"
    }
    if(z==null)
    {
        println "z is null"
    }
 
 输出为:
x==y
x is not y
z is null
z is null
(4)内部类
  Groovy支持内部类,但大多数情况下我们应该用函数字面值替代它。

 

发布了250 篇原创文章 · 获赞 7 · 访问量 3万+

Guess you like

Origin blog.csdn.net/bsr1983/article/details/84694373