简体   繁体   English

Java AutoWrappers:作为方法输入的行为?

[英]Java AutoWrappers: Behavior as method inputs?

I was wondering how exactly Auto Wrappers behave when called by a method. 我想知道自动包装器被方法调用时的行为。 In Java, primitive types get called by value and objects get called by reference, but what about Auto Wrappers? 在Java中,原始类型按值调用,而对象按引用调用,但是自动包装器呢?

Do they get called by value or by reference? 它们是通过价值还是通过引用来调用?

If they do get called by value, what would be the best way to call them by reference? 如果确实按值调用它们,那么按引用调用它们的最佳方法是什么? (more layers of wrapping?) (更多的包装层?)

I was wondering how exactly Auto Wrappers behave when called by a method. 我想知道自动包装器被方法调用时的行为。 In Java, primitive types get called by value and objects get called by reference, but what about Auto Wrappers? 在Java中,原始类型按值调用,而对象按引用调用,但是自动包装器呢?

These so-called "auto wrappers" are just ordinary classes. 这些所谓的“自动包装器”只是普通的类。 And they behave like ordinary classes when you invoke methods on them, and when you pass them. 当您在它们上调用方法并传递它们时,它们的行为类似于普通类。

When you talk about values being "called by value" or "called by reference", that is terminological nonsense. 当您谈论值是“按值调用”或“按引用调用”时,这是术语上的废话。 The "by value" and "by reference" is about argument passing semantics, not about calling semantics, and in Java everything is passed by value. “按值”和“按引用”与参数传递语义有关,而不是与调用语义有关,并且在Java中,所有值均由值传递。 Everything. 一切。 In the case of reference types, you are passing the reference by value. 对于引用类型,您将按值传递引用。

(With true "pass by reference", what you are actually is the address of (typically) one of the caller's variable. The (hypothetical) called method can then update that variable. You can't do that in Java. The language doesn't support it and the JVM instruction set doesn't support it.) (使用真正的“通过引用传递”,您实际上是(通常是)调用方变量之一的地址。(假设的)调用方法可以更新该变量。您无法在Java中做到这一点。该语言不会'不支持它,而JVM指令集不支持它。)

The only unusual thing about the wrapper types is that they can be subject to auto-boxing and auto-unboxing. 包装类型的唯一不寻常之处在于,它们可以进行自动装箱和自动拆箱。 That is orthogonal to argument passing semantics, because these are both examples of "conversions", and all conversions happen before arguments are passed. 这与参数传递语义正交,因为它们都是“转换”的示例,并且所有转换都参数传递之前发生。

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

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