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?
Amir Ghahrai
Ranch Hand
Joined: Jun 19, 2002
Posts: 110
posted
0
That's true, I think only b is valid. for a you need an explicit cast to convert it to char.
Amir
Deepali Pate
Ranch Hand
Joined: Mar 20, 2002
Posts: 114
posted
0
Thnx Anybody else wants to confirm that.
Alan Chong
Ranch Hand
Joined: Jun 05, 2002
Posts: 106
posted
0
Why not b and d ? d is definitely correct.
nupur dhawan
Ranch Hand
Joined: Jun 26, 2002
Posts: 71
posted
0
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
Nupur. <br />SCJP2.
Paul Villangca
Ranch Hand
Joined: Jun 04, 2002
Posts: 133
posted
0
A char is not wider than a byte, since a byte can take negative values, while a char cannot.
Deepali Pate
Ranch Hand
Joined: Mar 20, 2002
Posts: 114
posted
0
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
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.