的方法更改值,而不是返回任何东西 [英] Methods that change a value, and not return anything

查看:110
本文介绍了的方法更改值,而不是返回任何东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请什么是为改变一个变量的内容方法来看,虽然不返回任何东西。

Please what's the term for methods that alter the contents of a variable, while not returning anything.

例如在Java中从 java.util.Arrays中的类的静态方法排序内部排序数组,设置排序数组到的原数组变量的(不知道)。

For instance in Java from the java.util.Arrays class the static method sort sorts an array internally and sets the sorted array to the original arrays variable (not sure).

import static java.util.Arrays.sort;
public class bs {
public static void main(String [] args){

    int[] arr = {3,2,4,8,2,5,33,12,19,15,20};
    sort(arr); // doing this stuff <<<< and not int [] arr1 = sort(arr); 
  }
 }

1 - 有一个特定的术语,是指一种方法,

1 - Is there a specific term for that kind of method,

2 - 这是如何工作的内部?是否删除原始变量具有相同名称分配排序值到一个新的一个或??

2 - How does this work internally? Does it delete the original variable and assign the sorted values to a fresh one with that same name or??

谢谢!

推荐答案

我通常把它叫做的突变的 - 或者只是说说有副作用吧。 (通常赋值函数是的你调用的方法,它改变了对象的状态的对象。在这种情况下,我们正在改变的参数/参数所指的对象,这是的不同)。

I'd normally call it a mutator - or just talk about it having side-effects. (Usually a mutator is a method you call on an object which changes that object's state. In this case we're changing the object referred to by the argument/parameter, which is slightly different.)

至于它是如何工作 - 它明白之间的差别是非常重要的一个的变量的,一个的参考的和的对象

As for how it works - it's important to understand the difference between a variable, a reference and an object.

这个方法调用:

sort(arr);

复制的改编变量的值作为排序方法中的参数值。该值是一个的参考的一个对象(在这种情况下,数组)。该方法完全不改变的改编的价值 - 它仍然是相同的数组的引用。然而,这种方法改变了数组的内容的(即哪些元素有哪些值)。

copies the value of the arr variable as the value of the parameter within the sort method. That value is a reference to an object (the array in this case). The method doesn't change the value of arr at all - it's still a reference to the same array. However, the method changes the contents of the array (i.e. which elements have which values).

这篇关于的方法更改值,而不是返回任何东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆