Assigning a Value to Long Data Type in Java - Defaulting to Int
long val = 5000000000;

The error during this assignment is:

The literal 5000000000 of type int is out of range

Why does the compiler by default assume the literal to be type int when it is declared with type long?

1

4 Answers

You can use:

long val = 5000000000L;

Check it here

2

There is aspecific suffixes for long i.e L. If there is no suffix, then 5000000000 assumed to be an int type. And 5000000000 is out of int range, causing the erro. So you need to add L at the end of 5000000000 for it to be treated as a long value. Change your declaration from

long val = 5000000000;

to

long val = 5000000000L;

Add the letter L at the end of your number as shown below

long val = 5000000000L;

long: The long data type is a 64-bit signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive).

You should append 'l' or 'L' to the value explicitly used to initialize the variable; even as small as 0.

 long val = 0L;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest