• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

literals

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Why should line2 give compiler error and not line1?
For line1: 1 is an integer literal and since it fits in a byte so no compiler error.
If the above statement is true then Line2 should also compile.
Please explain ?

Thank you
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no, a floating point literal is always a double precision number unless you specifically declare the literal itself as a single precision floating point number by appending it with a letter f (or F).
A double precision number never fits in a single precision number without loss of precision even if the value is small enough that it can be described by a single precision number ("possible loss of precision" errors/warnings).
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Default type for integral literals is int; for floating point is double.

The compiler automatically narrows integral types (such as to byte in your example), but does not do that for float.

To Etienne's point in next post I'm editing this to add that the compiler automatically narrows integral "literals", not all integral types like the previous sentence may have implied.
[ February 10, 2005: Message edited by: Carol Enderlin ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Careful about the "automatic narrowing" ... it doesn't always work that way. For example:



Cheers,
Etienne
 
Stinging nettles are edible. But I really want to see you try to eat this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic