Scala Learn 4-java basic types of Scala classes

Source: Internet
Author: User

In Java, variable types fall into two main categories: primitive types and reference types. Although the auto-boxing and auto-unpacking mechanisms have been introduced after JDK1.5, we have greatly reduced our entanglement between the direct and reference types, but there are still some issues that we have to consider. For example, I encountered a problem with the base type and the default value of its wrapper type, such as generics can only use reference types, such as by default the value of the direct type cannot be manipulated as an object (1.toString () cannot be compiled) and so on.

Everything is an object in Scala. The 1.toString () notation in Scala is perfectly acceptable. Each of the basic types in Java has a corresponding encapsulation class in Scala, and each of Scala's package classes corresponds to a rich wrapper class. The types of direct quantities that are common in Scala are the corresponding encapsulation classes in Scala. But Scala's rich encapsulation classes, wrapper classes, and direct types in Java can be directly and implicitly convertible.

Take a look at the following code:

class scalaint {  def playwithint () {    = ten    new  java.util.arraylist[string]      List.ensurecapacity (capacity)  }}

In the code, you first use Scala's wrapper class int to define a variable capacity, and then use that variable as a parameter to its method on a Java.util.ArrayList object. In this step, Scala's int variable is implicitly converted to a variable of a direct type in Java.

In addition, the use of 1.to (3) or 1 to 3 in Scala has been mentioned in the previous section. The To method is not a method in the int type, but a method in its corresponding rich wrapper class Richint. When Scala infers that the value of type int 1 does not satisfy the requirement, it calls its Intwrapper method to convert it to the Scala.runtime.RichInt type, and then calls its to method.

The direct volume encapsulation class in Scala provides arithmetic operations, and the rich package class encapsulates some of the more commonly used methods to facilitate development.

Scala Learn 4-java basic types of Scala classes

Related Article

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.