title: If a Java method's return type is int, can the method return a value of type byte?
body: <p>I'm preparing myself for the Java SE 7 Programmer I exam (<a href="http://education.oracle.com/pls/web_prod-plq-dad/db_pages.getpage?page_id=5001&amp;get_params=p_exam_id:1Z0-803" rel="nofollow noreferrer">1Z0-803</a>) by reading a book called <a href="https://rads.stackoverflow.com/amzn/click/com/1617291048" rel="nofollow noreferrer" rel="nofollow noreferrer">OCA Java SE 7 Programmer I Certification Guide</a>. This book has a numerous amount of flaws in it despite the author having 12 years of experience with Java programming and despite a technical proofreader, presumably on a salary.</p>
  
  <p>There is one thing though that makes me insecure. The author says on page 168 that this statement is true:</p>
  
  <blockquote>
    <p>If the return type of a method is <code>int</code>, the method can return a value
    of type <code>byte</code>.</p>
  </blockquote>
  
  <p>Well I argue differently and need your help. Take this piece of code for an example:</p>
  
  <pre><code>public static void main(String[] args)
  {
      // This line won't compile ("possible loss of precision"):
      byte line1 = returnByte();
  
      // compiles:
      int line2 = returnByte();
  
      // compiles too, we "accept" the risk of precision loss:
      byte line3 = (byte) returnByte();
  }
  
  public static int returnByte()
  {
      byte b = 1;
      return b;
  }
  </code></pre>
  
  <p>Obviously, the compiler do not complain about a different return type int in the <code>returnByte()</code> method signature and what we actually do return at the end of the method implementation: a byte. The byte uses fewer bits (8) than the int (32) and will be cast to an int without the risk of a precision loss. But the returned value <strong>is</strong> and will <strong>always be</strong> an integer! Or am I wrong? What would you have answered on the exam?</p>
  
  <p>I'm not totally sure what the actual return type is, since this statement in the book is said to be true. <em>Is the cast happening at the end of our method implementation or is the cast happening back in the main method just before assignment?</em></p>
  
  <p>In the real world, this question would not matter as long as one understand the implicit casting and the risk of losing precision. But since this is just one of those questions that might popup on the exam, I'd love to know the technically correct answer to the question.</p>
  
  <h2><em>Clarification!</em></h2>
  
  <p>The majority of the answers seem to think that I want to know whether one can cast a <code>byte</code> to an <code>int</code> and what happens then. Well, that is not the question. I'm asking what the returned <strong>type</strong> is. In other words, is the author's quoted statement <em>right or wrong</em>? Does the cast to an int happen <em>before or after</em> the returnByte() method actually returns? If this was the real exam and you would have got the question, what would you have answered?</p>
  
  <p>Please see line1 in my code snippet. If what the author say is right, that line would have compiled as the returned value would have been a byte. But it does not compile, the rules of type promotion says that we risk loosing precision if we try to squeeze an int into a byte. For me, that is a proof that the returned value is an integer.</p>