Deepak
Deepak

Reputation: 1545

declaring long types correctly in java?

I was reading about the topic base types from the book datastructures and algorithms in java. I am quoting some text from the topic which might help:

"A variable declared to have one of these types simply stores a value of that type, rather than a reference to some object. Integer constants, like 14 or 195, are of type int, unless followed immediately by an 'L' or 'l', in which case they are of type long. Floating-point constants, like 3.1415 or 2.158e5, are of type double, unless followed immediately by an 'F' or 'f', in which case they are of type float. We show a simple class in Code Fragment 1.2 that defines a number of base types as local variables for the main method." - form the book

and then in the example code:

long l = 890L;

my question is why do I need to put a L in the end if the type of l(variable) is already declared as long?

for example if I do:

long l = 890;

it still works so why in the example code its written as long l = 890L;

Upvotes: 1

Views: 476

Answers (4)

Samuel Edwin Ward
Samuel Edwin Ward

Reputation: 6695

There is a distinction between the types of the variables and the types of the literals.

In this case:

long l = 890L;

You have a variable of type long and a literal of type long. Naturally this works.

In this case:

long l = 890;

You have a variable of type long and a literal of type int. The integer is automatically promoted to a long (because it will fit) and this works too. It's the same as this:

int i = 890;
long l = i;

Note that you can't do this:

int i = 890L;

This is because although in this case 890 is small enough to fit in an integer, there is no general rule to convert longs automatically to integers because that is not always the case.

Note that you can't do this either:

long l = 3000000000;

This is because you're trying to declare a literal of type int, but the number is too big to fit in an int.

Upvotes: 2

Peter Lawrey
Peter Lawrey

Reputation: 533920

why in the example code its written as long l = 890L;

To be more explicit that the author expects the value to be a long.

Personally I prefer brevity and leave it to the developer to work out the value will be a long, if they care, and most likely they won't even think about it.

Upvotes: 0

Fabio
Fabio

Reputation: 3067

I don't know exactly why it was designed like that, but if you have an integer without the L after it, it is assumed that that is an int. Try a defining a big number (outside the int scope) without the L, the compiler will tell you there's something wrong.

Same goes for numbers that are not integers: if you want a float, you gotta add a F suffix (such as 3.1415F) otherwise it is assumed that it's a double.

Upvotes: 1

Brett Okken
Brett Okken

Reputation: 6306

This is a widening implicit conversion. Java can clearly tell that you are attempting assign the int value 890 to the long l. This is successful (without a cast) because it can be done without any data loss.

I cannot tell you why the example uses 890l. I can hypothesize that it is doing so to be explicit, but this is simply a guess.

Upvotes: 2

Related Questions