KnowledgeBoat Logo

Computer Science

Explain the need to use suffix L in a long type data.

Values & Data Types Java

4 Likes

Answer

The default data type of integer literals is int. So if I try to assign a value to a variable of long type like this:

//Will result in Compilation Error
long worldPopulation = 7780000000;

This will result in a compilation error of "integer number too large". The reason for this error is that Java compiler is treating the number 7780000000 as an int and as this value is outside the range of int so we are getting this error. To fix this, we need to explicitly tell the compiler to treat this number as a long value and we do so by specifying L as the suffix of the number. The case of L doesn’t matter, both capital and small L are equivalent for this. The correct way to write the above statement using the suffix L is this:

//Correct way to assign long value
long worldPopulation = 7780000000L;

Answered By

1 Like


Related Questions