• 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

primitive conversion and assignment

 
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.

Could someone help me to understand it please?

Please, look at comments on code...




tks
 
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at these points:

1> All the integer values are of type int by default.
2> Java does not support implicit narrowing of data types. (Compile time error otherwise)
3> But when a byte, short or char type variable is initialized with a "Constant Expression" and if the value is within the range of type concerned then ONLY implicit narrowing takes place.(Compile time error otherwise)
 
Adolfo Eloy
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

swaraj gupta wrote:Have a look at these points:

1> All the integer values are of type int by default.
2> Java does not support implicit narrowing of data types. (Compile time error otherwise)
3> But when a byte, short or char type variable is initialized with a "Constant Expression" and if the value is within the range of type concerned then ONLY implicit narrowing takes place.(Compile time error otherwise)



Swaraj, thanks for reply.

Considering the statement 3, I thought that the code below should compile because 10 can be narrowed to a byte.


when I make the assignment of
byte b = 10; the compile narrowed it.


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

But when a byte, short or char type variable is initialized with a non-long, compile-time, "Constant Expression" and if the value is within the range of type concerned then ONLY implicit narrowing takes place.(Compile time error otherwise)


The implicit narrowing conversion is only applicable within an assignment context and not any other(including method invocation context)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic