Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

primitive assignment

 
Jason Li
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • 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.
 
asheet anand
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic