Scala learning notes and differences with Java-from the Java developer perspective

Source: Internet
Author: User
Tags function definition static class

Scala has a lot in common with Java, but it's a lot different. This is primarily a Java developer's perspective, summarizing some of the thinking changes that are faced in the process of using Scala.

Here is just a summary of the two languages in the development process of the different, will be updated after some switching in the development process is worth noting. The following is a list of some of the differences between the impressive Scala languages, the specific code demo sample, and the specifics described below.


?

    • In Scala, Java code can be called directly to connect seamlessly with Java.
    • Statements can be used without ";" End. And the recommendation does not apply ";".
    • Variable declaration begins with Var or val. You do not have to specify a variable type, the compiler can determine the variable type;
    • function can not define the return value type (except for special cases);
    • function return value can be returned without return;
    • Without the function definition and call, the function name can not be followed by parentheses;
    • Array access the subscript uses parentheses instead of brackets.
    • Tuples use the ". _n" way to access elements. n start from 1;
    • ......。
Specify the main function

To run a program in Java, just specify the main function of the portal. You can have multiple main functions in a project, and you can define the main function within each class. But unlike Java, Scalaproject. Only the main function, defined in object, is a program entry, and the main function defined in class cannot be used as a program entry.



You want the program to run. You need to define the main function in object, for example, as you can see. In Scala, class is the same as classes in Java, but there is no static type class in Scala, at the same time. A way to define object is provided in Scala. The "class" defined in this way is similar to the static class in Java, where it is used without the need for a new object, directly using variables and functions that can be visited outside. The method defined in object is also similar to the static method, and is directly interviewed by the object name.

Variable and field declarations

Different from the defined variables in Java. In Scala, variables are declared and defined in a way that is "Var|val variable name [: type] = value". When declaring a field. The ability to use the compiler to determine the type itself, that is, do not write ": type", for example

var2;或直接var2;

The difference between Var and Val is that the value of the variable declared by Var can also be changed, whereas the variable in Val declaration can only be assigned at the time of declaration. The value of the variable (similar to a read-only variable) cannot be changed afterwards. But not constant). Scala's functional programming is more recommended for Val.

Definition and difference of class

For example, as you can see, classes are defined in Scala by the class keyword, which looks similar to Java. The default identifier in the Scala class is public, and if you don't understand the private or other access, the members of the class are publicly available by default.

But Scala is a lot different from Java in terms of constructors, including the main constructor and the subordinate constructor in the Scala class. The main constructor of a class in Scala: The main constructor follows the class name directly. The arguments in the main constructor are compiled into the fields of the class, and the main constructor runs all statements in the class that are not included in the method body, assuming that Val or Var is not used to declare the variable in the argument of the main constructor function. The variable at this time is private[this] level and can only be interviewed within the class. The main constructor for the person class, for example, is seen. The arguments in the main constructor name and age are used as two member variables of the person class, and the print statements that are not included in any method are run as statements in the main constructor.

Since age defined in is not decorated with Val or Var, it is used as a member variable of the private type, and such variables can only be interviewed within the class.

So for example, as seen, name can be interviewed outside the class, and age cannot be interviewed externally.

The above is the case of the main constructor. Scala also includes a class of subordinate constructors, that is, other than the main constructor, the subordinate constructor has the following characteristics: The secondary constructor is declared with this, and the secondary constructor must call the main construct or other subordinate constructors. For example, as seen in the example of a secondary constructor.

A special function definition

The function definition in Scala differs from Java in that, as a whole, the return value type is specified in the form of ": type" after the list of participants, and then through "=" to undertake the function body. For non-return value types, the type can write "Unit" (similar to void in Java), which is simple for the function body. "=" can be followed directly after the statement. Do not use "{}".



Unlike Java, a statement block or function return value can be returned without displaying a "return" statement, and the value of the last expression of the general block is the return value of the function. So the above function can be changed to the following form.

The same variable declaration does not need to display the same type (the variable can judge the variable type), so the function can also determine the return type. So it is possible to write a simple example of what you see below:

However, it is important to note that if the function returns with return, you must understand the return type of the specified function. It is also assumed to be a recursive function, and it is necessary to specify the return type. What you see is a function returned by "return", when the function definition does not specify a return type, and the compiler gives an error.



For the function list is empty, the function name can not be followed by "()" when it is defined, and can be used without "()". For example, as seen in:

Use of arrays and tuples

Two array definitions, such as what you see, differ from Java in that you need to use the array keyword.

As in Java, array lengths are immutable and array elements are variable.

The way an array in Scala can access elements by subscript is, for example, seen, primarily in 2 ways, the first of which is through arr (i). Unlike in Java, the main way to visit in Java is through "arr[i."

This is because in Scala, whatever operator is a function call. It also provides access to "arr.apply (i)".



Array assignment and changes can be done in the following two ways, the first suitable for code display assignment and changes, and another suitable for the program run phase based on the criteria for assigning specific elements and changes.

There are 2 ways to iterate over an array.

The first of these is that the to of the For loop is also taken. Another way to traverse is to pass an anonymous function through a foreach method. Assuming that the array element needs to specify a type, it needs to be enclosed in "(item:string)" parentheses, assuming that the processing code is more complex and can be represented by a "{}" statement block.



Definitions and elements of tuples in Scala for example, as seen below, the definition uses "()" to enclose several variables or literals of different types.

It's worth noting that. The tuple element is interviewed by ". _n", and N is counted from 1.

References

Scala Learning Notes Series
Functions of the Scala Foundation
Scala Loops and Arrays
The Scala Guide series for Java developers

Scala learning notes and differences with Java-from the Java developer perspective

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.