Thursday 17 January 2019

Groovy: Identity Operator (==)


In Groovy, == operator calls the equals method internally. If you want to check the reference equality of the objects, you should use the 'is' method.

HelloWorld.groovy
def list1 = ["Hello", "World", "Groovy"]        
def list2 = ["Hello", "World", "Groovy"]
def list3 = list2

println "list1 == list2 : ${list1 == list2}"
println "list1 == list2 : ${list1 == list3}"
println "list1 == list2 : ${list2 == list3}"

println "list1.is(list2) : ${list1.is(list2)}"
println "list1.is(list3) : ${list1.is(list3)}"
println "list2.is(list3) : ${list2.is(list3)}"


Output

list1 == list2 : true
list1 == list2 : true
list1 == list2 : true
list1.is(list2) : false
list1.is(list3) : false
list2.is(list3) : true



Previous                                                 Next                                                 Home

No comments:

Post a Comment