Groovy Tip 1 = =, equals, and others

Source: Internet
Author: User

We say Java project development based on groovy language is an agile development, and the groovy language itself offers a lot of agility in addition to using grails as a Web development platform.

We know that in the Java language, we use the Equals method to determine whether the values of two strings are equal, and "= =" as symbols that determine whether two object references are the same. It turns out that we use the Equals method more than the "= =" notation, especially in the use of strings.

In the actual coding process, the shape is as follows:

if(abc1.equals(abc2))
  {
    ……
  }

Such an If language is particularly numerous, but each time we have to spell the Equals method, instead of simply using the "= =" symbol. While Ides such as Eclipse can help us spell the Equals method, the use of the Equals method delays our time, far from using the "= =" Symbol for agility.

The groovy language takes into account our actual requirements and decisively uses the "= =" notation instead of the Equals method to provide the agility of groovy language programming.

def abc1 = 'abc'
  def abc2 = "abc"
println abc1 == abc2

The result printed is true.

In this way, we do not have to abc1.equals the IF (ABC2), but can use directly:

if(abc1==abc2)
  {
    ……
  }

Is it convenient for many?

You can even use the "= =" symbol to determine whether the contents of a collection two objects are the same, such as:

def list1 = [1]
  def list2 = [1]
println list1 == list2

You can run the above statement to see that the result is true.

The contents of the two Map objects are the same or can be judged by the "= =" notation, such as:

def map1 = ['name':'tom']
  def map2 = ['name':'tom']
println map1 == map2

The result is true. But you have to run the following statement:

def map1 = ['name':'tom']
  def map3 = ['name':'alice']
println map1 == map3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.