In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "example Analysis of the Equality of Scala objects". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
If you want to compare two Scala objects to see if they are equal, you can use either = =, or its opposite! =. Here are a few examples:
Scala > 1 = = 2 res24: Boolean = false scala > 1! = 2 res25: Boolean = true scala > 2 = 2 res26: Boolean = true
These actions work on all objects, not just the basic types. For example, you can use him to compare lists:
Scala > List (1,2,3) = = List (1,2,3) res27: Boolean = true scala > List (1,2,3) = = List (4,5,6) res28: Boolean = false
Further, you can compare two different types of objects:
Scala > 1 = = 1.0res29: Boolean = true scala > List (1,2,3) = = "hello" res30: Boolean = false
You can even compare null, or anything that could be null. No exception will be thrown:
Scala > List (1,2,3) = = null res31: Boolean = false scala > null = = List (1,2,3) res32: Boolean = false
As you can see, = = has been carefully processed, so in many cases you can get the equality comparison you want. It's just a very simple rule: first check if the left side is null, and if not, call the equals method. Because equals is a method, the accuracy of the comparison depends on the parameters on the left-hand side. And since there is already an automatic null check, you don't need to check it again manually. The automatic checking mechanism does not check the parameters on the right-hand side, but any reasonable equals method should return false when the parameter is null.
This type of comparison also produces true for different objects, as long as their content is the same and their equals methods are content-based. For example, the following is a comparison of two strings that all have exactly five identical letters:
Scala > ("he" + "llo") = = "hello" res33: Boolean = true
What's the difference between Scala = = and Java's
Java can compare both primitive types and reference types. For primitive types, the = = of Java compares the equality of values, such as Scala. For reference types, however, Java's = = compares reference equality: reference equality, that is, whether both variables point to the same object in the JVM heap. Scala also provides this mechanism, which is called eq. However, eq and its antonym, ne, apply only to objects that can be mapped directly to Java.
This is the end of the content of "example Analysis of the Equality of Scala objects". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.