• 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 ...
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • 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

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


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


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.


Posts: 76432
  • 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
    Bookmark Topic Watch Topic
  • New Topic