Why can't I assign a 'long' a value of 4 billion?

Peter picture Peter · Feb 28, 2010 · Viewed 61.8k times · Source

I'm trying to declare a long value in Java, which unfortunately does not work.

This is my code. It results in the following error message: "The literal 4294967296 of type int is out of range".

long bytes = 4294967296;

I need this value to make a file filter that filters out files that are bigger than 4294967296 bytes (4GB). The other way round works without any issues (long size = file.length()) with every file size, which is why I can't figure out why my declaration is not working.

Answer

icktoofay picture icktoofay · Feb 28, 2010

Add L to the end of the number:

long bytes = 4294967296L;