title: Applying casts to the results of integer and floating point division: what's going on here?
body: <p>I'm a beginner and there's something that's not making much sense to me. Please could be so kind as to explain where I'm going wrong. I'm sorry if this has been asked before.</p>
  
  <p>Here the presence of the decimal point mean these get evaluated using floating point division.</p>
  
  <pre><code>System.out.println(1/3.0); // this prints: 0.3333333333333333
  System.out.println(1.0/3); // this prints: 0.3333333333333333
  System.out.println(1.0/3.0); // this prints: 0.3333333333333333
  </code></pre>
  
  <p>Apparently, this below is an example of "truncating-integer division." It seems a bit weird to me but ok.</p>
  
  <pre><code>System.out.println(1/3); // this prints: 0
  </code></pre>
  
  <p>Is it ok to say: "in the line below, the (double) cast is evaluated 1st. It effectively says: "treat 1/3 as a double - don't use truncating integer division. instead use floating point division.""</p>
  
  <pre><code>System.out.println((double)1/3); // this prints: 0.3333333333333333
  </code></pre>
  
  <p>Below, however we get 0.0 - how did that happen?</p>
  
  <pre><code>System.out.println((double)(1/3)); // this prints: 0.0
  </code></pre>
  
  <p>ok so maybe the extra parentheses mean the (1/3) gets evaluated 1st. It is evaluated using truncating integer division yielding 0. And <em>then</em> the double is applied giving us 0.0 Ok that makes sense</p>
  
  <p>Ok so we maybe can propose a couple of general rules here:</p>
  
  <p>Rule 1: (double) expression means apply the (double) cast first then evaluate the expression.</p>
  
  <p>Rule 2: (double) (expression) means evaluate the expression <em>then</em> apply the cast. 
  Great!</p>
  
  <p>So in the next line below we have: (int) expression, so I guess we can apply rule 1). The (int) cast is evaluated 1st. It effectively says: "treat 1.0/3 as a int - don't use as much memory as you would with a double. Don't use floating point division, instead apply truncating integer division."" So we have 0 right? No.</p>
  
  <pre><code>System.out.println((int)1/3.0); // this prints: 0.3333333333333333
  </code></pre>
  
  <p>Ok so we have 0.33333 so the (int) cast is not evaluated 1st. It is as if it wasn't there. Let's propose a 3rd rule:</p>
  
  <p>Rule 3: (int) expression means ignore the (int) cast altogether just evaluate the expression like the (int) isn't even there. </p>
  
  <p>Ok applying rule 3 to the line below, we have (int) but we are just going to ignore it.  1.0/3.0 is evaluated with floating point division and we get 0.3333333. Success!</p>
  
  <pre><code>System.out.println((int)1.0/3.0); // this prints: 0.3333333333333333
  </code></pre>
  
  <p>And in the last line below, again we have (int) but we are just going to ignore it. (1.0/3) is evaluated using floating point division yielding 0.3333333333 right? No.</p>
  
  <pre><code>System.out.println((int)(1.0/3)); // this prints: 0
  </code></pre>
  
  <p>Ok now I'm confused. Please could you help me get my head round this?</p>