Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>I don't know why it is referring to the long data type as an int</p> </blockquote> <p>It is not. You should learn to trust compiler messages (especially when they are from sane, modern compilers and not ancient C/C++ compilers). While the language that they speak might be hard to decipher at times, they are not usually lying to you.</p> <p>Let's look at it again:</p> <blockquote> <p>The literal of int 9223372036854775807 is out of range.</p> </blockquote> <p>Note, that it <strong>doesn't</strong> mention your variable <code>testLong</code> or the type <code>long</code> anywhere, so it's <strong>not</strong> about the initialization. The problem seems to occur at some other point.</p> <p>Now lets investigate some of the parts of the message:</p> <ul> <li><code>int</code> tells us that he wants to treat something as an <code>int</code> value (which is not what you wanted!)</li> <li>"out of range" is pretty clear: something is not within the expected range (probably that of <code>int</code>)</li> <li>"The literal": now that's interesting: what is a literal?</li> </ul> <p>I'll leave the cozy list to talk about literals for a moment: literals are places where you have some value in your code. There are <code>String</code> literals, <code>int</code> literals, <code>class</code> literals and so on. Every time you mention a <em>value</em> explicitly in your code, it's a literal.</p> <p>So it's not actually nagging you about the variable declaration, but the number itself, the value is what it's nagging you about.</p> <p>You can easily verify this by using the same literal in a context where a <code>long</code> and an <code>int</code> are equally acceptable:</p> <pre><code>System.out.println(9223372036854775807); </code></pre> <p><code>PrintStream.println</code> can take either <a href="http://download.oracle.com/javase/7/docs/api/java/io/PrintStream.html#println%28int%29" rel="noreferrer">an <code>int</code></a> or <a href="http://download.oracle.com/javase/7/docs/api/java/io/PrintStream.html#println%28long%29" rel="noreferrer">a <code>long</code></a> (or pretty much anything else). So that code should be fine, right?</p> <p>No. Well, maybe it <em>should</em> be, but according to the rules it <strong>is</strong> not fine.</p> <p>The problem is <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.1" rel="noreferrer">that "some digits" is defined to be an <code>int</code> literal</a> and therefore <strong>must</strong> be in the range defined by <code>int</code>.</p> <p>If you want to write a <code>long</code> literal, then you must make that explicit by appending the <code>L</code> (or lower case <code>l</code>, but I <strong>highly</strong> suggest you always use the upper-case variant, because it's much easier to read and harder to mistake for a <code>1</code>).</p> <p>Note that a similar problem occurs with <code>float</code> (postfix <code>F</code>/<code>f</code>) and <code>double</code> (postfix <code>D</code>/<code>d</code>).</p> <p>Side note: you'll realize that there are no <code>byte</code> or <code>short</code> literals and you can still assign values (usually <code>int</code> literals) to <code>byte</code> and <code>short</code> variables: that's possible due to special rules in <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.2" rel="noreferrer">§ 5.2 about Assignment Converson</a>: they allow assignment of constant expressions of a larger type to <code>byte</code>, <code>short</code>, <code>char</code> or <code>int</code> <em>if</em> the values are within the types range.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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