Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8024689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:55:59+00:00 2026-06-04T22:55:59+00:00

As a test, I wrote this code: object Ambig extends App { def f(

  • 0

As a test, I wrote this code:

object Ambig extends App {
  def f( x:Int    ) { println("Int"   ) }
  def f( x:String ) { println("String") }
  f( null.asInstanceOf[Int   ] )
  f( null.asInstanceOf[String] )
  f(null)
}

I was expecting to get an error on that last invocation of f(), saying that it was ambiguous. The compiler accepted it, and produced this output:

Int
String
String

Now I’m guessing that this has to do with the fact that Int is not an AnyRef, so the only version of f that works for f(null) is f(x:String). But then, if an Int can’t be null, what does null.asInstanceOf[Int] mean? The repl says it’s of type Int:

scala> :type null.asInstanceOf[Int]
Int

but I don’t really see how that works. After all, if I try to cast a String to an Int, all hell breaks loose:

scala> "foo".asInstanceOf[Int]
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
    at scala.runtime.BoxesRunTime.unboxToInt(Unknown Source)
        ...

Of course that’s to be expected — “foo” can’t be made into an Int. But neither can null, so why does casting null to an Int work? Presumably boxing in some form, but the type is still Int, which can’t be null…

What am I missing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T22:56:00+00:00Added an answer on June 4, 2026 at 10:56 pm

    The behaviour of casting null to an Int depends on the context in which it is done.

    First of all, if you cast a null to an Int, it actually means a boxed integer, whose value is null. If you put the expression in a context where the expected type is Any (which is translated to Object behind the scene, because in the JVM bytecode, there is no way to refer to a primitive type and a reference type with the same reference), then this value is not converted further – that is why println(null.asInstanceOf[Int]) prints null.

    However, if you use this same boxed integer value in a context where a primitive Int (Java int) is expected, it will be converted to a primitive, and null is (as a default value for reference types) converted to 0 (a default value for primitive types).

    If a generic method does this cast, then, naturally, you get a null back.

    However, if this method is specialized, then its return type is Int (which is a primitive integer in this case), so the null: Any value has to be converted to a primitive, as before.

    Hence, running:

    object Test extends App {
      println(null.asInstanceOf[Int])
    
      def printit(x: Int) = println(x)
    
      printit(null.asInstanceOf[Int])
    
      def nullint[T] = null.asInstanceOf[T]
    
      println(nullint[Int])
    
      def nullspecint[@specialized(Int) T] = null.asInstanceOf[T]
    
      println(nullspecint[Int])
    }
    

    produces:

    null
    0
    null
    0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote code like this: System.IO.File.ReadAllLines(c://test.txt) .Select(val => Console.WriteLine(val) ); And I'm getting a
I wrote a feature to test the default configuration of my web app using
I just wrote some code to test the behavior of std::equal, and came away
I am new to multithreading, and I wrote this code which prints the numbers
I wrote a test app that should never stop. It issues t.wait() ( t
LANGUAGE: I am writing an object-oriented code in MATLAB. I wrote almost all of
all. I wrote the following simple code to test scala.actors._: // java version 1.7.0_02
I wrote the following piece of code to test my understanding of virtual inheritance.
I wrote this code at the end of a long day: MyObject *thisObj; //
I can get an html code from web site this way: public void Test()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.