Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java JUnit assertEquals with Long

    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.

like image 361
Hamza Yerlikaya Avatar asked Jun 18 '09 14:06

Hamza Yerlikaya


2 Answers

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.

like image 152
AgileJon Avatar answered Nov 09 '22 21:11

AgileJon


42681241600 is interpreted as an int literal, which it is too large to be. Append an 'L' to make it a long literal.

If you want to get all technical, you can look up §3.10.1 of the JLS:

An integer literal is of type long if it is suffixed with an ASCII letter L or l (ell); otherwise it is of type int (§4.2.1). The suffix L is preferred, because the letter l (ell) is often hard to distinguish from the digit 1 (one).

like image 7
Michael Myers Avatar answered Nov 09 '22 21:11

Michael Myers