简体   繁体   English

类类型参数传递中的Java原语

[英]Java primitives in class type parameter passing

I know that if you pass a Class type as a parameter for a method the pointer will be passed and if you edit the parameter in the method you also edit the original object instance you passed (for example if a method named public void changeColor(Color color) does the following code: 我知道,如果您将Class类型作为方法的参数传递,则将传递指针,并且如果您在方法中编辑参数,还将编辑传递的原始对象实例(例如,如果名为public void changeColor(Color color)执行以下代码:

public void changeColor(Color color)
{
    color = new Color(20, 100, 50);
}

and you called the value like so: 您这样调用了该值:

Color color = new Color(10, 20, 30);
System.out.println(color.toString());
changeColor(color);
System.out.println(color.toString());

both lines of the console should be different ( java.awt.Color[r=10,g=20,b=30] and java.awt.Color[r=20,g=100,b=50] ), thus the value is changed and the pointer is passed. 控制台的两行应不同( java.awt.Color[r=10,g=20,b=30]java.awt.Color[r=20,g=100,b=50] ),因此值更改并传递指针。 On the other hand, if you use an int value, which is a primitive, you get different result: 另一方面,如果使用int值(它是原始值),则会得到不同的结果:

public void changeNumber(int i)
{
    i = 10;
}

and you called the value like so: 您这样调用了该值:

int n = 5;
System.out.println(n);
changeNumber(n);
System.out.println(n);

The console does not print 5 and 10 but on both lines says 5 . 控制台不会打印510但是两行都显示5 With that said, if I had this: 话虽如此,如果我有这个:

public class A
{
    public int i;
    public Color c;

    public A(int n, Color color)
    {
        i = n;
        c = color;
    }
}

And in the runnable class... 在可运行类中...

public static void main(String[] args)
{
    A a = new A(10, new Color(10, 40, 23));
    changeA(a);
}

public static changeA(A varA)
{
    varA.i = 54;
    varA.c = new Color(40, 30, 264)
}

Will i inside the A class also act as if it were part of the pointer of A or will the value if ai in the main method not change after I run changeA(a); 我在A类内是否也将其视为A指针的一部分,或者在运行changeA(a)之后main方法中的ai值不会改变? If not, what can I do so it does change? 如果没有,我该怎么办,它会改变吗?

You're looking to pass by reference in Java. 您正在寻找Java参考引用。 This isn't possible. 这是不可能的。 See the answers to this question for more details. 查看回答这个问题的更多细节。

Will i inside the A class also act as if it were part of the pointer of A or will the value if ai in the main method not change after I run changeA(a) 我在A类内是否也会像它是A指针的一部分一样工作,或者如果我运行changeA(a)后main方法中的ai值不会改变

The value of i in the A instance passed as the parameter to A will change. 作为参数传递给AA实例中i的值将更改。

But I wouldn't say that it is acting "as if it were part of the pointer of A" . 但是我不会说它的作用是“好像它是A指针的一部分” The reference to A is a single immutable value. 对A的引用是一个不变的值。 You are changing the state of the object it refers to. 您正在更改其引用的对象的状态。

By the way, this is not what "pass by reference" means, and it is not analogous to pass by reference as implemented in languages such as C# and C++, or the C hack of passing the address of a variable / member / whatever. 顺便说一句,这不是“按引用传递”的意思,也不类似于像C#和C ++这样的语言中实现的按引用传递,或者传递变量/成员/任何内容的C语言的C技巧。

This has been explain so many times before, and twice above, but this is how I like to explain it: 前面已经解释了很多次了,上面已经解释了两次,但这就是我喜欢的解释方式:

(Please note that I gloss the "internal details": I find that they are not needed to discus the semantics, but they should be understood at the end of the day. In fact, I'm not even going to discuss the evaluation strategies used except to say that Java uses call-by-value and that passed objects have call-by-object-sharing semantics, which is achieved internally by passing the reference of the object as a value.) (请注意,我掩盖了“内部细节”:我发现不需要它们来讨论语义,但是应该在一天结束时理解它们。实际上,我什至不打算讨论评估策略。用来表示Java使用call-by-value并且传递的对象具有call-by-object-sharing语义,这是通过将对象的引用作为值传递在内部实现的。)

  • When a primitive value is passed a copy is made . 当传递原始值时,将进行复制 (Although this is somewhat irrelevant to the next point because primitive values cannot be altered.) (尽管这与下一点无关,因为无法更改原始值。)

  • When an object is passed (or assigned), that same object is passed (or assigned). 传递(或分配)对象时,将传递(或分配) 同一对象 No copy of the object occurs : unless re-assigned the the relevant parameter variable will evaluate to the same object as what was passed in (or null , perhaps). 没有对象的副本出现 :除非重新分配,否则相关的参数变量将传入的对象评估为相同的对象 (或者为null )。 Since an object is itself , then mutating the object in one location -- irregardless of what "variable name" it currently has -- changes the same object everywhere. 由于对象本身就是对象,因此在一个位置进行对象更改(无论其当前具有什么“变量名称”)都可以任何地方更改同一对象。

I you want a new, independent object, then create one :) 我想要一个新的独立对象,然后创建一个:)

Happy coding. 快乐的编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM