Learn the dots of Scala (3)---everything is Object __scala

Source: Internet
Author: User

To look at Scala from an object-oriented perspective, Scala can be said to be a purely object-oriented programming language, because everything in Scala can be viewed as an object. This is different from Java, which distinguishes between basic data types and reference types. Numeric objects

5210000 + 1 *1024/1

This is the basic operation in Java, but Scala thinks everything is an object, so each number can be treated as an object and can be written in the following form:

(5210000). + ((1). * (1024))./(1))

Function object
This is also a great highlight of Scala's functional programming. Functions can be passed as arguments, can be stored with variables, and functions can be used to return a function.

Object Functiondemo extends App {
  def printresult (fun: (int, int) => Int): unit = println (Fun (4, 5))

  def add (x: int, y:int): int = x + y

  printresult (Add (_: int, _: int)
}

Two functions are defined in the code, the Printresult function, the Add function. The parameter of the Printresult function is to accept a function and return to unit. The Add function has an argument of two int types and returns an int type.

The last line of code function calls, printing out the sum result.

anonymous functions
In the above example, the Add function was invoked only once, in which case we could simply call out the first corresponding function in the form of a single, but not just to define it, in Scala we call it an anonymous function (anonymous functions, a popular explanation: a function without a name). )

The code following the use of anonymous functions is as follows:

Object Functiondemo extends App {
  def printresult (fun: (int, int) => Int): unit = println (Fun (4, 5))

  Printresul T ((x, y) => x + y)
}

(x, y) => x + y is an anonymous function where the type can be inferred automatically. If you define an anonymous parameter individually or you need to specify a parameter type.
Such as:

(X:int,y:int) =>x+y

=> to the left is the function argument list,=> the right side is the function body.

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.