File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JvalTest

 
Deepali Pate
Ranch Hand
Posts: 114
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thnx
Anybody else wants to confirm that.
 
Alan Chong
Ranch Hand
Posts: 106
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not b and d ?
d is definitely correct.
 
nupur dhawan
Ranch Hand
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Villangca
Ranch Hand
Posts: 133
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic