• 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

JvalTest

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Select the valid primitive assignments of the following.
A.int i = 10;
char c = i;
B.float f;
long l = 100L;
f = l;
C.short s = 20;
char c = s;
D.byte b = 20;
char c = b;
E.short s1 = 10;
short s2 = 20;
short result = s1*s2;
This is a question from JVALTEST answer given ia a and b which i think is wrong. Shouldnt it be only b?
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's true,
I think only b is valid. for a you need an explicit cast to convert it to char.
 
Deepali Pate
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thnx
Anybody else wants to confirm that.
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not b and d ?
d is definitely correct.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alan Chong:
Why not b and d ?
d is definitely correct.


Please note :
5.1.3 Narrowing Primitive Conversions
The following 23 specific conversions on primitive types are called the narrowing primitive conversions:
byte to char
short to byte or char
char to byte or short
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A char is not wider than a byte, since a byte can take negative values, while a char cannot.
 
Deepali Pate
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alan Chong:
Why not b and d ?
d is definitely correct.


Byte is smaller than char but can have negative values which are not there in char so u need a cast there.
Same theory why these dont work
byte to char
short to char/byte
char to byte/short
HTH
reply
    Bookmark Topic Watch Topic
  • New Topic