Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 681
Peter Avatar asked Feb 28 '10 00:02

Peter


2 Answers

Add L to the end of the number:

long bytes = 4294967296L; 
like image 170
icktoofay Avatar answered Sep 20 '22 11:09

icktoofay


To answer your question title, the maximum value of a long can be obtained via the constant:

Long.MAX_VALUE 

To solve your problem - add the l (L) literal after the number.

like image 33
Bozho Avatar answered Sep 19 '22 11:09

Bozho