• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Compilation errors for data types java(1.6 version)

 
Ranch Hand
Posts: 118
Android Objective C Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

In integral data types where by default any literal is considered as int, if any literal is exceeding the intended data type range then we get "Possible loss of precision" error

i.e

for byte data type the the range of literals is -128 to 127

now

if we have byte b = 128;

compilation error: possible loss of precision

found : int
required : byte

now as per my understanding

we get this compilation error only if the literal is within the data types scope i.e. this error occurs in integral/floating data types if it is exceeding the range of the specified data type. if the literal is any other data type value then "incompatible types" error occurs.

Is this correct or my understanding is wrong.

Thanks

 
Marshal
Posts: 76432
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, the value 128 is outwith the range −128…127. So it cannot be a valid byte. The literal is always an int unless it ends with L. If you have a compile‑time constant which is within the range of a narrower datatype, the compiler will permit such assignment without a cast. Yes, I think you have got it right.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic