Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This question is almost a duplicate of: <a href="https://stackoverflow.com/questions/1545229/scala-cant-multiply-java-doubles">Scala can&#39;t multiply java Doubles?</a> - you can look at <a href="https://stackoverflow.com/questions/1545229/scala-cant-multiply-java-doubles/1545418#1545418">my answer</a> as well, as the idea is similar. </p> <p>As <a href="https://stackoverflow.com/users/172677/eastsun">Eastsun</a> already hinted, the answer is an <em>implicit conversion</em> from an <code>java.lang.Integer</code> (basically a boxed <code>int</code> primitive) to a <code>scala.Int</code>, which is the Scala way of representing JVM primitive integers.</p> <pre><code>implicit def javaToScalaInt(d: java.lang.Integer) = d.intValue </code></pre> <p>And interoperability has been achieved - the code snipped you've given should compile just fine! And code that uses <code>scala.Int</code> where <code>java.lang.Integer</code> is needed seems to work just fine due to autoboxing. So the following works:</p> <pre><code>def foo(d: java.lang.Integer) = println(d) val z: scala.Int = 1 foo(z) </code></pre> <p>Also, as <a href="https://stackoverflow.com/users/189058/michaelkebe">michaelkebe</a> said, do not use the <code>Integer</code> type - which is actually shorthand for <code>scala.Predef.Integer</code> as it is deprecated and most probably is going to be removed in Scala 2.8.</p> <p><strong>EDIT</strong>: Oops... forgot to answer the why. The error you get is probably that the <code>scala.Predef.Integer</code> tried to mimic Java's syntactic sugar where <code>a + "my String"</code> means string concatenation, <code>a</code> is an <code>int</code>. Therefore the <code>+</code> method in the <code>scala.Predef.Integer</code> type only does string concatenation (expecting a <code>String</code> type) and no natural integer addition. </p> <p>-- Flaviu Cipcigan</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. 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.
    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