java中的int用完了20亿,还有其他选择吗? [英] int in java runs out of 2 billion, any alternative?

查看:361
本文介绍了java中的int用完了20亿,还有其他选择吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java应用程序中使用int作为PK,现在它已达到max int值(20亿),即使在DB中它也可以存储比它更多的数字。但是java int只能容纳大约20亿。

I used int as PK in Java application, now it has reached the max int value (2 billions), even in DB it can store the number more than it. but java int is only able to hold around 2 billions.

我无法将int更改为long以与DB对齐。因为这是一项巨大的努力。

I am unable to change int to long to align to DB. because it's huge effort.

除此之外,有人有办法吗?

except this, anybody have any approach?

推荐答案

Java中Integer的最大值为2,147,483,647。如果你需要更大的数字,你将不得不改变。你也可以使用整数的负范围,但是如果你已经达到了最大值,很可能你很快就会用完房间。

The maximum value for an Integer in Java is 2,147,483,647. If you need a bigger number, you'll have to change to a long. You could also use the negative range of the Integer, but if you've already hit the maximum, the likelihood is that you'll run out of room pretty soon.

但是,如果数据库中没有20亿个元素,则可以重用未使用的主键。这可能效率很低,因为您必须搜索未使用的密钥。

However, if you don't have 2 billion elements in the DB, you could reuse the unused primary keys. This would probably be inefficient, because you'd have to search for unused keys.

我建议您只需更改代码即可。从长远来看,付出努力将获得回报。

I'd suggest just going through the effort of changing the code. Putting in the effort now will pay off in the long run.

这篇关于java中的int用完了20亿,还有其他选择吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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