当前位置:网站首页>Introduction to groovy syntax
Introduction to groovy syntax
2022-06-22 14:37:00 【Leisurely summer】
1、 Introduce
Groovy since 2003 Appeared in Java field . With more than ten years of history 、 Development and experience , It is a kind of Java Syntax compatible object-oriented programming language , It can be compiled as JVM Bytecode . In a way ,Groovy Can be regarded as Java A scripted improved version of . This is because it runs in although it runs in JVM On , Because of the way it works , It can work well with Java Code and its related libraries to interoperate . Most effective Java Code can also be converted to valid Groovy Code .
Groovy Designed to be both a programming language and a scripting language . This means that you need to compile Java Different ,Groovy It can be combined with the use of all kinds of grammar sugar , It reduces a lot of work in the coding process .
Groovy and Java The biggest difference in code is Groovy More flexible , Grammar requires less , So it attracts a lot of Java Users . This means that many people enter Groovy Of Java Developers will find the process of learning it very simple . Because basically , Most object-oriented programming languages tend to follow the same syntax . This shared ideology makes it easy for developers to stay Java and Groovy Switch between , Or it can be mixed Java and Groovy.
2、groovy notes
groovy There are two kinds of comments ( And Java identical )
// Single-line comments
/**/ Multiline comment
3、groovy Statement output
Mode one (java edition )
class HelloGroovy {
static void main(args) {
System.out.println("Hello world")
}Mode two (groovy)
println "hello world"Be careful :groovy There is no need to end with a semicolon in grammar
4、 Defining variables
Mode one
image Java Use data types to declare directly
int x = 10
String s = " Zhang San "
Mode two
Use def Keyword declaration variables ,groovy It will make your incoming data , Automatically infer variable types
def x = 3
def name = " Zhang San "5、 Single quotation marks 、 The difference between double quotation marks and three quotation marks
5.1、 Single quotation marks
Commonly used to define strings , And java Like double quotation marks in “\” For escape characters
def name = ' measuring \' Try code '5.2、 Double quotes
The function is the same as that of single quotation marks , In addition, it can also use ${ Variable name } Set string parameters dynamically
def testName = "call me"
def text = "Hello : ${testName}"
println testName.class // class java.lang.String
println text.class // class org.codehaus.groovy.runtime.GStringImplBe careful : The data type of the first line of code is Java Medium String data type , But the data type in the second code is groovy peculiar GString data type ,GString And java Medium String Can be passed on to each other , It's transformational .
${} Expression can extend any expression
5.3、 Three quotes
The basic function is the same as that of single quotation marks , besides , If we want to define a multi line string , When you want to keep its format , We can use three quotation marks to complete
def mail = '''\
abc
efg
'''
println(mail)notes :groovy Use the printing method , You can wrap objects with parentheses , You can also use no parentheses , Use spaces to separate objects , The printing method can also be realized , The two functions are exactly the same .
6、 Common methods for handling strings
6.1、center( Center fill )
This is the string padding method , Parameters 1 Is how many strings to fill , Parameters 2 When filling , Objects used for filling . It has Two construction methods , A parameter construction method , Specifies how many strings to add , Fill with spaces ( When the specified number is less than the length of the string, it will not be filled )
def str = "groovy hello"
println str.center(15, "p")6.2、padLeft( padding-left )
def str = "groovy"
println str.padLeft(8,"0")6.3、padRight( Right fill )
def str = "groovy"
println str.padRight(8,"0")6.4、 String comparison
def str2 = "hello"
println str > str2 // The comparison is Unicode code , You can also use compareTo Method to compare strings 6.5、 Get the value of the string
def str = "groovy hello"
str[0] // Get the value of the string according to the index ,
str.getAt(4) // java The way
str[1..4] // Incoming range , To get the string 6.6、 String subtraction (minus Method )
Subtraction of strings , Minus is str Contained in the str The elements of , This method can also use the operator " - " Instead of
str = "ABC999"
STR2 = "aBC999"
str.minus(str2) // The result is aBC6.7、 Output strings in reverse order (reverse Method )
str.reverse() // Output strings in reverse order 6.7、 Capitalize the initial of a string (capitalize Method )
str.capitalize() // Capitalize the initial of a string 6.8、 Determine whether the string is a numeric type (isNumber Method )
str.isNumber() // Determine whether the string is a numeric type 6.8、 Convert a string to another type ( Be similar to java Mutual conversion of various data types in )
str3 = "8"
println str3.toInteger() // Convert a string to another type notes :Groovy There are also many string manipulation methods , Here are just a few commonly used , For details, see GroovyAPI file
7、 Conditional statements and circular statements
Groovy in if Conditional statements 、for and while Circular statements and Java identical , Here we mainly introduce switch..case and for Enhanced use of loop statements , as well as Groovy Its transformation .
7.1、switch..case
groovy Medium Switch..case It can be any data type ( Than Java Medium switch..case Much stronger )
def x = 1.2345
def result
switch (x) { // groovy Medium Switch..case It can be any data type
case 'foo':
result = 'find foo'
break
case 'bar':
result = 'find bar'
break
case [4, 5, 6, 'inlist']: // list
result = 'list'
break
case 1..44:
result = 'range' // Range
break
case Integer:
result = 'integer'
break
case BigDecimal:
result = 'decimal'
break
default: result = 'default'
}7.2、for A loop statement loops through a range
/*
* Cycle the range
*/
def sum = 0
for (i in 0..9) {
sum += i
}7.3、for Loop statement pair List Cycle through
/* Yes list Cycle through */
sum = 0
for (i in [1, 2, 3, 4, 2, 3, 1, 0]) {
sum += i
}7.4、for Loop statement pair Map Set to cycle
/*
Yes map Set to cycle
*/
for (i in [" Zhang San ": 23, "lucy": 19, "Bob": 30]) {
sum += i.value
}边栏推荐
- Flink status management
- unity的富文本Text的Color设置颜色全透明
- Summary of patent blog writing
- Should programmers choose software talent outsourcing companies?
- 11 method reference and constructor application
- C# 分页计算 总页数、当前页数据集合
- SQL Server common functions
- C#泛型方法
- Unity prevents the BTN button from being clicked continuously
- 融云:让银行轻松上“云”
猜你喜欢

Specific methods and steps of PROFINET communication between s7-200smart and Fanuc robot

client-go gin的简单整合九-Create

一文彻底弄懂工厂模式(Factory)

一文彻底弄懂建造者模式(Builder)

VR全景拍摄,打破传统宣传雁过不留痕的僵局

History of hash index design

Oceanbase database helps the ideal automobile intelligent production line to realize automatic recovery within 30 seconds

安装和使用protobuf-c

加密市场进入寒冬,是“天灾”还是“人祸”?

C# Winform 相册功能,图片缩放,拖拽,预览图分页
随机推荐
uniapp app 端截屏且保存到本地
Is polardbx PG or MySQL?
Specific methods and steps of PROFINET communication between s7-200smart and Fanuc robot
Lisez ceci pour vous apprendre à jouer avec la cible de test de pénétration vulnhub - driftingblues - 5
Groovy之列表操作
unity和C#中怎么去比较2个日期大小
位置编码(PE)是如何在Transformers中发挥作用的
Quickly understand the commonly used symmetric encryption algorithm, and no longer have to worry about the interviewer's thorough inquiry
VR panoramic shooting breaks the deadlock of traditional propaganda that wild geese pass without leaving traces
利用图片实现APP元素定位sikulix
机器学习之感知机
Random forest of machine learning
Are you familiar with redis cache of highly paid programmers & interview questions series 114? How are redis keys designed? Are you familiar with the memory obsolescence mechanism?
[untitled]
What does Huawei's minutes on patents say? (including Huawei's top ten inventions)
Tianrun cloud is about to be listed: VC tycoon Tian Suning significantly reduces his holdings and is expected to cash out HK $260million
Rongyun: let the bank go to the "cloud" easily
CSAPP之详解Labs
天润云上市在即:VC大佬田溯宁大幅减持,预计将套现2.6亿港元
Z-Score和deltf/f有什么区别?