Java JUnit assertEquals with long

assertEquals( new Long(42681241600) , new Long(42681241600) ); 

I try to check for two long numbers, but when I try to compile this I get

  integer number too large: 42681241600 

mistake. The documentation shows that there is a Long, Long assertEquals method, but it is not called.

+6
java long-integer junit
source share
4 answers

Do you want to:

 assertEquals(42681241600L, 42681241600L); 

Your code called assertEquals (Object, Object). You also needed to add an โ€œLโ€ at the end of your numbers to tell the Java compiler that the number should be compiled as long, not int.

+17
source share

42681241600 is interpreted as an int literal that is too large. Add "L" to make it a long literal.

If you want to get all the technical, you can find ยง3.10.1 JLS :

An Integer literal is of type long if it is a suffix with the letter ASCII L or L (ell); otherwise, it is an int type (ยง4.2.1) . The suffix L is preferred because the letter L (ell) is often difficult to distinguish from the number 1 (one).

+7
source share

add "L" at the end of your number, for example:

 new Long(42681241600L) 

in Java, each literal number is treated as an integer.

+2
source share

You should also consider using Long.valueOf, as this may allow some optimization:

 Long val = Long.valueOf(1234L); 

From the 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 usually be used in preference to the Long (long) constructor, since this method is likely to get significantly better space and runtime by caching frequently requested values.

0
source share

All Articles