File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "a question" Watch "a question" New topic

a question

Ashish Jain
Ranch Hand

Joined: Sep 23, 2000
Posts: 50
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?

Ashish Jain<br />MCP, SJCP, IBM XML, SCEA
Ramesh Donnipadu
Ranch Hand

Joined: Sep 16, 2000
Posts: 100
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;

I agree. Here's the link:
subject: a question
jQuery in Action, 3rd edition