• 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

primitive assignment

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, i am confused by the primitive assignment. Can someone explain to me? Here is one example:
Question 56.

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;
The answer given is A and B. But when i compile a test, only B doesn't give error message. And i am confused by JSL 5.2 Assignment conversion. Please throw a light on it.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi jason,
u got it right .answer should be "b" only.
1.implicit cating is done from char to int.but not in this case where it is int to char.
about the other options explicit casting is required converting from char to other integer types and vice-versa.
i hope it clears ur doubt.
bye
asheet
 
asheet anand
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi jason,
u got it right .answer should be "b" only.
1.implicit casting is done from char to int.but not in this case where it is int to char.you need explicit casting in this case
About the other options explicit casting is required converting from char to other integer types and vice-versa.
i hope it clears ur doubt.
bye
asheet
 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Always remember when assigning a type of lower storage space to that of higher, casting is required to ensure that the lost of precision is deliberately done so by the programmer.
reply
    Bookmark Topic Watch Topic
  • New Topic