• Post Reply Bookmark Topic Watch Topic
  • New Topic

Appending l/f/d to indicate type  RSS feed

 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When actually is one supposed to append 'l' to long, 'f' to float, and 'd' to double values?
float fl = 2.5; looks good
float fl = 2.5f; why??? dont we already know its a float?
Can anyone explain this concept to me please...Dying to know why we have these in java!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A floating-point constant like 2.5 is a double value in Java by default. That means that you can't assign 2.5 to a "float" variable without that "F" at the end, because you can't assign a double value to a float variable, or you'll get a "loss of precision" compiler error.

The "L" at the end of longs (never, never, never use a lower case ell!) is needed if the integer value you're writing is too large to fit in an int. In that case without the "L" you'll get a "value too large" compiler error.

You never need to use "D" after a number.
 
subhash kumar
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




float fl = 2.5; looks good
float fl = 2.5f; why??? dont we already know its a float?

how java going to distinguish between the float and double
by putting "f" i think

same way for integer those are long by the use of "L"
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:The "L" at the end of longs (never, never, never use a lower case ell!)

All too true. An l is too similar to a 1 in most fonts, so it's harder to make the distinction. 1l could be read as 11 whereas 1L is clearly 1. You'll see it better in code tags:
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i accept using F to distinguish between a float and a double, because float f = 2.5; wont work!
but when to use L??
long l = 5; and int i = 5; works good..u need not say the compiler, explicitly thats a long,right?
islong l = 5L; needed?

and Rob, only because of that similarity between 1 and l , it is not preferred to use? but anyway 'l' is also valid right?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lower-case "l" is accepted, but if I ever caught one of my team members using it, they'd get at least a written reprimand. Gah.

Note that for floats and doubles, the default for a literal is the wider/larger type (double) while for long/int, it's the smaller type (int). That's why you need "f" to assign floats, but can assign an int literal to either int or long.

You need L only for values that are too large to hold in an int; in other words, this will not compile:

long big = 8589934591;

but this will:

long big = 8589934591L;

Because 8589934591 is 0x1FFFFFFFF, which needs more bits than an int can hold.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may get a tiny performance enhancement by declaring an integer literal with the L rather than relying on the compiler to cast it for you, so long l = 1L; might be a good idea.

And please don't use so much red text; many people find it hard to read.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell - I didnt know that people might find it difficult. Sorry, i'll avoid using red.

thats great... so we are forced to use 'L' and 'F' at times..days ago i thought it was just an option...!
thanks all
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must use F or (float) if you want a float number. You can use f instead. eg float f = 123.45f; float f = (float) 123.45; Using 123.45f is more efficient than the cast.
You must use L (avoid l) if you have an integer <-2147483648 or >2147483647.
You would probably do well to use L on any integers you want to be handled as longs, eg long l = 123L; or long millisecondsInMonth = 31L * 24L * 60L * 60L * 1000L;
Note that the milliseconds in month expression is one where you can get serious errors if you miss out the Ls.
You would do well to use a D or a d if you have a whole number you want to use as a double, eg double infinity = 1d / 0d; You would get an Exception if you miss out the D/ds.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1/0 or 0/0 is supposed to throw an ArithmeticException!!! but here no!!
Cooool!! How does the 'D', do the magic???
Any interesting explanation for this?!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vinoth kumar k wrote:1/0 or 0/0 is supposed to throw an ArithmeticException!!! but here no!!
Cooool!! How does the 'D', do the magic???
Any interesting explanation for this?!


Integer divide by 0 gives the exception; floating point gives the special value Infinity.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, but...



prints "Infinity"! My doubt is that, how a double type stores a String in it?? Its not a class to over ride the toString() method to check if the value is infinity and print "Infinity" - its just a primitive type.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:long millisecondsInMonth = 31L * 24L * 60L * 60L * 1000L;

That's a good example. Although without the L's it will compile it will not do what you want due to the range of int.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a few special bit patterns that are reserved to represent special values: these values are called NaN (not a number), and positive and negative Infinity. There's no String in there -- it's just a special floating-point value which is printed specially.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote: . . . That's a good example.
It's from Java Puzzlers by Bloch and Gafter.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinoth Kumar Kannan wrote:My doubt is that, how a double type stores a String in it?? Its not a class to over ride the toString() method to check if the value is infinity and print "Infinity" - its just a primitive type.

A double does ofcourse not store a String. This does not have anything to do with strings at all. The 1D in your code is not a string.

1D is exactly the same as 1.0. It's just a notation to specify numeric literals. The only thing is does it tell the compiler "I have the number 1 here and you must treat it as a double, instead of as an integer".
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Jesper : I'm clear about that 1D is 1.0 to the compiler. What i was asking was,

would store 'infinity' in d - not exactly though. When that double is printed it just gives off a "Infinity". Similarly for "NaN".
May be as Ernest Friedman-Hill mentioned, it might result in a special bit-pattern, which when passed to System.out.println() gives "Infinity".
May be coded into the method implementation of println().
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinoth Kumar Kannan wrote:
May be as Ernest Friedman-Hill mentioned, it might result in a special bit-pattern, which when passed to System.out.println() gives "Infinity".
May be coded into the method implementation of println().


That's true for all floating point values. If System.out.println() gives "3.141592", it doesn't mean that the floating point variable is holding the string, "3.141592". The variable is holding the bit pattern for that specific value, which the printstream converted to a string that can be printed. For infinity, the variable is holding the bit pattern for infinity, which the printstream converted to a string, "Infinity", that can be printed.

Henry

 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it does not store "infinity". It calculates the IEEE754 64-bit value for (positive)∞ which is 0111 1111 1111 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000.

If the first "0" is replaced by a "1" it becomes -∞ and if any of the other "0"s is replaced by a "1" it becomes NaN.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!