<p>If you declare variables of type byte or short and attempt to perform arithmetic operations on these, you receive the error "Type mismatch: cannot convert int to short" (or correspondingly "Type mismatch: cannot convert int to byte"). </p>

<pre><code>byte a = 23;
byte b = 34;
byte c = a + b;
</code></pre>

<p>In this example, the compile error is on the third line.</p>

[{'answer_id': 81394, 'author': 'Brad Richards', 'author_id': 7732, 'author_profile': 'https://Stackoverflow.com/users/7732', 'pm_score': 5, 'selected': True, 'text': '<p>Although the arithmetic operators are defined to operate on any numeric type, according the Java language specification (5.6.2 Binary Numeric Promotion), operands of type byte and short are automatically promoted to int before being handed to the operators.</p>\n\n<p>To perform arithmetic operations on variables of type byte or short, you must enclose the expression in parentheses (inside of which operations will be carried out as type int), and then cast the result back to the desired type.</p>\n\n<pre>byte a = 23;\nbyte b = 34;\nbyte c = (byte) (a + b);</pre>\n\n<p>Here\'s a follow-on question to the real Java gurus: why? The types byte and short are perfectly fine numeric types. Why does Java not allow direct arithmetic operations on these types? (The answer is not "loss of precision", as there is no apparent reason to convert to int in the first place.)</p>\n\n<p>Update: jrudolph suggests that this behavior is based on the operations available in the JVM, specifically, that only full- and double-word operators are implemented. Hence, to operator on bytes and shorts, they must be converted to int.</p>\n'}, {'answer_id': 81446, 'author': 'David Sykes', 'author_id': 3154, 'author_profile': 'https://Stackoverflow.com/users/3154', 'pm_score': 3, 'selected': False, 'text': '<p>The answer to your follow-up question is here: </p>\n\n<blockquote>\n  <p>operands of type byte and short are automatically promoted to int before being handed to the operators</p>\n</blockquote>\n\n<p>So, in your example, <code>a</code> and <code>b</code> are both converted to an <code>int</code> before being handed to the + operator. The result of adding two <code>int</code>s together is also an <code>int</code>. Trying to then assign that <code>int</code> to a <code>byte</code> value causes the error because there is a potential loss of precision. By explicitly casting the result you are telling the compiler "I know what I am doing".</p>\n'}, {'answer_id': 81490, 'author': 'jrudolph', 'author_id': 7647, 'author_profile': 'https://Stackoverflow.com/users/7647', 'pm_score': 2, 'selected': False, 'text': "<p>I think, the matter is, that the JVM supports only two types of stack values: word sized and double word sized. </p>\n\n<p>Then they probably decided that they would need only one operation that works on word sized integers on the stack. So there's only iadd, imul and so on at bytecode level (and no operators for bytes and shorts).</p>\n\n<p>So you get an int value as the result of these operations which Java can't safely convert back to the smaller byte and short data types. So they force you to cast to narrow the value back down to byte/short.</p>\n\n<p>But in the end you are right: This behaviour is not consistent to the behaviour of ints, for example. You can without problem add two ints and get no error if the result overflows.</p>\n"}, {'answer_id': 81728, 'author': 'Tom Hawtin - tackline', 'author_id': 4725, 'author_profile': 'https://Stackoverflow.com/users/4725', 'pm_score': 1, 'selected': False, 'text': "<p>The Java language always promotes arguments of arithmetic operators to int, long, float or double. So take the expression:</p>\n\n<pre><code>a + b\n</code></pre>\n\n<p>where a and b are of type byte. This is shorthand for:</p>\n\n<pre><code>(int)a + (int)b\n</code></pre>\n\n<p>This expression is of type int. It clearly makes sense to give an error when assigning an int value to a byte variable.</p>\n\n<p>Why would the language be defined in this way? Suppose a was 60 and b was 70, then a+b is -126 - integer overflow. As part of a more complicated expression that was expected to result in an int, this may become a difficult bug. Restrict use of byte and short to array storage, constants for file formats/network protocols and puzzlers.</p>\n\n<p>There is an interesting recording from JavaPolis 2007. James Gosling is giving an example about how complicated unsigned arithmetic is (and why it isn't in Java). Josh Bloch points out that his example gives the wrong example under normal signed arithmetic too. For understandable arithmetic, we need arbitrary precision.</p>\n"}, {'answer_id': 73372404, 'author': 'Dean', 'author_id': 8749628, 'author_profile': 'https://Stackoverflow.com/users/8749628', 'pm_score': 0, 'selected': False, 'text': '<p>In Java Language Specification (5.6.2 Binary Numeric Promotion):</p>\n<blockquote>\n<p>1 If any expression is of type double, then the promoted type is double, and other expressions that are not of type double undergo widening primitive conversion to double.</p>\n<p>2 Otherwise, if any expression is of type float, then the promoted type is float, and other expressions that are not of type float undergo widening primitive conversion to float.</p>\n<p>3 Otherwise, if any expression is of type long, then the promoted type is long, and other expressions that are not of type long undergo widening primitive conversion to long.</p>\n<p>4 Otherwise, none of the expressions are of type double, float, or long. In this case, the promoted type is int, and any expressions that are not of type int undergo widening primitive conversion to int.</p>\n</blockquote>\n<p>Your code belongs to case 4. variables <code>a</code> and <code>b</code> are both converted to an <code>int</code> before being handed to the <code>+</code> operator. The result of <code>+</code> operation is also of type <code>int</code> not <code>byte</code></p>\n'}]
['https://Stackoverflow.com/questions/81392', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/7732/']