File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes primitive assignment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "primitive assignment" Watch "primitive assignment" New topic
Author

primitive assignment

Jason Li
Ranch Hand

Joined: Jun 24, 2001
Posts: 46
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.


green horn
asheet anand
Ranch Hand

Joined: Nov 15, 2000
Posts: 83
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

Joined: Nov 15, 2000
Posts: 83
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

Joined: Apr 06, 2001
Posts: 371
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: primitive assignment
 
Similar Threads
QUICK assignments exam q ..
JvalTest
primitive assignment
Question about primitive assignments
primitive assignment problem