What is the differences between Int and Integer in Scala?

ScalaTypesIntegerInt

Scala Problem Overview


I was working with a variable that I had declared as an Integer and discovered that > is not a member of Integer. Here's a simple example:

scala> i
warning: there were deprecation warnings; re-run with -deprecation for details
res28: Integer = 3

scala> i > 3
<console>:6: error: value > is not a member of Integer
       i > 3
         ^

Compare that to an Int:

scala> j
res30: Int = 3

scala> j > 3
res31: Boolean = false

What are the differences between Integer and Int? I see the deprecation warning but it's unclear to me why it was deprecated and, given that it has been, why it doesn't have a > method.

Scala Solutions


Solution 1 - Scala

"What are the differences between Integer and Int?"

Integer is just an alias for java.lang.Integer. Int is the Scala integer with the extra capabilities.

Looking in Predef.scala you can see this the alias:

 /** @deprecated use <code>java.lang.Integer</code> instead */
  @deprecated type Integer = java.lang.Integer

However, there is an implicit conversion from Int to java.lang.Integer if you need it, meaning that you can use Int in methods that take an Integer.

As to why it is deprecated, I can only presume it was to avoid any confusion over which kind of integer you were working with.

Solution 2 - Scala

Integer gets imported from java.lang.Integer and is only for compatibility with Java. Since it is a Java class, of course it can't have a method called "<". EDIT: You can mitigate this problem by declaring an implicit conversion from Integer to Int.

 implicit def toInt(in:Integer) = in.intValue()

You'll still get deprecation warning though.

Solution 3 - Scala

I think the problem you're seeing has has to do boxing/unboxing of value types and the use of the Java class Integer.

I think the answer is here: Boxing and unboxing in Scala. There is no implict unboxing in Scala. You've defined i as the Java class Integer but in the i > 3, the 3 is being treated and an int.

Solution 4 - Scala

Integer is a Java class, java.lang.Integer. It's different from Java's primitive type int, which is not a class. It can't have < defined, because Java does not allow operators to be defined for classes.

Now, you might wonder why such a type exist at all? Well, primitive types cannot be passed as references, so you can't pass an int to a method expecting java.lang.Object, equivalent to Scala's AnyRef, for example. To do that, you put that int inside an Integer object, and then pass the Integer.

Solution 5 - Scala

Integer gets imported from java.lang.Integer and is only for compatibility with Java. Since it is a Java class, of course it can't have a method called "<".

EDIT: You can mitigate this problem by declaring an implicit conversion from Integer to Int.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionpr1001View Question on Stackoverflow
Solution 1 - ScalaRichard DallawayView Answer on Stackoverflow
Solution 2 - ScalaKim StebelView Answer on Stackoverflow
Solution 3 - ScalaValenteinView Answer on Stackoverflow
Solution 4 - ScalaDaniel C. SobralView Answer on Stackoverflow
Solution 5 - ScaladeepakView Answer on Stackoverflow