Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# JvalTest

Deepali Pate
Ranch Hand
Posts: 114
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
Posts: 110
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
Thnx
Anybody else wants to confirm that.

Alan Chong
Ranch Hand
Posts: 106
Why not b and d ?
d is definitely correct.

nupur dhawan
Ranch Hand
Posts: 71
Originally posted by Alan Chong:
Why not b and d ?
d is definitely correct.

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

Paul Villangca
Ranch Hand
Posts: 133
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
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