如何通过引用传递原始数据类型? [英] How do I pass a primitive data type by reference?

查看:32
本文介绍了如何通过引用传递原始数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在java中通过引用传递原始类型?例如,如何将 int 传递给可修改的方法?

How can I pass a primitive type by reference in java? For instance, how do I make an int passed to a method modifiable?

推荐答案

在 Java 中没有直接通过引用传递原语的方法.

There isn't a way to pass a primitive directly by reference in Java.

一种解决方法是将引用传递给包装类的实例,然后该类包含原语作为成员字段.为自己编写这样的包装类可能非常简单:

A workaround is to instead pass a reference to an instance of a wrapper class, which then contains the primitive as a member field. Such a wrapper class could be extremely simple to write for yourself:

public class IntRef { public int value; }

但是一些预先构建的包装类怎么样,这样我们就不必编写自己的了?好的:

But how about some pre-built wrapper classes, so we don't have to write our own? OK:

Apache commons-lang 可变*类:
优点:单线程使用的良好性能.完整性.
缺点:引入了第三方库依赖项.没有内置的并发控制.
代表类:MutableBoolean, MutableByte, MutableDouble, MutableFloat, MutableInt, MutableLong, MutableObjectMutableShort.

The Apache commons-lang Mutable* classes:
Advantages: Good performance for single threaded use. Completeness.
Disadvantages: Introduces a third-party library dependency. No built-in concurrency controls.
Representative classes: MutableBoolean, MutableByte, MutableDouble, MutableFloat, MutableInt, MutableLong, MutableObject, MutableShort.

java.util.concurrent.atomic 原子*类:
优势:标准 Java (1.5+) API 的一部分.内置并发控制.
缺点:在单线程设置中使用时性能损失很小.缺少对某些数据类型的直接支持,例如没有 AtomicShort.
代表性类:AtomicBoolean, AtomicInteger, AtomicLong,和 AtomicReference.
注意:作为用户 ColinD 在他的回答中显示,AtomicReference 可用于近似一些缺失的类,例如原子短.

The java.util.concurrent.atomic Atomic* classes:
Advantages: Part of the standard Java (1.5+) API. Built-in concurrency controls.
Disadvantages: Small performance hit when used in a single-threaded setting. Missing direct support for some datatypes, e.g. there is no AtomicShort.
Representative classes: AtomicBoolean, AtomicInteger, AtomicLong, and AtomicReference.
Note: As user ColinD shows in his answer, AtomicReference can be used to approximate some of the missing classes, e.g. AtomicShort.

长度为 1 的原始数组
OscarRyz 的回答演示了使用一个长度为 1 的数组来包装"一个原始值.
优点:快速编写.表现力.不需要第 3 方库.
缺点:有点脏.没有内置的并发控制.导致没有(显然)自我记录的代码:方法签名中的数组是否存在以便我可以传递多个值?或者它在这里作为传递引用仿真的脚手架?

Length 1 primitive array
OscarRyz's answer demonstrates using a length 1 array to "wrap" a primitive value.
Advantages: Quick to write. Performant. No 3rd party library necessary.
Disadvantages: A little dirty. No built-in concurrency controls. Results in code that does not (clearly) self-document: is the array in the method signature there so I can pass multiple values? Or is it here as scaffolding for pass-by-reference emulation?

另见
StackOverflow 问题Java 中的可变布尔字段"的答案.

我的意见
在 Java 中,您应该尽量少用或根本不使用上述方法.在 C 中,通常使用函数的返回值来传递状态代码(成功/失败),而函数的实际输出则通过一个或多个输出参数传递.在 Java 中,最好使用异常而不是返回码.这释放了方法返回值以用于承载实际的方法输出——大多数 Java 程序员发现这种设计模式比输出参数更自然.

My Opinion
In Java, you should strive to use the above approaches sparingly or not at all. In C it is common to use a function's return value to relay a status code (SUCCESS/FAILURE), while a function's actual output is relayed via one or more out-parameters. In Java, it is best to use Exceptions instead of return codes. This frees up method return values to be used for carrying the actual method output -- a design pattern which most Java programmers find to be more natural than out-parameters.

这篇关于如何通过引用传递原始数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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