使用 int 或 Integer 中的哪一个
我需要创建一个数据传输对象,用于存储从数据库检索的记录。 在此数据传输对象中,我需要声明一个数字字段。 对于哪一个更好 - int 或 Integer
如果我将字段定义为整数,如果我打算将“整数”类型定义为,是否会对性能产生影响从数据库中检索超过 2000 条记录!?
提前致谢。
I need to create a data transfer object, which I will use for storing the records retrieved from database. In this data transfer object, I need to declare a numeric field. For that which one is better - int or Integer
If I am defining the field as Integer, will there be any performance impact because of 'Integer' type if I am going to retrieve more than 2000 records from DB!?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
Integer
是一个更好的选择,因为它可以处理null
; 对于int
,如果使用resultSet.getInt(..)
,null
将默默地变为0
。 否则,它可能会抛出一些异常,例如“无法将null
设置为原始属性”。性能在这里并不重要。
int
,您最终将添加额外的处理代码; 这对你没有多大好处。 您的代码将不干净、不直接,有大量样板代码,而且您甚至不会获得性能。0
,而本意是null
。 想象一下用户提交表单但没有为int
提供任何值的情况。 默认情况下,您最终会得到0
。 当数据库中该字段不为空时,这是有道理的,或者说确实如此。Integer
is a better option, as it can handlenull
; forint
,null
would become0
, silently, ifresultSet.getInt(..)
is used. Otherwise, it might throw some exception, something like, "Unable to setnull
to a primitive property".Performance is of little concern here.
int
, you will end-up adding extra handling code; and that wouldn't benefit you much. Your code will not be clean and straight-forward, lot of boiler-plate code, and you wouldn't even gain performance.0
, wherenull
was intended. Imagine the case where user submitted a form, and doesn't supply any value forint
. You will end up getting0
by default. It makes sense, or does that really, when that field isnot null
in the database.您真正应该根据您需要对象做什么来做出决定,而不是性能成本。 一旦使用分析器发现了速度问题(万恶之源),就应该根据性能做出决定。
查看两者的一些功能并根据其做出决定,例如,
Integer
可以为null
,int
则不能。 那么DB中的int
是一个Nullable
字段吗?Integer
类方法吗?就我个人而言,我总是选择原始类型而不是包装器。 但这只是一个偏好,而不是基于任何技术优点。
You should really make your decision based on- what you need your object to do, rather than the performance costs. Deciding based on performance should be done, once a speed issue has been identified with a profiler - the root of all evil and all that.
Look at some of the features of both and use that for your decision, e.g.
Integer
can benull
,int
cannot. So is theint
in the DB aNullable
field?Integer
class methods?Personally, I always opt for the primitive over the wrapper. But that's just a preference thing, rather than based on any technical merit.
在我看来,将某些东西声明为 int 或 Integer 之间的选择简单地取决于 null 是否是它的有效值。 自动装箱(和自动拆箱)将解决数字必须是一种或另一种类型的任何转换问题。 性能(正如已经指出的)在几乎所有情况下也不太可能引人注目。
此外,int 应该是自然的选择,并且如果这是一个问题的话,它可能是性能最好的。 如果您需要能够存储空值,那么您必须使用 Integer (并且还要确保对于仅采用整数的方法不会自动拆箱空值引用,因为这将导致 NullPointerException) 。
To my mind, the choice between declaring something as int or Integer simply comes down to whether null is a valid value for it or not. Autoboxing (and autounboxing) will take care of any conversion issues where the number simply must be one type or another. Performance (as has been pointed out) is also unlikely to be noticable in almost all cases.
Besides, int should be the natural choice, and is likely to be the most performant should that be an issue anyway. If you need to be able to store nulls, then you have to use Integer (and also ensure that no null references are auto-unboxed for a method that takes simply ints as this will result in a NullPointerException).
Integer
理论上比int
慢,但是除非您正在处理数字,否则对性能的影响应该很小。 JIT 优化也将减少性能损失。在原始类型或引用类型方面使用更适合您情况的类型。
Integer
is theoretically slower thanint
, however the performance impact should be minimal unless you are crunching numbers. Also JIT optimizations will reduce the performance loss.Use the one that better suits your situation in terms of primitive or reference type.
int 比整数快 10 倍,
我们使用 jetm 性能库测试此代码
,结果:
测试:对象 10.184
测试:原语1.151
int is 10x faster than integer
we test this code with jetm performance library
and the results:
test:objects 10.184
test:primitives 1.151
给您一个想法,2000 Integer 会给您的查询增加大约 0.5 毫秒。 如果你必须序列化这些数据,它可能会增加很多。
然而,正确性应该是第一位的。 跑得太快但错了是没有意义的。 您必须考虑空值以及如何处理它们。 (除非列不为 NULL)您可以使用 Integer.MIN___VALUE,也可以使用 long 字段代替 int,并使用 Long.MIN_VALUE 表示 null。 尽管它比 int 大,但它仍然比 Integer 小很多倍并且效率更高。
To give you an idea, 2000 Integer would add about 0.5 ms to you query. If you have to serialize this data it could add quite a bit more.
However, correctness should come first. There is no point being very fast but wrong. You have to consider null values and how you handle them. (Unless the column is NOT NULL) You could use Integer.MIN___VALUE or you could use a long field instead of int and use Long.MIN_VALUE for null. Even though it is larger than int, it would still be many times smaller and more efficient than Integer.
java 使用
int
进行大多数计算。Integer
用于除原始数组之外的所有形式的集合。使用大量临时整数来破坏垃圾收集器并在后台使用不可分析的 CPU,这将导致一切速度普遍减慢。 每秒丢弃太多临时数据将导致 CG 进入紧急“我现在需要内存”模式,这可能会导致延迟关键应用程序(即:实时交互式图形、物理设备控制器或通信)停顿。
所以对我来说,如果我有一个许多嵌套调用不执行数学运算,但访问许多集合,例如使用映射的键我使用 Integer 以避免传递参数时出现大量自动装箱。
如果操作是数学密集型的或用作循环计数器或其他面向数学的操作并且不存储在集合中(原始数组除外),则我使用原始数据。 除了 String 之外的所有其他基元都是如此,String 是一个成熟的对象。
int
is used by java for most all calculations.Integer
is used in all forms of Collections except for primitive arrays.Using lots of temporary Integers with thrash the garbage collector and use unprofilable CPU in the background that will cause general slowdowns in everything. Too many temporaries being trashed per second will cause the CG to enter emergency "I need memory now" mode that can cause stalls in latency critical applications (ie: real time interactive graphics, physical device controllers or communications)
So for me if I have a lot of nested calls that do no math but access a lot of collections such as using keys for maps I use Integer so as to avoid tons of auto boxing when arguments are passed.
If the operations are math intensive or used as loop counters or other math oriented operations and not stored in collections (other than primitive arrays) I use the primitive. The same goes for all the other primitives except String which is a full fledged object.
我想这尤其取决于您用于访问数据库的内容。 使用普通的老式 JDBC,您可以使用
int
进行操作,而 ORM 无论如何都可以将它们默默地转换为Integers
。 Integer 允许您处理空值。I guess it depends among other things on what you are using for accessing the database. With plain old JDBC you could do with
int
s, while an ORM could silently convert them toIntegers
anyway. And Integer would allow you to handle nulls.无法使用
toString
或(String)
将int
转换为String
。Integer
可以使用toString
或(String)
转换为String
,并且可以处理null
>。int
can't be cast to theString
with using thetoString
or(String)
.Integer
can cast toString
withtoString
or(String)
and it can handlenull
.如果您想检查
null
值,那么Integer
是最好的,但如果您想比较整数,那么 int 可能更好。 在下面的示例中,我使用整数 c= 1000 和 d= 1000 并比较它返回 false,但如果是 int,它们将返回 true。If you want to check for a
null
value thenInteger
is best but if you want to compare the integer then int may be better. In the following example I am using integer c= 1000 and d= 1000 and compare it return false but in case of int they will return true.要涵盖的一种场景是验证。
假设我们有以下类:
如果用户没有在请求中提供
val
的值,我们将得到一个令人讨厌的NumberFormatException
。如果将
int
替换为Integer
,我们可以使用@NotNull
并更优雅地解决这个问题。One scenario to cover would be validation.
Imagine we have the following class:
If the user doesn't provide a value for
val
in the request, we will get a nastyNumberFormatException
.If
int
is replaced withInteger
, we can use@NotNull
and resolve this issue more gracefully.