Scala program Design-java virtual machine multi-core programming practice (i)

Source: Internet
Author: User

  1. Once an object is created, it will no longer change its contents, so that the object is immutable. This can be done without worrying about competition management when accessing objects in multiple threads, and Java's string is an example of an immutable object. Based on this, when you use Scala to create multithreaded applications, you can write unlocked code with the invariant state (immutable states) to write concise multithreaded code without worrying about data contention between threads and dealing with the nightmare of lock and release.
  2. Other languages on the JVM What about groovy, JRuby, Clojure? So far, there's a strong-typing language that provides both functional style and good concurrency support, only Scala.
  3. Scala is the acronym for Scalable language, a mixed-type functional programming language that was not available for the first version in 03.
  4. In Scala, all objects, such as 2.toString (), generate compilation errors in Java, whereas in Scala, the ToString () method of an int instance is called. Scala favors simplicity, which provides a respite from years of abuse of your little finger-the semicolon is optional in Scala. In Scala, the dot operator is also optional, depending on the context, as well as the parentheses. For example do not write S1.equals (S2), can write S1 equals S2. The semicolon, parentheses, and dot numbers are removed.
  5. Scala's real charm is that there are very few built-in rules, and the rest, including operators, are part of the Scala library.
  6. Val VS. Var:val and Var are used to define variables, the variables defined in Val are immutable, the values are fixed after initialization, variables defined with VAR are variable, and the number of modifications is OK. The invariance here refers to the variable itself, not the instance referenced by the variable, for example, Val buffer = new StringBuffer (), cannot point to the other reference, but can still use methods such as append () to modify the buffer In Scala, the preference for Val, not Var, is to promote immutability and functional style.
  7.  for (I<-1 to) {       print (i+", ")     for (i<-1 until 10) {       print (i+ ",")    }
    where to includes the nether, while until does not include the Nether
  8. In Scala, it's really easy to create a multiline string, as long as you put a multiline string between 3 double quotes ("" "..." "), Scala allows you to embed double quotes in a string, and Scala keeps the contents of the three double quotes intact in Scala, called the original string.
  9. Scala's handling of = = is different from Java; it is consistent with all types of processing, in Scala, regardless of type, = = is a value-based comparison, and if you want to perform an identity-based comparison, you can use the EQ () method,
    Object HelloWorld {def main (args:array[string]) {val S1 = new StringBuilder ("ABCDEFG")val s2 = new StringBuilder ("ABCDEFG")println (S1 = = s2)//true->equivalent to Java ' s s1.equals (S2)println (S1 eq S2)//false->equivalent to Java ' s s1==s2  }}
    Note that Scala's = = processing is consistent for all types and avoids the confusion of using = = in Java, however, you must remember that this is semantically different from Java in case of an unexpected occurrence.
  10. In Scala, inheriting a base class is similar to Java, with a two-point limit, and (1) Overriding the method requires the Override keyword, (2) only the primary constructor can pass parameters to the base class constructor.
  11. Markerfactory is a single case, once the definition is complete, its name represents the only instance of the singleton object, and the singleton can be passed to the function as if it were a typical passing instance.
    class Marker (Val color:string) {println ("creating" + This)override Def toString (): String = "Marker color" + Color}Object Marketfactory {private Val markers = Map ("Red", New Marker ("Red"),"Blue", New Marker ("Blue"),"Green", New Marker ("green")  )def getmarker (color:string) = if (Markers.contains (color)) markers (color) ELSE nulldef main (args:array[string]) {println (marketfactory getmarker ("Blue"))println (marketfactory getmarker ("Blue"))println (marketfactory getmarker ("Red"))println (marketfactory getmarker ("Red"))println (marketfactory getmarker ("yellow"))  }}

Scala program Design-java virtual machine multi-core programming practice (i)

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.