• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

byte to char narrowing

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
byte b= -97;
char cd=(char)b;
System.out.println(" " +cd);
// prints ?
int id=cd;
System.out.println(" " +id);
// prints 65439
can anybody tellme what's going on and why
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
rahul,
b = -97 is a negative number. So, it is stored in its two's complement form - 1111 1111 1001 1111.
Since char is an unsigned integer, this number is assigned as a positive integer to the char variable cd. So, this value is 65439. Since most of the machines don't support unicode characters, they display '?' for displaying characters which are not in the ascii range.
When you assign the char variable to int variable id, this value 65439 is assigned. And that gets printed.
Hope this helps,
Regards,
Suresh.

Originally posted by rahul_mkar:
hi
byte b= -97;
char cd=(char)b;
System.out.println(" " +cd);
// prints ?
int id=cd;
System.out.println(" " +id);
// prints 65439
can anybody tellme what's going on and why


 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
went straight into my head.
thanks it helped
Rahul.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic