• Post Reply Bookmark Topic Watch Topic
  • New Topic

Compilation errors for data types java(1.6 version)  RSS feed

 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • 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

 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!