数字太长,无法传递给 Random() ?
我正在尝试做
Random generator = new Random(1309233053284);
Random
是 java.util.Random
它说数字太长,但为什么可以 System.currentTimeMillis()
传递给构造函数?它返回更大的数字。
如果您想知道的话,1309233053284
是毫秒。
I'm trying to do
Random generator = new Random(1309233053284);
Random
being java.util.Random
It says the number is too long, but why can System.currentTimeMillis()
be passed to the constructor? It returns even bigger numbers.
1309233053284
are milliseconds, if you're wondering.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能会有更好的运气:
在 Java 中,除非另有说明,所有文字数字都是
int
类型。要将您的数字解释为long
,您需要在其后面添加“L”(或“l”,但这很难与“1”区分开来,因此不太清晰) 。You may have better luck with:
In Java, all literal numbers are of type
int
unless otherwise specified. To get your number interpreted as along
, you need to suffix it with 'L' (or alternately 'l', but that is difficult to distinguish from a '1', and therefore somewhat less clear).试试这个
你应该将它指定为 long。
如果您调用
new Random(1309233053284)
,它将使用带有int
参数的构造函数。当您调用new Random(System.currentTimeMillis())
时,它使用带有long
参数的构造函数,因为System.currentTimeMillis()
返回一个长的。为了使其正常工作,您还应该通过添加l
将1309233053284
指定为 long。Try this
You should specify it as a long.
If you call
new Random(1309233053284)
, it will use the constructor taking anint
argument. When you callnew Random(System.currentTimeMillis())
, it's using the constructur taking along
argument sinceSystem.currentTimeMillis()
returns a long. To make it work, you should also specify1309233053284
to be a long by adding thel
.整数文字默认为
int
类型。您需要为 float 添加f
,为 double 添加d
,为 long 添加L
。L
优于l
,因为后者看起来像1
例如
integer literals are
int
type by default. You need to addf
for float,d
for double andL
for long.L
is preferred tol
as the later can look like1
e.g.