what is the correct result of null.asInstanceOf[Int] ?

796 views
Skip to first unread message

Grzegorz Balcerek

unread,
Feb 8, 2011, 11:06:01 AM2/8/11
to scala...@googlegroups.com
Hello,
What is the correct result of the expression null.asInstanceOf[Int] ? Should it have any value? Which one?
The following two lines show different values (in scala 2.8.1).
 
scala> null.asInstanceOf[Int]
res0: Int = 0
 
scala> println(null.asInstanceOf[Int])
null
 
Are they both correct? If not which one (if any) is correct?
 
Regards,
Grzegorz Balcerek
 

Paul Phillips

unread,
Feb 8, 2011, 1:01:00 PM2/8/11
to Grzegorz Balcerek, scala...@googlegroups.com
On 2/8/11 8:06 AM, Grzegorz Balcerek wrote:
> Hello,
> What is the correct result of the expression null.asInstanceOf[Int] ?
> Should it have any value? Which one?

Normally I would point to the spec to say what the correct result should
be. However the spec seems to be in denial about this situation, still
holding to its position that it should throw an NPE. Although the last
time I looked at this we had a much different array representation so
I'm not sure all arguments still hold, at that time the NPE was not viable.

Present issues include at least how to deal with a polymorphic array
representation like ArraySeq. It is backed by a reference array but can
be parameterized on any type (that is the point) so:

scala> val xs = new collection.mutable.ArraySeq[Int](10)
xs: scala.collection.mutable.ArraySeq[Int] = ArraySeq(null, null, null,
null, null, null, null, null, null, null)

So what should this do? Well, it does the same thing you report.

scala> xs(0)
res7: Int = 0

scala> println(xs(0))
null

Take a look at this ticket for some other thoughts on the matter.

https://lampsvn.epfl.ch/trac/scala/ticket/2996
"2.8 collections: toString on ArraySeq of a primitive type reveals
underlying nulls"

Here is an even more interesting manifestation:

scala> new collection.mutable.ArraySeq[Float](5) mkString
res0: String = nullnullnullnullnull

scala> new collection.mutable.ArraySeq[Float](5) map (_.toString) mkString
res1: String = 0.00.00.00.00.0

Roland Kuhn

unread,
Feb 8, 2011, 1:06:33 PM2/8/11
to scala...@googlegroups.com
You answered your own question with that test: that expression is 0 unless you use it where it needs boxing, in which case it becomes null. That sounds about right to me (I'd be surprised if null becomes Integer(0) by casting).

scala> Integer.valueOf(null.asInstanceOf[Int])ďż˝
res41: java.lang.Integer = 0

scala> (null.asInstanceOf[Int]).asInstanceOf[AnyRef]ďż˝
res43: AnyRef = null

Regards,

Roland


Am 08.02.2011 17:06, schrieb Grzegorz Balcerek:
Hello,
What is the correct result of the expression null.asInstanceOf[Int] ? Should it have any value? Which one?
The following two lines show different values (in scala 2.8.1).
ďż˝
scala> null.asInstanceOf[Int]
res0: Int = 0
ďż˝
scala> println(null.asInstanceOf[Int])
null
ďż˝
Are they both correct? If not which one (if any) is correct?
ďż˝
Regards,
Grzegorz Balcerek
ďż˝

Grzegorz Balcerek

unread,
Feb 8, 2011, 3:50:59 PM2/8/11
to Paul Phillips, scala...@googlegroups.com
Thank you for the explanation.

I understand that there might be bugs or implementation issues in the code.
And yes, I am rather interested in trying to understand the spec and
figuring out from it what should the correct value be.

Actually, I do not find where the spec says that a NPE should be thrown.
I would rather expect CastClassException based on what is said in the last
paragraph in the section 12.2. But that is a minor point.

In any case, I would not expect any value to be returned (both 0 and null
seem incorrect to me) because null is of type scala.Null and based on figure
12.1 in the spec (Class hierarchy of Scala) I do not see how the type system
could allow casting null to any of AnyVal subtypes.
So what I would expect is a compile error or at least some kind of exception
but not a value being returned.
Am I correct?

Regards,
Grzegorz Balcerek

Paul Phillips

unread,
Feb 8, 2011, 3:54:14 PM2/8/11
to Grzegorz Balcerek, scala...@googlegroups.com
On 2/8/11 12:50 PM, Grzegorz Balcerek wrote:

> Actually, I do not find where the spec says that a NPE should be thrown.
> I would rather expect CastClassException based on what is said in the
> last paragraph in the section 12.2. But that is a minor point.

6.3 The Null Value

...
• asInstanceOf[T ] returns the “null” object itself if T conforms to
scala.AnyRef, and throws a NullPointerException otherwise.

> In any case, I would not expect any value to be returned (both 0 and
> null seem incorrect to me) because null is of type scala.Null and based
> on figure 12.1 in the spec (Class hierarchy of Scala) I do not see how
> the type system could allow casting null to any of AnyVal subtypes.

Well, that's why the spec says it throws an NPE.

Grzegorz Balcerek

unread,
Feb 9, 2011, 6:11:52 AM2/9/11
to Paul Phillips, scala...@googlegroups.com
Yes, thank you. 6.3 explains it. I found it after I wrote the mail.
Sorry for unnecessary traffic.
Grzegorz

----- Original Message -----
From: "Paul Phillips" <pa...@improving.org>
To: "Grzegorz Balcerek" <gbal...@echostar.pl>
Cc: <scala...@googlegroups.com>
Sent: Tuesday, February 08, 2011 9:54 PM
Subject: Re: [scala-user] what is the correct result of
null.asInstanceOf[Int] ?
Reply all
Reply to author
Forward
0 new messages