英文:
Advantage of returning boxed primitive values from library function
问题
翻译部分
背景
我正在研究一些 Java Redis 客户端,比如 Lettuce 和 Jedis。我注意到这两个库都将它们的方法定义为返回装箱的基本数据类型,而不是直接的原始数据类型。
例如,sadd()
返回的是 Long
,而不仅仅是 long
。
https://lettuce.io/lettuce-4/release/api/com/lambdaworks/redis/api/sync/RedisSetCommands.html#sadd-K-V...-
和
https://github.com/xetorthio/jedis/blob/d7aba13a8b65e66dedc01c51b73e3794cbe68a62/src/main/java/redis/clients/jedis/commands/BinaryJedisCommands.java#L139
问题
为什么返回装箱的基本数据类型呢?
据我所知,装箱会增加性能开销。
对于我的库来说,它使用了 Redis 库,将值保持为装箱状态并将其传播给库的用户,这样做是否有意义呢?
编辑:这些方法中没有一个返回 null
,因此将特殊含义编码到 null
中并不适用于它们。
英文:
Context
I am looking at a couple of Java Redis clients like Lettuce and Jedis. I see that both libraries have defined their methods to return boxed primitive types rather than straight primitives.
For example, sadd()
returns Long
rather than just long
.
https://lettuce.io/lettuce-4/release/api/com/lambdaworks/redis/api/sync/RedisSetCommands.html#sadd-K-V...-
and
https://github.com/xetorthio/jedis/blob/d7aba13a8b65e66dedc01c51b73e3794cbe68a62/src/main/java/redis/clients/jedis/commands/BinaryJedisCommands.java#L139
Question
What could be the reason for returning boxed primitives?
From what I understand boxing adds performance overhead.
Would it make sense for my library which uses Redis library, to keep and the return values boxed and propagate it to users of the library?
Edit: None of the methods return null
, so encoding special meaning to null does not apply to them.
答案1
得分: 2
Boxed primitives can be used in Collections whereas regular primitives can't (although they can still be added to a Collection of the corresponding boxed type), and also allow you to work with references instead of values, which saves time and space in some circumstances (i.e when the size of the reference is smaller than the size of the data). But as you say, boxed primitives incur the costs of memory and performance overhead.
英文:
Boxed primitives can be used in Collections whereas regular primitives can't (although they can still be added to a Collection of the corresponding boxed type), and also allow you to work with references instead of values, which saves time and space in some circumstances (i.e when the size of the reference is smaller than the size of the data). But as you say, boxed primitives incur the costs of memory and performance overhead.
答案2
得分: 2
依我看,主要原因是能够检索到一个 Object
而不是一个原始类型。例如,可以检索到 null
。
像 Integer
、Long
这样的数值包装类,在大约 -128;+127
之间被缓存为值。所有其他值将在内存中复制。而且,存储 Long
所需的空间是存储 long
的三倍多。
一般情况下,我遵循以下规则。
如果不需要返回 null
,你应该优先返回原始值而不是数值包装类。
英文:
In my opinion the main reason is to ability to retrieve an Object
instead of a primitive type. E.g. it's possible to retrieve null
.
Numeric wrappers like Integer
, Long
... are cached values between around -128; +127
. All other values will be duplicated in memory. Moreover to store Long
it takes over 3 times more space than store long
.
In general case I following following rule.
If you do not need to return null
you should prefer to return a primitive value than numeric wrapper.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论