• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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)
 
snakes are really good at eating slugs. And you wouldn't think it, but so are tiny ads:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic