aspose file tools*
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

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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: 39547
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.

Ping & DNS - updated with new look and Ping home screen widget
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
Similar Threads
doubt regarding division
Dividing floats
Using Postfix notation......
converting a float to an integer?
Need help with a++/b-- expression