简体   繁体   中英

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?

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?

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. 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.)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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