Scala function and the difference between it and method

grammar

val 函数变量名 = (参数名:参数类型,参数名:参数类型...) => 函数体

Note:

  • In Scala, a function is an object (variable)
  • Similar to methods, functions also have parameter lists and return values
  • Function definition does not need to use def definition
  • No need to specify the return value type

Example: Define a function that calculates the sum of two integers and call the function

	//定义函数
    val getSum = (a:Int,b:Int) => a+b;
    //调用getSum
    var sum= getSum(1,2);
    println(sum);

The difference between method and function

In Java, there is no difference between methods and functions, and knowledge is called differently:

  • The method belongs to the class or object. At runtime, it is loaded into the method area of ​​the JVM
  • The function object can be assigned to a variable, at runtime, it is loaded into the heap memory of the JVM
  • Function is an object, inherited from FunctionN, function objects have apply, curried, toString, tupled methods, but methods do not

Guess you like

Origin blog.csdn.net/zh2475855601/article/details/113835459