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

Source: Internet
Author: User

There are many similarities between Scala and Java, but there are many differences. 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 partial, but impressive Scala language difference, specific code examples and detailed elaboration see below.
?

    • In Scala, Java code can be called directly and seamlessly connected to Java;
    • Statements can be used without ";" End, and the recommendation does not apply ";" ;
    • Variable declarations start with Var or val, you do not have to specify a variable type, and the compiler can infer the variable type;
    • function can not define the return value type (except for special cases);
    • function return value can be returned without return;
    • The function name can not be followed by the parentheses when the function is defined and called without parameters.
    • Array access subscript uses parentheses instead of brackets;
    • Tuples use ". _n" to access elements, n starting from 1;
    • ......。
Specify the main function

Run the program in Java, as long as you specify the main function of the portal. There can be multiple main functions in a project, and the main function can be defined within each class. But unlike Java, in Scala, only the main function defined in object can be used as a program entry, and the main function defined in class cannot be used as a program entry.

To be able to execute the program, you need to define the main function in object as shown. In Scala, class is the same as classes in Java, but there are no static type classes in Scala, and in the meantime Scala provides a way to define object, a class defined in this way like the static class in Java. You do not need new objects to use, directly use variables and functions that can be accessed outside. The method defined in object is also similar to the static method, accessed directly through the object name.

Variable and field declarations

Unlike defined variables in Java, variables are declared and defined in Scala in the form of "Var|val variable name [: type] = value". When declaring a field, you can use the compiler to automatically infer the type without writing ": type", such as

var2;或直接var2;

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

Definition and difference of class

As shown, classes are also defined in Scala by the class keyword, which looks similar to Java. The default accessors in the Scala class are public, and if you do not explicitly specify private or other accessors, the members of the class are publicly accessible 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 is followed directly by the class name, and the parameters in the main constructor are compiled into the fields of the class, and the main constructor executes all the statements in the class that are not contained in the method body, and if Val or Var is not used to declare the variable in the parameters of the main constructor function, The variable at this time is private[this] level and can only be accessed within the class. As shown in the main constructor of the person class, the parameters name and age in the main constructor are treated as two member variables of the person class, and the print statements that are not contained inside any method are executed as statements in the main constructor.

Because the age defined in is not decorated with Val or Var, it is used as a member variable of the private type and can only be accessed within the class. So, as shown, name can be accessed outside the class, and age cannot be accessed externally.

The above is the case of the main constructor, and 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, the secondary constructor must call the main construction or other subordinate constructors. As shown 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 after the argument list by ": Type", and then through "=" to undertake the function body. For a type with no return value, the type can write "Unit" (like void in Java), and for the function body simple, "=" can be followed directly after the statement, without "{}".

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

The same variable declaration does not need to display the same type as specified (the variable can infer the variable type), so the function can infer the return type, so it can be written as follows:

However, it is important to note that if the function returns using return, you must explicitly specify the return type of the function, and if it is a recursive function, you need to specify the return type. Shown is the function returned by "return", the function definition does not specify the return type, the compiler error.

For the function argument list to be empty, the function name can be defined without following the "()", or "()" when used, as shown in:

Use of arrays and tuples

The two arrays are defined as shown, and unlike Java, the array keyword is required. As in Java, array lengths are immutable and array elements are variable.

In Scala, arrays are accessed in an array by subscript, as shown in the main 2 ways, the first is through "arr (i)" This way, unlike in Java, Java mainly through "arr[i]" this way access. This is because in Scala, any operator is a function call. It also provides a "arr.apply (i)" access method.

Array assignment and modification can be done in the following two ways, the first one is suitable for code display assignment and modification, the second is suitable for the program runtime to assign and modify specific elements according to conditions.

There are 2 ways to iterate over an array. The first is to note that the to of the For loop is also taken, and the second traversal is to pass the anonymous function through a foreach, if the array element needs to specify a type, it needs to be enclosed in "(item:string)" brackets, which can be represented by a "{}" statement block if the processing code is more complex.

The definition and element access of a tuple in Scala is defined by using "()" to enclose several variables or literals of different types directly. It is important to note that the tuple element is accessed by means of ". _n", and N is counted starting at 1.

Reference

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 summary-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.