File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

int i = 1/3;

 
Soum Sark
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why does it compile without giving an error . Doesnt 1/3 produce a double no (0.3333) and thus it would be a loss of precision to push it into an int ?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No it does not (give 0.33333).
Do you have any Java books? If not there are some free ones available, and excellent tutorials are available here on JR. You may have to read some sections several times to get some topics clear.
If you are really, really stuck then please repost.
-Barry
[ August 17, 2002: Message edited by: Barry Gaunt ]
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java Tutorial at java.sun.com is very good.
Thinking in Java at bruceeckel.com is a free masterpiece.
 
Ron Newman
Ranch Hand
Posts: 1056
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If both operands are integers, the result will be an integer. Integer division truncates, so 1/3 is 0. If you want a double, you need to say 1/3.0 or 1.0/3 instead.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Ron, you are too good.
 
Soum Sark
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Barry, you should learn something from others
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic