• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why this works without a F/f?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, it helps. Now I think, I had been stupid to ask this?
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic