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 Floats Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Floats" Watch "Floats" New topic


Tim Holmes

Joined: Aug 26, 2005
Posts: 25
Why does f equal 2.0 here instead of 2.6?

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Both a and b are integers, so the division is an integer division (which involves throwing away the decimal places). Something like "a / (float) b" will force a floating-point division.
Hiral Amodia

Joined: May 14, 2008
Posts: 2
Yeah... Ulf is perfectly right....
what is happening is :-

int a=8;
int b=3;
float f =(float) (a++/b--);

Thus (a++/b--)first gives an int result as 2 which is then casted to float makint it 2.0
Tim Holmes

Joined: Aug 26, 2005
Posts: 25
That makes sense (casting int to a float). Thanks.
I agree. Here's the link:
subject: Floats
It's not a secret anymore!