java 中调用scala_在Scala中按名称调用与按值调用

java 中调用scala

Scala按名称调用vs按值调用 (Scala Call by name vs call by value)

A Scala method is a block of code that has a name and which may accept variables from the code that calls the method.

Scala方法是具有名称的代码块,可以接受来自调用该方法的代码中的变量。

Syntax:

句法:

    def method_name (parameter_list){
	    // code 
    } 

The variables that a method accepts are called parameters. The way of passing parameters to a method is called parameter passing.

方法接受的变量称为参数。 将参数传递给方法的方式称为参数传递。

Scala中参数传递的方法 (Methods of parameter passing in Scala)

In Scala, there are two ways of passing parameters while calling a method,

在Scala中,有两种方法可以在调用方法时传递参数:

  1. Call by value

    按价值致电

  2. Call by name

    用名字打电话

1)按价值致电 (1) Call by Value)

In Call by value method of parameter passing in Scala - a copy is passed to the method. This means that the parameters passed to the method have nothing to do with the actual values. These parameters are called formal parameters. The changes made in the formal parameters are not reflected in the actual parameters (the values passed while calling the method ).

Scala中传递参数的按值调用方法中 -将副本传递给该方法。 这意味着传递给方法的参数与实际值无关。 这些参数称为形式参数 。 形式参数中所做的更改不会反映在实际参数中(调用方法时传递的值)。

Sample of a method declared to accept call by value parameter passing,

声明为通过值参数传递接受调用的方法的样本,

    def add(a:Int , b:Int){
        var sum = a + b ; 
        println("sum = " + sum);
    }

The call will pass values that will be copied to variables a and b, and the operations on a and b will be independent of the actual values passed.

该调用将传递将被复制到变量a和b的值 ,并且对a和b的操作将独立于传递的实际值。

Program to demonstrate an example of CALL BY VALUE

程序演示一个CALL BY VALUE示例

object MyClass {
    //function defintion
    def add(x: Int){
        var sum = x + 1
        println("sum = "+sum)
    };
    
    def main(args: Array[String]) {
        var a = 34
        //function call
        add(a)
    }
}

Output

输出量

sum = 35

2)按名称致电 (2) Call by name)

Call by name method of parameter passing in scala also passes the memory address of the variable instead of its value. So, the program will work on the same variable. Due to address based parameter passing the variable's address is passed to the method which will perform the function.

在scala中传递的参数的按名称调用方法还会传递变量的内存地址,而不是其值。 因此,该程序将在相同的变量上工作。 由于基于地址的参数传递,变量的地址将传递给将执行功能的方法。

Sample of the method declared to accept call by name parameter passing,

声明接受名称参数传递的方法的示例,

    def add(a: => Int , b: => Int){
        var sum = a + b; 
        println("sum = " + sum);
    }

This call will copy the memory address of the passed variables and the actual variables are used for processing.

该调用将复制所传递变量的内存地址,并且实际变量将用于处理。

Program to demonstrate an example of CALL BY NAME

程序演示一个CALL BY NAME示例

object MyClass {
    def add(x: => Int){
        var sum = x + 1
        println("sum = "+sum)
    };
    
    def main(args: Array[String]) {
        var a = 34
        add(a)
    }
}

Output

输出量

sum = 35

Why not swapping function?

为什么不交换功能?

The most common example demonstrating the use of call by value and call by name in programming is swapping function.

交换函数是表示在编程中按值调用和按名称调用的最常见示例。

In Scala, the parameters of the functions are val i.e. immutable which means you can't reinitialize them in the function. leaves us to a scenario where the most easy way to learn the difference is not applicable. So, the classical program is created to just give you differences. Now, consider this code, which tries to reinitialize the parameters of Scala.

在Scala中,函数的参数是val,即不可变,这意味着您无法在函数中重新初始化它们。 让我们陷入一种最简单的学习差异方法不适用的情况。 因此,创建经典程序只是为了给您带来差异。 现在,考虑以下代码,该代码尝试重新初始化Scala的参数。

object MyClass {
    def add(a: => Int){
         a = x + 1
        println("sum = "+a)
    };
    
    def main(args: Array[String]) {
        var a = 34
        add(a)
    }
}

Output

输出量

error: reassignment to val
         a = x + 1
           ^
one error found


翻译自: https://www.includehelp.com/scala/call-by-name-vs-call-by-value.aspx

java 中调用scala

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值