Scala study notes 9-= and = =

Source: Internet
Author: User

= Assignment operation

Scala's assignment operations are very different from Java's. For assignment operations such as A=B, the return value in Java is the value of a, and the unit is returned in Scala. The unit is a value type, where there is only a unique value, i.e. (), the unit is used only to declare the return value of a function or method, and other scenarios are basically meaningless. This can easily lead to some mis-use, such as A=b=c, which is absolutely possible in Java, but running in Scala will result in an error. Look at the code:

var A, b, c = 1=  b=cprintln (a)

The above code has not been executed in the IDE error, reluctantly executed to see what will be reported wrong:

The type mismatch error is reported because Scala infers that a is an int variable in the first line of code, and then assigns the result of the B=c-a unit value-to it, which of course will report an error.

Even seeing the assignment operator "=" in Scala is also a strange thing: it is not a method, and by default it is not possible to use--a.= (1). For this point temporarily can not delve into, later have time to look again.

= = Arithmetic

In Java, the = = operation for basic types and reference types has different methods of calculation:

    • For basic types, the = = Operation compares the values of the expressions on both sides;
    • For reference types, the = = Operation compares references to objects on both sides of the object, not the value of the object, and the Equals method is used to compare the value of the object in Java.

There are no objects in Scala, so the = = operation is value-oriented regardless of the type. This is guaranteed by the Scala any class (which is derived from all classes in Scala): it implements the = = () method as the final method. So the comparison of values in Scala makes it easy to use the = = operation, and if there are some specialized comparisons, you need to rewrite the Equals method. You can use the EQ () method if you want to perform comparisons for references. Take a look at the following procedure:

Val str1 = "Hello"= "Hello"new String ("Hello"//  equivalent to Java ' s Str1.equals (STR2)//  equivalent to Java ' s str1 = = str2println (str1 = str3) println (str1 eq STR3)

Execution results of the program:

The results are clear.

---------------------------------------------------

Today I learned Scala's = and = =, these two operations and Java in the corresponding symbol of the operation has a significant difference, you need to pay attention to later.

#########

Scala study notes 9-= and = =

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.