Java方法不会更改参数对象 [英] Java method doesn't change parameter objects

查看:96
本文介绍了Java方法不会更改参数对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的功能:

public static int partition(List list, ListElement elemL, ListElement elemR){
    ListElement elemX;
    ...
    elemR = elemX.next;
    return x; 
}

并且在功能结束时,更改了 elemR ,但是从主方法调用该函数后,参数 elemR 仍具有与该函数之前相同的值称呼. 有什么问题?在调用函数后如何更改此ListElement并保存"此更改,而无需将返回类型更改为ListElement(我也需要整数返回值)?

And at the end of the funktion the elemR is changed, but after calling the function from the main method the parameter elemR has still the same value like before the function call. What's the problem? How can i change this ListElement and "save" this change after the function is called without changing the return type to ListElement (i need the integer return value too)?

推荐答案

Java函数参数由引用名称调用,这意味着当您将对象作为函数参数放置时,JVM将引用的值复制到新变量,并且将其作为参数传递给函数. 如果更改对象的内容,则原始对象将更改,但是如果更改引用的实际值,则这些更改将在函数结束时销毁.

Java functions parameters are called by reference name, meaning that when you place an object as an function argument, the JVM copies the reference's value to a new variable, and passes it to the function as an argument. If you change the contents of the object, then the original object will change, but if you change the actual value of the reference, then these changes will be destroyed when the function ends.

我希望对您有帮助

这篇关于Java方法不会更改参数对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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