Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# why there must be L after long number

Aziza Korikova
Greenhorn
Posts: 11
My previous related post was marked as "resolved" for some reason so i guess i wont get answer there, even though i still obviously had question.
My question is:
Why must there be L after a long number, but not F or D after float/double? I have never heard of L or F or D until I got compiler message for not putting L. What is the reasoning behind this, why cant we work with longs the same as with floats and double and other types?

Mohana Rao Sv
Ranch Hand
Posts: 485
Try to call these methods by passing parameters.

Aziza Korikova
Greenhorn
Posts: 11
Mohana Rao Sv wrote:Try to call these methods by passing parameters.

ok...I called first one with 10, second with 10.2, and third with 9999999999L, and it worked as expected. But this doesnt answer my question. My question is why I must put L after number greater than the range of int, but I dont have to put D or F after a double or float; ie, why i cant just pass in 9999999999 for the third method, but i can just pass in 10.2, not 10.2D, for the second method. I just want to know the reasoning behind this because it is my first time seeing this and it strikes me as strange

Mohana Rao Sv
Ranch Hand
Posts: 485
you are saying 10.2 is double but I say it's float. But how the java understands what type of parameter you are passing.

Aziza Korikova
Greenhorn
Posts: 11
Mohana Rao Sv wrote:you are saying 10.2 is double but I say it's float. But how the java understands what type of parameter you are passing.

well alright so now i passed in 4.94065645841246544e-324 , which is definitely a double, not float. I did not get error message saying i need to put D after the number like i did for long type..

Jeff Verdegan
Bartender
Posts: 6109
6
Mohana Rao Sv wrote:you are saying 10.2 is double but I say it's float.

You're mistaken. Every integer literal without further qualification is an int. Every floating point literal without further qualification is a double. Those are the rules defined by the Java language.

But how the java understands what type of parameter you are passing.

Every expression has a type. Java has rules for matching up the types of the expressions passed as args with the declared parameter types of the method, doing automatic conversions in some cases.

Jeff Verdegan
Bartender
Posts: 6109
6
Aziza Korikova wrote:
Mohana Rao Sv wrote:you are saying 10.2 is double but I say it's float. But how the java understands what type of parameter you are passing.

well alright so now i passed in 4.94065645841246544e-324 , which is definitely a double, not float. I did not get error message saying i need to put D after the number like i did for long type..

If you don't put a D, it's already a double. But for integers, if you don't put an L, it's an int, so if the number is too big for an int, it's a compiler error.

Aziza Korikova
Greenhorn
Posts: 11
Jeff Verdegan wrote:
Aziza Korikova wrote:
Mohana Rao Sv wrote:you are saying 10.2 is double but I say it's float. But how the java understands what type of parameter you are passing.

well alright so now i passed in 4.94065645841246544e-324 , which is definitely a double, not float. I did not get error message saying i need to put D after the number like i did for long type..

If you don't put a D, it's already a double. But for integers, if you don't put an L, it's an int, so if the number is too big for an int, it's a compiler error.

Right and my point is, why this extra step of putting L? Why does the compiler not immediately recognize a longer number as a long, not int, the way the compiler automatically recognizes a longer decimal number as a double, not float? I am just asking for the reasoning behind this

Jeff Verdegan
Bartender
Posts: 6109
6
Aziza Korikova wrote:
Jeff Verdegan wrote:
Aziza Korikova wrote:
Mohana Rao Sv wrote:you are saying 10.2 is double but I say it's float. But how the java understands what type of parameter you are passing.

well alright so now i passed in 4.94065645841246544e-324 , which is definitely a double, not float. I did not get error message saying i need to put D after the number like i did for long type..

If you don't put a D, it's already a double. But for integers, if you don't put an L, it's an int, so if the number is too big for an int, it's a compiler error.

Right and my point is, why this extra step of putting L? Why does the compiler not immediately recognize a longer number as a long, not int,

Because the rules of Java say that every integer literal expression without other qualification (like an "L") is an int. And if you want to know why the designers chose that rule, you'll have to ask them.

the way the compiler automatically recognizes a longer decimal number as a double, not float?

It doesn't do that. Every floating point literal expression without the qualifying F is of type double, even if it would fit into a float.

Campbell Ritchie
Sheriff
Posts: 50690
83
You should use the L even after small values, eg 123L, so the compiler puts them into 64 bits rather than its having to undergo a widening conversion later.

Henry Wong
author
Marshal
Posts: 21734
85
Aziza Korikova wrote:
Right and my point is, why this extra step of putting L? Why does the compiler not immediately recognize a longer number as a long, not int, the way the compiler automatically recognizes a longer decimal number as a double, not float? I am just asking for the reasoning behind this

The reason for the compiler behaving that way is because that is how it is defined in the Java Language Specification. The default for floating point literals is double. And the default for whole numbers is int. And there is nothing in the JLS that specifies detection and auto-conversion of literals.

Henry

Jeff Verdegan
Bartender
Posts: 6109
6
Campbell Ritchie wrote:You should use the L even after small values, eg 123L, so the compiler puts them into 64 bits rather than its having to undergo a widening conversion later.

The widening conversion is done at compile time, but even if it were at runtime, I wouldn't consider it a reason to to add the L. I usually do add the L to long literals though, even when not needed, for clarity and documentation, and also to make sure I have the right type. If I call foo(123L) and it compiles, I know that method takes a long arg.