Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you're sure that this object is an <code>Integer</code> :</p> <pre><code>int i = (Integer) object; </code></pre> <p>Or, starting from Java 7, you can equivalently write:</p> <pre><code>int i = (int) object; </code></pre> <p>Beware, it can throw a <a href="http://download.oracle.com/javase/6/docs/api/java/lang/ClassCastException.html" rel="noreferrer"><code>ClassCastException</code></a> if your object isn't an <a href="http://download.oracle.com/javase/6/docs/api/java/lang/Integer.html" rel="noreferrer"><code>Integer</code></a> and a <a href="http://download.oracle.com/javase/6/docs/api/java/lang/NullPointerException.html" rel="noreferrer"><code>NullPointerException</code></a> if your object is <code>null</code>.</p> <p>This way you assume that your Object is an Integer (the wrapped int) and you unbox it into an int.</p> <p><code>int</code> is a primitive so it can't be stored as an <code>Object</code>, the only way is to have an <code>int</code> considered/boxed as an <code>Integer</code> then stored as an <code>Object</code>.</p> <hr> <p>If your object is a <code>String</code>, then you can use the <a href="http://download.oracle.com/javase/6/docs/api/java/lang/Integer.html#valueOf(java.lang.String)" rel="noreferrer"><code>Integer.valueOf()</code></a> method to convert it into a simple int :</p> <pre><code>int i = Integer.valueOf((String) object); </code></pre> <p>It can throw a <a href="http://download.oracle.com/javase/6/docs/api/java/lang/NumberFormatException.html" rel="noreferrer"><code>NumberFormatException</code></a> if your object isn't really a <code>String</code> with an integer as content.</p> <hr> <p><strong>Resources :</strong></p> <ul> <li><a href="http://download.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html" rel="noreferrer">Oracle.com - Autoboxing</a></li> <li><a href="http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html" rel="noreferrer">Oracle.com - Primitive Data types</a></li> </ul> <p><strong>On the same topic :</strong></p> <ul> <li><a href="https://stackoverflow.com/questions/501653/java-whats-the-difference-between-autoboxing-and-casting">Java: What's the difference between autoboxing and casting?</a></li> <li><a href="https://stackoverflow.com/questions/766468/autoboxing-so-i-can-write-integer-i-0-instead-of-integer-i-new-integer0">Autoboxing: So I can write: Integer i = 0; instead of: Integer i = new Integer(0);</a></li> <li><a href="https://stackoverflow.com/questions/2565397/convert-object-into-primitive-int">Convert Object into primitive int</a></li> </ul>
    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. 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