Java JUnit assertEquals with Long
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1012994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java JUnit assertEquals with Long
提问by Hamza Yerlikaya
assertEquals( new Long(42681241600) , new Long(42681241600) );
I am try to check two long numbers but when i try to compile this i get
我试图检查两个长数字,但是当我尝试编译它时,我得到了
integer number too large: 42681241600
error. Documentation shows there is a Long,Long assertEquals method but it is not called.
错误。文档显示有一个 Long,Long assertEquals 方法,但它没有被调用。
回答by AgileJon
You want:
你要:
assertEquals(42681241600L, 42681241600L);
Your code was calling assertEquals(Object, Object). You also needed to append the 'L' character at the end of your numbers, to tell the Java compiler that the number should be compiled as a long instead of an int.
您的代码正在调用 assertEquals(Object, Object)。您还需要在数字末尾附加“L”字符,以告诉 Java 编译器该数字应编译为 long 而不是 int。
回答by Michael Myers
42681241600 is interpreted as an int
literal, which it is too large to be. Append an 'L' to make it a long
literal.
42681241600 被解释为一个int
文字,它太大了。附加一个“L”以使其成为long
文字。
If you want to get all technical, you can look up §3.10.1 of the JLS:
如果您想获得所有技术信息,可以查看JLS 的 §3.10.1:
An integer literal is of type
long
if it is suffixed with an ASCII letterL
orl
(ell); otherwise it is of typeint
(§4.2.1). The suffixL
is preferred, because the letterl
(ell) is often hard to distinguish from the digit1
(one).
long
如果以 ASCII 字母L
或l
(ell)为后缀,则为整数文字类型;否则它是类型int
(§4.2.1)。后缀L
是首选,因为字母l
(ell) 通常很难与数字1
(one)区分开来。
回答by cd1
append an "L" at the end of your number, like:
在您的号码末尾附加一个“L”,例如:
new Long(42681241600L)
in Java, every literal number is treated as an integer.
在 Java 中,每个文字数字都被视为一个整数。
回答by paulcm
You should also generally consider using Long.valueOf since this may allow some optimisation:
您通常还应该考虑使用 Long.valueOf 因为这可能允许一些优化:
Long val = Long.valueOf(1234L);
From the J2SDK:
从J2SDK:
public static Long valueOf(long l)
Returns a Long instance representing the specified long value. If a new Long instance is not required, this method should generally be used in preference to the constructor Long(long), as this method is likely to yield significantly better space and time performance by caching frequently requested values.
public static Long valueOf(long l)
返回表示指定 long 值的 Long 实例。如果不需要新的 Long 实例,则通常应优先使用此方法而不是构造函数 Long(long),因为此方法可能会通过缓存频繁请求的值来显着提高空间和时间性能。