• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a question

 
Ashish Jain
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which of the following are legal statements?
A. float f=1/3;
B. int i=1/3;
C. float f=1.01;
D. double d = 999d;
ans given is A,B,D
Now it's confusing again, because 1/3 results in double and 1/3 also. How come they got assigned to float and int?
 
Ramesh Donnipadu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashish,
A. float f=1/3;
An integer division is performed and the result integer 0 is promoted to float 0.0 and assigned to f. Please note that integer division is different from ordinary division.
B. int i=1/3;
Again integer results in 0 being assigned to int i.
C. float f=1.01;
In Java, floating point types belong to type double by default. So, a double 1.01 can' be assigned to float f without cast. So this is wrong. Correct assignment could have been float f = (float) 1.01; or float = 1.01f;


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic