aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why this works without a F/f? 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 "Why this works without a F/f?" Watch "Why this works without a F/f?" New topic
Author

Why this works without a F/f?

Shiwani Aggarwal
Greenhorn

Joined: Aug 21, 2003
Posts: 22
When we declare a float by
f = 0.0 it result in error because it defaults to Double
but why float f = 1/3; does not do the same?
And similarly why int i = 1/3; does not give any compile error, since 1/3 in fact is a floating number?
Thanks
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Hi Pretty,
The dividen and divisor are both integer, so the result will be an integer as well. However, if you a assign the result to a float or double, as in f=1/3, then a widening conversion will occur.
Hope this helps.
Shiwani Aggarwal
Greenhorn

Joined: Aug 21, 2003
Posts: 22
Thanks, it helps. Now I think, I had been stupid to ask this?
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
No, Preeti, I wouldn't feel stupid for asking. If you're new to programming, or new to Java, you will find a thousand little rules and no beginner can expect to understand them all. Heck, most experts don't understand all of them!
So, go easy on yourself while you're learning, and have some fun with it. I always look at code and math as puzzles to solve and that takes a lot of the pressure off because I've always enjoyed puzzles.
Cheers,
Jeff


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why this works without a F/f?
 
Similar Threads
Constructor doubt
float f=1/3;
why float f=1/3 is 0?
Question on float data type
wrappers