This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt about a float value Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Doubt about a float value" Watch "Doubt about a float value" New topic
Author

Doubt about a float value

Justen Hill
Greenhorn

Joined: Nov 07, 2007
Posts: 5
int a = 8;
int b = 3;
float c = a++/b--;
System.out.println(c);

Why does this produce 2.0? Shouldn't the result be able to handle a more accurate answer?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56528
    
  14

Think about at what point in the calculatiuon of your expression the implicit conversion from int to float occurs.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10040
    
    6

I would say that that is pretty darn accurate. In fact, I would say it is precise.

what would you get if you did this?

int a = 8;
int b = 3;
int c = a++/b--;
float d = c;
System.out.println(d);


Never ascribe to malice that which can be adequately explained by stupidity.
Justen Hill
Greenhorn

Joined: Nov 07, 2007
Posts: 5
I see it now. The division of the ints occurs first, then the result is stuffed in a float.

Is it actually a float since it doesn't have an 'f' assignment or is it a double?
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Doubt about a float value
 
Similar Threads
Primitive casting
is this a legal overriding method?
Primitive casting
Overload
Overloaded functions?