Scala is an interesting language. On the one hand, it absorbs the inherited characteristics of many languages, on the one hand, it does not abandon the powerful platform of Java. It runs on the Java virtual machine (JavaVirtualMachine) and easily realizes interoperability with rich Java class libraries. It supports both object-oriented programming and functional programming. The program it writes is as succinct as a dynamic language, but in fact it is a static language in the strict sense. Scala is like a master of martial arts. It combines the best of computer history in the past few decades, and it provides a new choice for programmers.
Introduction to JavaJava is an object-oriented programming language. It not only absorbs the various advantages of the C++ language, but also eliminates the concepts of multiple inheritance and pointers that are difficult to understand in C++. Therefore, the Java language is powerful and easy to use. As a representative of static object-oriented programming languages, the Java language implements object-oriented theory very well, allowing programmers to perform complex programming in an elegant way.
Java is characterized by simplicity, object-oriented, distributed, robustness, security, platform independence and portability, multi-threading, and dynamics. Java can write desktop applications, web applications, distributed systems, and embedded system applications.
How Java worksIt consists of four aspects:
(1) Java programming language
(2) Java class file format
(3) Java virtual machine
(4) Java application interface
When editing and running a Java program, you need to cover these four aspects at the same time. Use text editing software (such as Notepad, WordPad, UltraEdit, etc.) or integrated development environment (Eclipse, MyEclipse, etc.) to define different classes in Java source files [22], by calling classes (the classes implement JavaAPI) The method accesses the resource system, compiles the source file to generate a binary intermediate code, stores it in the class file, and then runs the class file by running a Java virtual machine corresponding to the operating system platform environment, and executes the bytecode generated by the compilation. , the method implemented in the class file is called to satisfy the JavaAPI call of the program.
The difference between scala and java1, support for strings
Scala uses three double quotes "" to support line break strings, Java needs to use "+" for string concatenation.
2, method return value
The return of scala is optional, and the method call will automatically return the expression of the last evaluation. If scala uses return, it needs to display the return value of the specified method.
Java needs to display the return of the value with return.
3, the default value of the class and method modifiers
Scala defaults to public, java defaults to protected.
4, the default imported class
Scala imports the java.lang package, scala package, and scala.Predef class by default.
Java imports java.lang package by default
5, the interface
Scala does not support interface interfaces, using traits (similar to abstract classes in Java).
Java support interface
6, class members and singleton objects
The scala language mechanism supports singleton objects and associated objects, and associated classes. Associated and companion objects need to be used in a class file. When using companion objects, the system implicitly calls apply to generate an object of the companion instance.
Java is a member of a class, and a singleton object needs to be implemented by itself.
7, method parameters
Scala supports functions as arguments, in the format deferror(msg:=â€String), which means that msg is an input parameter and the output parameter is a String function.
Java does not support.
The advantage of scala over javaCompared with Java, I think Scala has the following two main advantages:
FP generic support
If you use a lot of template interfaces in Spring, you will feel that FP is actually quite easy to use.
And this is just the tip of the iceberg for FP benefits.
The function is actually an input-"output (scala also represents a function), without any side effects, regardless of state, due to this feature, the functional programming paradigm has many benefits in the distributed field.
For functional programming, my knowledge is really fur, but it can be said that FP has advantages over OO, and Scala has almost the same advantages for Java.
Because FP has so many advantages, Java8 introduced FP. To some extent, Java recognizes Scala's approach.
Type system support
If Java is a type-safe language, then there is no doubt that Scala's type is more secure. To some extent, Scala's type is Turing-complete, and Java is not. A good friend of mine has studied this aspect deeply (http://hongjiang.info/scala/), and my understanding of the type system with Scala is still a skin.
It is these two advantages that make Scala more secure than Java, yet flexible and imaginative.
Other language advantages
In Java, do you sometimes want to inherit multiple AbstractClasses? Sorry, Java only supports single inheritance.
In Scala, you can mixin (Java8 also started to introduce defaultmethod)
In Java, want a singleton? Either do it in staticblock, or use Enum's singleton feature, or other more tangled methods.
In Scala, a singleton is declared as long as it is declared as an object.
In Java, would you like to delay loading a singleton? Doublecheck
In Scala, just modify the variable to lazy in the object.
In Java, what do you want to do with the collection? Use layer by layer for loop
In Scala, you can get some SQL-like enjoyment by using some collection operations of the collection.
In Java, I want to callback the Future in concurrency? Sorry, Future is not Listenable (cannot support callbacks) unless you use additional tools (such as guava, spring)
In Scala, asynchronous programming is advocated, and the cooperation between future and promise is very pleasant.
In Java, what if you want to transparently extend a class of a third-party library? Pack, add another layer.
In Scala, there is a powerful implicit mechanism that allows you to do this more elegantly while still ensuring type safety (much safer than Ruby's monkeypatch)
Scala's expressiveness is strong, and the same function code, the number of lines in Java and Scala is not the same.
These are just linguistic advantages, but Scala can seamlessly integrate with Java.
Despite all the benefits, Scala has the following disadvantages:
The grammar is complex and the learning curve is very high
Domestic Scala programmers are hard to find (the current Scala's influence is slowly expanding, such as the popularity of the star Spark in the Scala community is slowly pulling the popularity of Scala, like rails to ruby)
The community, the ecology is still relatively small, and the Scala-style library is still very small (but it is easy to slant the street with Java to make up for this)
Storage Electric Water Heater,Electric Water Heates For Showers,Enamel Inner Tank Electric Water Heater,Wall Mounted Storage Water Heater
Shandong Sangle Group Co.,Ltd. , https://www.sangle-group.com