• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Boxing and Autoboxing

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.final int b = 45;
Short sh = b;


2. final long c = 45;
Short variable = c;


1. compiles but 2. doesnot compile.Can somebody please explain the reason
for the same
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
5.2 Assignment Conversion:


In addition, if the expression is a constant expression (�15.28) of type byte, short, char or int :
*A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.
*A narrowing primitive conversion followed by a boxing conversion may be used if the type of the variable is :
**Byte and the value of the constant expression is representable in the type byte.
**Short and the value of the constant expression is representable in the type short.
**Character and the value of the constant expression is representable in the type char.


In your case, the expression is a constant expression of type long and it is not covered by the special case. Therefore, a explicit cast must be used.
 
joshua antony
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks wise.
But I am still not sure how this works.
".......constant expression is representable in the type of the variable."
What does this representable mean in the exact context.
Please bear with me if I am asking silly question, I am not able to get it.
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The value of the constant expression is representable in the type byte from -128 to 127.

[ August 23, 2006: Message edited by: wise owen ]
 
joshua antony
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
final long c = 45;
Short variable = c;

In this case also the value of the constant expression(which is 45) is representable in the type short.
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In your case, the expression is a constant expression of type long and it is not covered by the special case. Therefore, a explicit cast must be used.

 
joshua antony
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks wise,
I got it now
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!