• 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

please explain. char problem.

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

char c = 3; // this will compile, even 3 is an int value
int a = 3;
char d = a; // this won�t compile, even 3 is an int value
we have explicitely convert a to char as,
char d = (char) a;
why ?

pankaj shinde
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
char is 16 bit unsigned primitive data type where as int is 32 bit signed primitive data type.
char can be implicitly converted to an int but where as int cannot be implicitly converted to char because int range is higher than that of char.
Explicit casting is needed if we want to assign int value to char.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes but --->
char c = 3 compiles,
and
int a =3; char c =a no.

I think this is cause 3 its a compile time constant, and compiler feels happly and implicity put the cast.

If you do
final int i = 3;
char c = i;

this compiles fine.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Explicit casting is needed if the compiler doesn't know for sure that the assigned value will fit.

char c = 3 compiles


Here the compiler knows that "3" will fit into a char.

and
int a =3; char c =a no.


The compiler is only so smart. It sees a general assignment of int to char, and wants to see a cast.

If you do
final int i = 3;
char c = i;
this compiles fine.



Here, again, the compiler knows for sure that a 3 is assigned, which will fit.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
char c = 3;
or
int i = 3;
c = 1;
These both cases are of "Implicit narrowing primitive conversion on assignments" and hence it worked. it to work rules are -
1. The source should be a constant expression either of byte, short, char or int.
2. Also the destination should either be byte, short, char or int.
3. Most important the value should be in range.

To make it work...
final int i = 2; // final mentions here that its a constant source...
c = i;


On the other hand follwing would not work
int i1 = 10;
final int i2 = i1;
char c = i2;
becase, at compile time i2 does not have any value.

Hope it works.


Antonio
 
sushil bhogale
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.
Sorry... it should be

char c = 3;
or
int i = 3; ==> this should be final int i = 3
c = 1;

2. In response, I have wrongly written Antonio's name from other thread...
 
Well behaved women rarely make history - Eleanor Roosevelt. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic