Which of the following are legal statements? 1) float f=1/3; 2) int i=1/3; 3) float f=1.01; 4) double d=999d; answer is 1,2,4 how come not only 4?
Ajith Kallambella
Sheriff
Joined: Mar 17, 2000
Posts: 5781
posted
0
1) float f=1/3; Is right because the type of 1/3 is int which can be converted to float without losing precision. 2) int i=1/3; Strange it may seem, this is right too. The type of expression 1/3 happens to be int here and it gets converted to int i. Though the precision is lost, this is a valid expression.
3) float f=1.01; You know why this is wrong. Integer literals with a decimal point in them are considered as double, so you need an explicit cast. 4) double d=999d; Perfectly legal. So the answers are 1,2,4.
Since there is nothing wrong with this question( and Marcus is ) I am moving this to SCJP forum
Ajith [This message has been edited by Ajith Kallambella (edited August 29, 2000).]
Open Group Certified Master IT Architect.
Sun Certified Architect(SCEA).