my dog learned polymorphism*
The moose likes Beginning Java and the fly likes why there must be L after long number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why there must be L after long number" Watch "why there must be L after long number" New topic
Author

why there must be L after long number

Aziza Korikova
Greenhorn

Joined: Apr 11, 2012
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

Joined: Aug 01, 2007
Posts: 485

Try to call these methods by passing parameters.



ocjp 6 — Feeding a person with food is a great thing in this world. Feeding the same person by transferring the knowledge is far more better thing. The reason is the amount of satisfaction which we get through food is of only one minute or two. But the satisfaction which we can get through the knowledge is of life long.
Aziza Korikova
Greenhorn

Joined: Apr 11, 2012
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

Joined: Aug 01, 2007
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

Joined: Apr 11, 2012
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

Joined: Jan 03, 2004
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

Joined: Jan 03, 2004
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

Joined: Apr 11, 2012
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

Joined: Jan 03, 2004
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

Joined: Oct 13, 2005
Posts: 36468
    
  16
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
Sheriff

Joined: Sep 28, 2004
Posts: 18104
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why there must be L after long number
 
Similar Threads
Overloading with Var-args and Boxing Doubt
Math.round Question
Var-Args Doubt
Help On Characters!
range of long data type