• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

byte to char casting

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anyone explain this
byte a = 10 ;
final int x = 10 ;

Which of the following assigments are valid ?

Options :

a . byte y = x ;
b . char ch = a ;
c . char c = x ;
d . short j = a = x ;

I think the answer should be a,b,c,d but given is a,c,d
Why is that i can't assign a byte(8bits) to a char(16bits)
does this require an explicit cast??
TIA
[ June 01, 2004: Message edited by: Sudhakar Krishnamurthy ]
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
char is unsigned.. and byte is signed..so conversion from byte to char might result in loss of information/precision..so..byte to char not allowed.. Friends,am i rite?
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Widening conversions
byte ->short -> int -> long -> float-> double
char->int-> long -> float -> double

Narrowing conversions
byte<-short <- int <- long <- float<- double
char<-int<- long <- float <- double
byte<->char
short<->char

Narrowing conversions require the use of casting.
The casting may be not needed if what is converted is a final constant and thereby as good as a literal and within range of the destination type
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Read this if it's not 100% clear..

Greetz
 
I need a new interior decorator. This tiny ad just painted every room in my house purple.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic