Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As explained in <a href="http://msdn.microsoft.com/en-us/library/ybs77ex4(VS.71).aspx" rel="noreferrer">short C#</a> (but also for other language compilers as well, like Java)</p> <p>There is a predefined implicit conversion from short to int, long, float, double, or decimal.</p> <p>You cannot implicitly convert nonliteral numeric types of larger storage size to short (see Integral Types Table for the storage sizes of integral types). Consider, for example, the following two short variables x and y:</p> <pre><code>short x = 5, y = 12; </code></pre> <p>The following assignment statement will produce a compilation error, <strong>because the arithmetic expression on the right-hand side of the assignment operator evaluates to int by default.</strong></p> <pre><code>short z = x + y; // Error: no conversion from int to short </code></pre> <p>To fix this problem, use a cast:</p> <pre><code>short z = (short)(x + y); // OK: explicit conversion </code></pre> <p>It is possible though to use the following statements, where the destination variable has the same storage size or a larger storage size:</p> <pre><code>int m = x + y; long n = x + y; </code></pre> <hr> <p>A good follow-up question is:</p> <p>"why arithmetic expression on the right-hand side of the assignment operator evaluates to int by default" ?</p> <p>A first answer can be found in:</p> <p><a href="http://www.jblech.de/Glesner-Blech-COCV-2003.pdf" rel="noreferrer">Classifying and Formally Verifying Integer Constant Folding</a></p> <blockquote> <p>The <strong>Java language specification defines exactly how integer numbers are represented and how integer arithmetic expressions are to be evaluated</strong>. This is an important property of Java as this programming language has been designed to be used in distributed applications on the Internet. <strong>A Java program is required to produce the same result independently of the target machine executing it</strong>. </p> <p>In contrast, C (and the majority of widely-used imperative and object-oriented programming languages) is more sloppy and leaves many important characteristics open. The intention behind this inaccurate language specification is clear. The same C programs are supposed to run on a 16-bit, 32-bit, or even 64-bit architecture by instantiating the integer arithmetics of the source programs with the arithmetic operations built-in in the target processor. This leads to much more efficient code because it can use the available machine operations directly. As long as the integer computations deal only with numbers being “sufficiently small”, no inconsistencies will arise. </p> <p>In this sense, the C integer arithmetic is a placeholder which is not defined exactly by the programming language specification but is only completely instantiated by determining the target machine. </p> <p>Java precisely defines how integers are represented and how integer arithmetic is to be computed. </p> </blockquote> <pre><code> Java Integers -------------------------- Signed | Unsigned -------------------------- long (64-bit) | int (32-bit) | short (16-bit) | char (16-bit) byte (8-bit) | </code></pre> <blockquote> <p>Char is the only unsigned integer type. Its values represent Unicode characters, from <code>\u0000</code> to <code>\uffff</code>, i.e. from 0 to 2<sup>16</sup>−1. </p> <p><strong>If an integer operator has an operand of type long, then the other operand is also converted to type long. Otherwise the operation is performed on operands of type int, if necessary shorter operands are converted into int</strong>. The conversion rules are exactly specified.</p> </blockquote> <p>[From Electronic Notes in Theoretical Computer Science 82 No. 2 (2003)<br> Blesner-Blech-COCV 2003: <a href="http://pes.cs.tu-berlin.de/pes/index.php.html" rel="noreferrer">Sabine GLESNER</a>, Jan Olaf BLECH,<br> Fakultät für Informatik,<br> Universität Karlsruhe<br> Karlsruhe, Germany]</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload