A summary of var,val,immutable,mutable comprehension in Scala

Source: Internet
Author: User

The latest project to use the Spark platform, whether it is to see the source code or writing programs need to use the Scala language, after two days of contact, the map map here a bit of doubt, do a bit of testing after a note of their own ideas.

First, there are Var and Val two ways of defining variables in Scala, the former defining a variable, the latter defining a non-variable, similar to the const in final or C + + in Java, as shown in Figure 1 to illustrate the difference.


Figure 1

Attempting to change a VAL-defined variable will cause an error. The difference between Var and Val should be less problematic.

Then the map type, in the official API, the map sub-class can be divided into at least modifiable mutable and non-modifiable immutable two major types of map, in the subclass in addition to HASHMAP,SORTEDMAP and other specific implementations, there is the same name of the subclass type map, the default, Define a variable of type map (I would like to say the reference, do not know Scala has no such concept), its type is not modifiable, if you want to define a modifiable type needs to be shown, as shown in Figure 2.


Figure 2

The difference between mutable and immutable can be preliminarily understood by naming, the former can modify the content, and the latter cannot be modified after initialization. As shown in Figure 3, adding elements to the map and mmap in Figure 2, the map failed while trying the + = operation, and mmap successfully modified the content, adding "(" CC ", 3)" element. Viewing official documents is very quick to understand, because the "+ =" operator or function is a member (function) of mmap, but not a member of the map (function), which is also very well understood, since my map is a immutable type, why also provide you with a "+ =" Functions or members that can modify their own content. But here's a question, mmap and map are Val types, why "+ =" can modify Mmap. This is not expanded, marked as question 1, and I will continue to discuss it later.


Figure 3

In general, the "+" and "-" operations on the map type variables of immutable and mutable return an operation with the same type as the original map variable, without changing the original variable, as shown in Figure 4.


Figure 4

Finally, come to our question, that is, an extension of question 1, assuming that you define a var immutable variable, or a Val-mutable variable. The first thing that can be changed is what is mutable and what is immutable, but it is understood to think carefully.

First throw my opinion:

1) Val and var just indicate the defined variable (reference ... Can be modified to point to other content, which is what I said above why I prefer to use the concept of reference to call variables.

2) mutable and immutable indicate whether the contents of this space that are open in memory can be modified. If you have a bit of a C + + or Java base, and a little bit of understanding of the new heap space concept, we'll be able to reach a chord.

According to my own understanding (not guaranteed to be correct, but feel convenient to understand), the above map and mmap and their contents of the memory distribution can be assumed as follows:


Figure 5

Val and Var refer to the contents of the Virtual Box 1, where Val refers to a content that cannot be changed to point to other content, such as map, while Var refers to a content that can be arbitrarily changed to point to other content, as shown in the figure.


Figure 6

Immutable and mutable are only meaningful for the content in virtual box 2. For example, for mmap, use "+ =" to add a "(" FC---3 ")" dual, it is represented as shown in Figure 7


Figure 7

The area that the mmap points to does not change, only the content in that area has been modified. The content cannot be modified for areas defined by the immutable type. Recalling the above mentioned issue 1, with immutable. Map's var variable to modify how, for example

var map =map ("AA", 1, "BB"->2)

map + = ("CC"-3)

Before we run over, to immutable. The variable with the map type uses "+ =" to make an error indicating that there is no member. But the above two lines of code are correct, and notice that the map has indeed been modified, as shown in Figure 8.


Figure 8

What is this for? In fact, the principle is very simple, because the map points to the area changes, because map is a var variable, the above operation is divided into two parts: the first step with "Map + (" CC ", 3)" Return a new Immutbale region; The second step: point the map to the new immutable area. As shown in Figure 9, the original immutable zone content is not changed, but is later recycled by the JVM's GC mechanism.


Figure 9

At this point, var,val,mutable,immutable four cases of memory variable changes have been explained, the content is a bit messy, but if still do not understand, you can refer to java "= =" and "equals" differences between the memory layout and reference changes, The personal feeling principle is the same. In addition, since the use of "= =" comparison when the same content area, that is, the same hashcode will return true, in order to support Figure 7 and Figure 9, to do the last Test, we feel at random.


Figure 10


Figure 11

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.