Java中可以通过引用传递吗?
抱歉,如果这听起来像是一个新手问题,但前几天,一位 Java 开发人员提到了通过引用传递参数(通过它,只需传递一个引用对象)
从 C# 的角度来看,我可以通过值或引用传递引用类型,这对于值类型也是如此。
我编写了一个 noddie 控制台应用程序来展示我的意思..我可以用 Java 来做到这一点吗?
namespace ByRefByVal
{
class Program
{
static void Main(string[] args)
{
//Creating of the object
Person p1 = new Person();
p1.Name = "Dave";
PrintIfObjectIsNull(p1); //should not be null
//A copy of the Reference is made and sent to the method
PrintUserNameByValue(p1);
PrintIfObjectIsNull(p1);
//the actual reference is passed to the method
PrintUserNameByRef(ref p1); //<-- I know im passing the Reference
PrintIfObjectIsNull(p1);
Console.ReadLine();
}
private static void PrintIfObjectIsNull(Object o)
{
if (o == null)
{
Console.WriteLine("object is null");
}
else
{
Console.WriteLine("object still references something");
}
}
/// <summary>
/// this takes in a Reference type of Person, by value
/// </summary>
/// <param name="person"></param>
private static void PrintUserNameByValue(Person person)
{
Console.WriteLine(person.Name);
person = null; //<- this cannot affect the orginal reference, as it was passed in by value.
}
/// <summary>
/// this takes in a Reference type of Person, by reference
/// </summary>
/// <param name="person"></param>
private static void PrintUserNameByRef(ref Person person)
{
Console.WriteLine(person.Name);
person = null; //this has access to the orginonal reference, allowing us to alter it, either make it point to a different object or to nothing.
}
}
class Person
{
public string Name { get; set; }
}
}
如果java不能做到这一点,那么它只是按值传递引用类型? (公平地说)
非常感谢
骨头
Sorry if this sounds like a newbie question, but the other day a Java developer mentioned about passing a paramter by reference (by which it was ment just pass a Reference object)
From a C# perspective I can pass a reference type by value or by reference, this is also true to value types
I have written a noddie console application to show what i mean.. can i do this in Java?
namespace ByRefByVal
{
class Program
{
static void Main(string[] args)
{
//Creating of the object
Person p1 = new Person();
p1.Name = "Dave";
PrintIfObjectIsNull(p1); //should not be null
//A copy of the Reference is made and sent to the method
PrintUserNameByValue(p1);
PrintIfObjectIsNull(p1);
//the actual reference is passed to the method
PrintUserNameByRef(ref p1); //<-- I know im passing the Reference
PrintIfObjectIsNull(p1);
Console.ReadLine();
}
private static void PrintIfObjectIsNull(Object o)
{
if (o == null)
{
Console.WriteLine("object is null");
}
else
{
Console.WriteLine("object still references something");
}
}
/// <summary>
/// this takes in a Reference type of Person, by value
/// </summary>
/// <param name="person"></param>
private static void PrintUserNameByValue(Person person)
{
Console.WriteLine(person.Name);
person = null; //<- this cannot affect the orginal reference, as it was passed in by value.
}
/// <summary>
/// this takes in a Reference type of Person, by reference
/// </summary>
/// <param name="person"></param>
private static void PrintUserNameByRef(ref Person person)
{
Console.WriteLine(person.Name);
person = null; //this has access to the orginonal reference, allowing us to alter it, either make it point to a different object or to nothing.
}
}
class Person
{
public string Name { get; set; }
}
}
If it java cannot do this, then its just passing a reference type by value? (is that fair to say)
Many thanks
Bones
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不,Java 不能做到这一点。 Java 只按值传递。它也按值传递引用。
No, Java cannot do this. Java only passes by value. It passes references by value too.
通过引用传递是一个经常被 Java 开发人员误解的概念,可能是因为他们无法做到这一点。阅读此内容:
http://javadude.com/articles/passbyvalue.htm
Pass by reference is a concept often misunderstood by Java devs, probably because they cannot do it. Read this:
http://javadude.com/articles/passbyvalue.htm
在 C# 中说“按值传递引用类型”相对容易产生误解。
您不能按值传递引用类型 - 您可以传递对对象的引用,或对引用的引用。在java中,你不能将引用传递给引用。
然而,您的 noddie 应用程序使区别更加清晰。
It's relatively misleading to say "pass a reference type by value" in C#.
You cannot pass a reference type by value - you can pass a reference to the object, or a reference to the reference. In java, you cannot pass a reference to the reference.
Your noddie application makes the distinction a little clearer, however.
从技术上讲,正如我们所知,引用对象的变量严格来说是指向该对象的指针。当将对象作为参数传递给方法时,指针或引用将被复制到参数变量。 Java 称之为“按值传递”。
因此,您最终会得到至少两个引用同一对象的变量。如果将参数变量设置为 null,则由于引用计数,其他变量将保留其值。
如果你有以下方法:
和一个对象:
像这样调用方法:
你基本上在方法的本地范围内得到这个:
如果你设置
person = null
,它不会影响 < 的值代码>马库斯。这是由于引用计数。但是,如果您这样做:
这也是正确的:
您确实在参数变量中拥有对原始对象的引用。影响参数变量确实会影响原始对象。除了将其设置为 null 并期望原始值也设置为 null 之外,您可以执行所有操作。引用计数可以防止这种情况发生。
Technically, as we know, variables that reference objects are strictly pointers to the object. When passing an object as a parameter to a method, the pointer or reference gets copied to the parameter variable. Java calls this "pass by value".
So, you end up with at least two variables that reference the same object. If you set the parameter variable to null, other variables keep their value thanks to reference counting.
If you have the following method:
And an object:
Calling the method like this:
You basically get this in the method's local scope:
If you set
person = null
, it doesn't affect the value ofmarcus
. This is due to reference counting.However, if you do this:
This is also true:
You do have a reference to the original object in the parameter variable. Affecting the parameter variable does affect the original object. You can do everything except set it to null and expect the original to be set to null also. Reference counting prevents that.
是的,正如您所指出的,Java 正在按值传递引用。这对于java程序员来说是一个很好的面试话题,但大多数人似乎都弄错了。
Yes, as you pointed out, Java is passing references by value. This makes a good interview topic for java programmers, most seem to get it wrong.
您能做的最接近的就是传递 AtomicReference。这可以由调用者更改。它相当丑陋,但恕我直言,它应该是。更改方法中的参数是一个坏主意。
The closest you can do is pass AtomicReference. This can be altered by the caller. It is rather ugly, but IMHO it should be. Changing an argument in a method is a bad idea.