本文概述
- 算术运算符
- 一元运算符
- 分配算术运算符
- 关系运算符
- 逻辑运算符
- 按位运算符
- 条件运算符
以下是groovy中的运算符:
- 算术运算符
- 一元运算符
- 分配算术运算符
- 关系运算符
- 逻辑运算符
- 按位运算符
- 条件运算符
范例1:
package com.app
class GroovyOperatorsExample1 {
static void main(args) {
int a = 10
int b = 5
int c
c = a + b
println "Addition = " + c
c = a - b
println "Subtraction = " + c
c = a * b
println "Multiplication = " + c
c = a / b
println "Division = " + c
c = a % b
println "Remainder= " + c
c = a ** b
println "Power = "+c
}
}
输出:
文章图片
在Groovy中,我们还具有一些用于执行算术运算的函数,例如加号,减号,intdiv和幂。在下面给出的示例中显示了这些功能的用法。
范例2:
package com.app
class GroovyOperatorsExample2 {
static void main(args) {
int a = 10.3
int b = 5
int c
c = a.plus(b)
println "plus = " + c
c = a.minus(b)
println "minus = " + c
c = a.intdiv(b)
println "intdiv = " + c
c = a.power(b)
println "Power = "+c
}
}
输出:
文章图片
一元运算符通常,一元运算符只需要一个运算符即可执行该操作。一元运算符用于执行诸如递增/递减,取反和取反布尔值的操作。
范例3:
package com.app
class GroovyOperatorsExample3 {
static void main(args) {
int a = 10
int c
c = +a
println "Unary plus = " + c
c = -a
println "Unary minus = " + c}
}
输出:
文章图片
范例4:
package com.app
class GroovyOperatorsExample4 {
static void main(args) {
int a = 10
int c
c = a++
println "Post Increment = " + c
println "Value of a after Post Increment = " + a
c = ++a
println "Pre Increment = " + c
println "Value of a after Pre Increment = " + a
int b = 10
c = b--
println "Post decrement = " + c
println "Value of a after Post decrement = " + b
c = --b
println "Pre decrement = " + c
println "Value of a after Pre decrement = " + b
}
}
输出:
文章图片
分配算术运算符在常规中,分配算术运算符用于为变量分配新值。
范例5:
package com.app
class GroovyOperatorsExample5 {
static void main(args) {
int a = 10
a+=3
println "a+=3 ------> " + a
a-=3
println "a-=3 ------> " + a
a*=3
println "a*=3 ------> " + a
a/=3
println "a/=3 ------> " + a
a%=3
println "a%=3 ------> " + a
a**=3
println "a**=3 ------> " + a
}
}
输出:
文章图片
关系运算符在常规中,关系运算符用于比较两个对象,以检查它们是否相同或不同,或者一个大于,小于或等于其他对象。
范例6:
package com.app
class GroovyOperatorsExample6 {
static void main(args) {
int a = 10
int b = 12
boolean c
println "a = 10"
println "b = 12"
c = a == b
println "Relational Operator equals [c = a == b] ----> " + c
c = a != b
println "Relational Operator different [c = a == b] ----> " + c
c = a <
b
println "Relational Operator less than [c = a <
b] ----> " + c
c = a <
= b
println "Relational Operator less than equal to [c = a <
= b] ----> " + c
c = a > b
println "Relational Operator greater than [c = a > b] ----> " + c
c = a >= b
println "Relational Operator greater than equal to [c = a >= b] ----> " + c}
}
输出:
文章图片
逻辑运算符在groovy中,布尔表达式有3个逻辑运算符,这些运算符是AND(
范例7:
package com.app
class GroovyOperatorsExample7 {
static void main(args) {
boolean c
c = true &
&
true
println "Logical AND operator = " + c
c = true || false
println "Logical OR operator = " + c
c = !false
println "Logical NOT operator = " + c}
}
输出:
文章图片
注意:在常规情况下,与逻辑“和”相比,逻辑“不”具有更高的优先级。范例8:
package com.app
class GroovyOperatorsExample8 {
static void main(args) {
boolean c
c = (!false &
&
false)
printlnc
}
}
输出:
文章图片
注意:在常规情况下,逻辑“和”与逻辑“或”相比具有更高的优先级。范例9:
package com.app
class GroovyOperatorsExample1 {
static void main(args) {
boolean c
c = true || true &
&
false
printlnc
}
}
输出:
文章图片
按位运算符在常规中,按位运算符用于对二进制数字或整数位进行运算。
范例10:
package com.app
class GroovyOperatorsExample10 {
static void main(args) {
int a = 0b00101111
println "a = 0b00101111 ----> "+a
int b = 0b000010101
println "b = 0b000010101 ----> "+b
println "(a &
a) ----> "+(a &
a)
println "(a &
b) ----> "+(a &
b)
println "(a | a) ----> "+(a | a)
println "(a | a) ----> "+(a | b) int c = 0b11111111
println "c = 0b11111111"
println "((a ^ a) &
c) ----> "+((a ^ a) &
c)
println "((a ^ b) &
c) ----> "+((a ^ b) &
c)
println "((~a) &
c) ----> "+((~a) &
c)
}
}
输出:
文章图片
示例11:
package com.app
class GroovyOperatorsExample11 {
static void main(args) {
int a = 23
int b = 43
println "Converting Integer to Binary a = 23 ----> " + Integer.toBinaryString(a)
println "Converting Integer to Binary b = 43 ----> " +Integer.toBinaryString(b)
println "Converting binary to integer 10111 ----> a = " + Integer.parseInt("10111", 2)
println "Converting binary to integer 101011 ----> b = " + Integer.parseInt("10111", 2)
}
}
输出:
文章图片
条件运算符在Groovy中,条件操作符分为以下三种:
- 不是操作员
示例12:
package com.app
class GroovyOperatorsExample12 {
static void main(args) {
println "(!true) ----> "+(!true)
println "(!'srcmini') ----> "+(!'srcmini')
println "!Null ----> "+(!'')
}
}
输出:
文章图片
- 三元运算符
示例13:
package com.app
class GroovyOperatorsExample13 {
static void main(args) {
String Answer
String s = 'srcmini'
Answer = (s!=null &
&
s.length()>0) ? 'Found' : 'Not found' }
}
输出:
文章图片
- 猫王算子
示例14:
package com.app
class GroovyOperatorsExample1 {
static void main(args) {
String Answer
String s = 'srcmini'
println Answer = s ? 'Found' : 'Not Found'
println Answer = s ?: 'Found'
}
}
【groovy操作符】输出:
文章图片
推荐阅读
- groovy条件语句
- groovy基本语法
- 第一个groovy项目
- 在eclipse中创建一个groovy项目
- 如何在windows上安装groovy
- 如何在eclipse上安装groovy
- groovy的介绍
- groovy入门教程