• 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

Typecasting between char and byte

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
byte b3 = 1; // Line 1
char c = (byte)2; // Line 2
c = 3; // Line 3
//c = b3; // Line 4

If Line 4 is uncommented, there is a "possible loss of precision" compiler error. What I am getting confused is, when its possible to assign a byte-casted value to a char type (Line 2), why is it not possible to assign a byte-declared variable directly to a char type (Line 4) ?

I am getting very confused in all these primitives type casting

Any tips would be appreciated
[ October 15, 2008: Message edited by: Rekha Srinath ]
 
Ranch Hand
Posts: 220
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
char is 16 bit un-signed integers
it is range starts from zero (0)

However byte is signed and can include negative integers however char cannot include so if you want to assign byte;

byte can only assigned short,int, long, float, double
without a need for cast because they can cover byte value !!

byte b3 = 1; // Line 1
char c = 2; // Line 2
c = 3; // Line 3
//c = b3; // Line 4

short s = b3; //no need cast It can cover all range
double d = b3;
long l = b3;
float f = b3;
[ October 15, 2008: Message edited by: Anut Walidera ]
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anut,

How come char c = (byte)2; is possible? Through typecast, you are indirectly assigning a byte to a char, right?

But, I find that, if I say char c = (byte)-2;, I get the same loss of precision error.
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I think I am getting it.

If I say final byte b3 = 1; c = b3;, there is no compiler error because there is a guarantee through 'final' that the value of b3 will not change.

This was not the case without 'final', hence the compiler cribs sensing the possibility of change in non-final b3's value.
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Rekha. And there will not be any compiler error upto the byte value 127.
 
reply
    Bookmark Topic Watch Topic
  • New Topic