Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this Java long overflowing? [closed]

This is very weird.

A class I wrote has the following data member:

final static long MAX_FILE_SIZE_BYTES = 50000000000L;

at one point in my code the following block is run

System.out.println("MAXFILESIZEBYTES: " + MAX_FILE_SIZE_BYTES);

and the output is:

MAXFILESIZEBYTES: -1539607552

My question is, why is this long value overflowing? Java is supposed to be machine independent, and longs are supposed to hold 64 bits. What gives?

like image 877
almel Avatar asked Jul 26 '26 21:07

almel


1 Answers

Cannot reproduce.

50000000000L is 0x0000000BA43B7400.

-1539607552 is FFFFFFFFA43B7400, which is what you would get if you cast the value to int.

Ergo somewhere you are casting it to int. Maybe you have a shadowed variable.

like image 54
user207421 Avatar answered Jul 28 '26 11:07

user207421