《Groovy語言規範》-語法(二)
4.6.斜杠字符串
除了通常的帶引號字符串,groovy提供斜杠字符串,使用/作為分隔符。斜杠字符串對於定義正則表達式和模式是特別有用的,因為不需要轉義反斜杠。
一個斜杠字符串:
def fooPattern = /.*foo.*/
assert fooPattern == '.*foo.*'
隻有正斜杠需要反斜杠轉義:
def escapeSlash = /The character \/ is a forward slash/
assert escapeSlash == 'The character / is a forward slash'
斜杠字符串是多行的:
def multilineSlashy = /one
two
three/
assert multilineSlashy.contains('\n')
斜杠字符串也能被插值(如,GString):
def color = 'blue' def interpolatedSlashy = /a ${color} car/
assert interpolatedSlashy == 'a blue car'
有幾個陷阱需要注意:
一個空的斜杠字符串不能使用雙正斜杠表示,因為它被Groovy解析器作為一個單行注釋理解。這就是為什麼以下斷言實際上無法編譯,因為它看起來像一個無終止的語句:
assert '' == //
4.7.美元符修飾的斜杠字符串
美元符斜杠字符串是一個有開口$/和閉口$/界定的多行GString。這轉義字符是美元符,它能轉義另一個美元符,或一個正斜杠。但是雙美元符和雙正斜杠不用被轉義,除了轉義像GString占位符序列開始的字符串子序列的美元符,或者你需要轉義一個序列,開頭看著像閉包美元符斜杠字符串分隔符。
示例:
def name = "Guillaume" def date = "April, 1st"
def dollarSlashy = $/ Hello $name, today we're ${date}.
$ dollar sign $$ escaped dollar sign \ backslash / forward slash $/ escaped forward slash $/$ escaped dollar slashy string delimiter /$
assert [ 'Guillaume', 'April, 1st', '$ dollar sign', '$ escaped dollar sign', '\\ backslash', '/ forward slash', '$/ escaped forward slash', '/$ escaped dollar slashy string delimiter' ].each { dollarSlashy.contains(it) }
4.8.字符串總結表
String name String syntax Interpolated Multiline Escape character Single quoted '…' \ Triple single quoted '''…''' 是 \ Double quoted "…" 是 \ Triple double quoted """…""" 是 是 \ slashy /…/ 是 是 \ Dollar $/…/$ 是 是 \
4.9.字符
與Java不同,Groovy沒有顯式的字符文本,然而你可以通過三種不同方式,可以將Groovy字符串實際作為一個字符使用。
char c1 = 'A' (1) assert c1 instanceof Character
def c2 = 'B' as char (2) assert c2 instanceof Character
def c3 = (char)'C' (3) assert c3 instanceof Character
(1)當定義變量時,通過指定char類型,使變量包含字符
(2)通過as操作符使用類型強製轉換
(3)通過char操作符做類型轉換
第一個選項是(1)有趣的當一個字符在一個變量中,而另外兩個(2和3)是更令人關注時char值必須作為一個方法調用的參數。
5.數字
Groovy支持不同類型的整數和小數,通常以Java的Number類型返回。
5.1.整數
整數類型與Java相同:
- byte
- char
- short
- int
- long
- java.lang.BigInteger
你能以如下定義創建這些類型的整數:
// primitive types byte b = 1 char c = 2 short s = 3 int i = 4 long l = 5
// infinite precision BigInteger bi = 6
如果你通過使用def關鍵字使用可選類型,那麼整數的類型將是可變的:它取決於這個類型實際包含的值。
對於正數:
def a = 1 assert a instanceof Integer
// Integer.MAX_VALUE def b = 2147483647 assert b instanceof Integer
// Integer.MAX_VALUE + 1 def c = 2147483648 assert c instanceof Long
// Long.MAX_VALUE def d = 9223372036854775807 assert d instanceof Long
// Long.MAX_VALUE + 1 def e = 9223372036854775808 assert e instanceof BigInteger
對於負數也一樣:
def na = -1 assert na instanceof Integer
// Integer.MIN_VALUE def nb = -2147483648 assert nb instanceof Integer
// Integer.MIN_VALUE - 1 def nc = -2147483649 assert nc instanceof Long
// Long.MIN_VALUE def nd = -9223372036854775808 assert nd instanceof Long
// Long.MIN_VALUE - 1 def ne = -9223372036854775809 assert ne instanceof BigInteger
5.1.1.可選擇的非十進製表示
二進製數
在Java6及以前和Groovy一樣,數字隻能使用十進製,八進製和十六進製表示,使用Java7和Groovy2你能使用0b前綴作為一個二進製符號:
int xInt = 0b10101111 assert xInt == 175
short xShort = 0b11001001 assert xShort == 201 as short
byte xByte = 0b11 assert xByte == 3 as byte
long xLong = 0b101101101101 assert xLong == 2925l
BigInteger xBigInteger = 0b111100100001 assert xBigInteger == 3873g
int xNegativeInt = -0b10101111 assert xNegativeInt == -175
八進製數
八進製數使用0後麵跟八進製數的典型格式表示。
int xInt = 077 assert xInt == 63
short xShort = 011 assert xShort == 9 as short
byte xByte = 032 assert xByte == 26 as byte
long xLong = 0246 assert xLong == 166l
BigInteger xBigInteger = 01111 assert xBigInteger == 585g
int xNegativeInt = -077 assert xNegativeInt == -63
十六進製數
十六進製數使用0x後麵跟十六進製數的典型格式表示。
int xInt = 0x77 assert xInt == 119
short xShort = 0xaa assert xShort == 170 as short
byte xByte = 0x3a assert xByte == 58 as byte
long xLong = 0xffff assert xLong == 65535l
BigInteger xBigInteger = 0xaaaa assert xBigInteger == 43690g
Double xDouble = new Double('0x1.0p0') assert xDouble == 1.0d
int xNegativeInt = -0x77 assert xNegativeInt == -119
5.2.小數
小數類型與Java一樣:
- float
- double
- java.lang.BigDecimal
你能采用如下定義方式創建這些類型的數字:
// primitive types float f = 1.234 double d = 2.345
// infinite precision BigDecimal bd = 3.456
小數能使用指數,使用e或E指數字母,緊跟著一個可選符號,且有一個整數表示指數:
assert 1e3 == 1_000.0
assert 2E4 == 20_000.0
assert 3e+1 == 30.0
assert 4E-2 == 0.04
assert 5e-1 == 0.5
為了精確的進行小數計算,Groovy選擇java.lang.BigDecimal作為小數類型。此外,float和double也被支持,但要求有一個顯式類型定義,類型轉換或後綴。即使BigDecimal是默認的小數,攜帶float或double作為類型參數的方法或閉包也可以接受這些數值。
小數不能使用二進製,八進製和十六進製表示。
5.3.有下劃線的文本
當寫一個很長的數字,使用眼睛很難弄清楚有多少數字組合在一起,例如使用千,單詞等組合。通過允許你在數字中添加一些下劃線,更容易發現這些組合:
long creditCardNumber = 1234_5678_9012_3456L
long socialSecurityNumbers = 999_99_9999L
double monetaryAmount = 12_345_132.12
long hexBytes = 0xFF_EC_DE_5E
long hexWords = 0xFFEC_DE5E
long maxLong = 0x7fff_ffff_ffff_ffffL
long alsoMaxLong = 9_223_372_036_854_775_807L
long bytes = 0b11010010_01101001_10010100_10010010
最後更新:2017-05-22 12:01:27