• 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

Conversion between char and short ?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The below assignment causes compile time error

short a=10;
char c=a;

as both char and short are of 16-bits why implicit conversion is not done here?
I know that char variables are unsigned and short variables are signed.
Can any one explain me the problem clearly?
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ramana,

According to JLS 5.2 Assignment Conversion here


In addition, a narrowing primitive conversion may be used if all of the following conditions are satisfied:

The expression is a constant expression of type byte, short, char or int.
The type of the variable is byte, short, or char.
The value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable.



The first point gives ur solution.


short a=10;
char c=a;


since a is not a constant expression, thats why the compiler flags an error.

Try this:
final short a=10;
char c=a;

This works fine.
Hope it makes u clear
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

as both char and short are of 16-bits why implicit conversion is not done here?
I know that char variables are unsigned and short variables are signed.
Can any one explain me the problem clearly?



While both char and short are 16 bits long, a short can be negative while a char cannot be negative,

If a == -5, what should "c=a" do?

As Animesh points out, if "a" is a final nonnegative number, there is no risk of "a" not fitting into "c".
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


As Animesh points out, if "a" is a final nonnegative number...



Animesh points out that if "a" is a compile-time constant (...), not a final.
I made the distinction clear in another thread just 24 hours ago.
 
Roses are red, violets are blue. Some poems rhyme and some don't. And some poems are a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic