gr8vyguy@Blogjava

Groovy读书笔记2

Chapter 3 The simple Groovy datatypes

everything as an object and all operators as method calls

Java’s Type System

Java distinguishes between primitive types (such as int, double) and reference types (such as Object and String).

primitive types have value semantics.   fixed set in Java, no method calls
reference types have pointer(reference) semantics.  no operators

Groovy’s Type System

Groovy does not use primitive type in Java, use wrapper classes for that.
      int value   => define an Integer instance

Literals
     15, 0x1234FFFF                   Integer as default
      100L,                                     L as Long
      12.3F                                     F as Float
       1.23D                                   D as Double
       123G                                    as BigInteger
       1.23, 1.23G                        G as BigDecimal as default

Autoboxing
      primitive types  <-->   wrapper classes

Optional Typing

      explicit static typing             int value
      implicit dynamic typing       def value,  Groovy assumes java.lang.Object

      duck typing

A rule of thumb

      As soon as you think about the static type of a reference, declare it; 
      when thinking “just an object,” use dynamic typing.

Operator Overriding, Operator Overloading, Operator Implementing
     
      Operators are shorthand for method calls.

a++和++a都是调用a.next(), 一定有办法区分是pre还是post的,next才好返回pre还是post的value

an operation is closed under its type.

coercion
     one argument promoted to the more general type.

    BigDecimal is more general that Integer.  Super Set vs Sub Set

String, GString

    String Interpolation, "1 + 2 = ${1+2}"

    'c' as char: coerce a string into a character

    "... ${...} ..."  {} denote a closure

Regular Expression

    declaration what, instead of programming how

    Groovy's regular expression relies on Java's regex.

    A pattern is a regular language.

    不像在理论计算机, 只是有Alphabet和几个Operator组成Pattern, 这里主要有Symbol构成

    Uppercase symbols define the complement. d for digit, D for non-digit

   greedy match mode and restrictive match mode

Number

    Coercion
       if either operand is Double or Float, then the result is Double
       assert (1.1D * 1.2G) instanceof Double
       assert (1.1F * 1.2G) instanceof Double

    Coercion if exceeds the current range only for **

    Division
          if any of the arguments is of type Float or Double, then the result is Double
          otherwise BigDecimal

                1 / 2 ==> BigDecimal
                1.intdiv(2) ==> Integer Division

   == coerces to the more general type before comparing.
         assert 1.1G == 1.1G
         assert (1.1G == 1.1D) == false
               1.1 in Binary Radix is infinite, 
               1.1 can’t be exactly represented as a Double(IEEE Floating Number Format).
               but BigDecimal can do that.




posted on 2007-12-12 13:46 gr8vyguy 阅读(657) 评论(0)  编辑  收藏 所属分类: Java


只有注册用户登录后才能发表评论。


网站导航:
 
<2007年12月>
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

导航

统计

公告

  • 转载请注明出处.
  • msn: gr8vyguy at live.com
  • 常用链接

    留言簿(9)

    随笔分类(68)

    随笔档案(80)

    文章分类(1)

    My Open Source Projects

    搜索

    积分与排名

    最新评论