Comparison of Scala and Java = =

Source: Internet
Author: User

If you want to compare and see if two objects are equal, you can use either = = or its inverse! =. (Applies to all objects, not just basic data types)

?
1234 scala> 1==  2res24: Boolean = falsescala> 1!=  2res25: Boolean = true

These actions work on all objects, not just basic types. For example, you can compare a list with him:

?
1234 scala> List(1, 2, 3) == List(1, 2, 3) res27: Boolean = truescala> List(1, 2, 3) == List(4, 5, 6) res28: Boolean = false

Further, you can also compare two objects of different types:

?
1234 scala> 1==  1.0res29: Boolean = truescala> List(1, 2, 3) == "hello"res30: Boolean = false

You can even compare null, or anything that might be null. There will be no exceptions thrown:

?
1234 scala> List(1, 2, 3) == nullres31: Boolean = falsescala> null==  List(1, 2, 3) res32: Boolean = false

(but compare: null = = NULL) when the error occurs)

?
123 规则:首先检查左侧是否为null,如果不是,调用equals方法。由于equals是一个方法,因此比较的精度取决于左手边的参数。又由于已经有一个自动的null检查,因此你不需要手动再检查一次了。

This type of comparison also produces true for different objects, as long as their content is the same and their
The Equals method is written based on content. For example, the following is a comparison of two strings with exactly five identical letters:

?
12 scala> ( "he"+ "llo") == "hello"res33: Boolean = true

What is the difference between Scala's = = and Java

In Java, you can compare a base type or a reference type . For basic Types , Java = = Comparison
Value comparisons, such as Scala. For reference Types , however, Java = = Compares whether a reference is the same object (comparing memory addresses), meaning that both variables point to the same object in the JVM heap. Scala also offers this
Kind of mechanism, the name is eq. However, the EQ and its antonyms,ne, are applied only to a pair that can be mapped directly to Java
Like.


Example:

?
1234567891011121314151617 object BitOpt {  class A{} def main(args:Array[String]){   val a = new A   val b = new A    println(a==b)    println(a eq b)    println(a ne b)    println(!a.eq(b)) }}结果:falsefalsetruetrue

Comparison of Scala and Java = =

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.