Home > Scala > Null, null, Nil, Nothing, None, and Unit in Scala

Null, null, Nil, Nothing, None, and Unit in Scala

Null– Its a Trait.
null– Its an instance of Null- Similar to Java null.

Nil– Represents an emptry List of anything of zero length. Its not that it refers to nothing but it refers to List which has no contents.

Nothing is a Trait. Its a subtype of everything. But not superclass of anything. There are no instances of Nothing.

None– Used to represent a sensible return value. Just to avoid null pointer exception. Option has exactly 2 subclasses- Some and None. None signifies no result from the method.

Unit– Type of method that doesn’t return a value of anys sort.

Note: Any is supertype of AnyRef and AnyVal. AnyRef is the supertype of all the reference classes (like String, List, Iterable) in scala. AnyVal is the supertype of all the value classes (like Int, Float, Double, Byte, Short..). Null is a subtype of all the reference classes. null is its only instance. Nothing is subtype of every other type i.e of reference and value classes.

Think- AnyRef == Object in Java.

The above is a breif summary of a wonderful post by Matt Malone where he has explained all these concepts in depth with examples. Read the blog post here.

Categories: Scala Tags: , , , , , , ,
  1. Matt
    July 23rd, 2009 at 20:19 | #1

    Glad you enjoyed the article. Thanks for the shout-out.

    • i82much
      July 3rd, 2010 at 03:15 | #2

      Thanks for this. I got my Nil and null mixed up and caused some confusion with why I couldn’t instantiate some objects as Nil.

  2. egg
    July 22nd, 2011 at 00:37 | #3

    fun chatting about nothing. Don’t forget the object _scala.xml.Null_. I was stuck for some time, mistaking it for null. Its used in scala.xml.MetaData which in turn is used for xml attributes.

    Scala is fun!

  3. DS
    October 31st, 2011 at 20:58 | #4

    You have written something that is someway wrong :
    “Unit- Type of method that doesn’t return a value of anys sort.”
    Unit is used like void, but is slightly different. As in PF all functions MUST return a value. Unit is a TYPE (sub class of AnyVal) that has only one value : (), and () is the (useless) value that is returned by functions of type Unit. 😉

    • October 31st, 2011 at 21:52 | #5

      It was in a way conveying the same information. I didnt wan’t to explain more as I was trying to link a well explained article.

      Thanks a lot for bringing this up. It makes sense to add some more information

  4. August 26th, 2013 at 17:06 | #6

    Here is a link to some comments and clarifications on twitter related to this post https://twitter.com/itemstate/status/371957550012317696

    • August 26th, 2013 at 19:14 | #7

      Thanks for sharing and also pointing me to the clarification. I have replied to the tweet. I will look into this and update the post as the content is not my original one, instead a summary of the blog post linked towards the end of the post.

    • August 26th, 2013 at 19:41 | #8

      Clarification for the tweet mentioned in your comment:

      1. From the documentation of Unit:

      /** `Unit` is a subtype of [[scala.AnyVal]]. There is only one value of type
      * `Unit`, `()`, and it is not represented by any object in the underlying
      * runtime system. A method with return type `Unit` is analogous to a Java
      * method which is declared `void`.
      */

      From what I stated about Unit: It says: return type of the Method that doesn’t return value of any sorts. The intention here is to state that there is no value backed/contained with in it.

      2. This post doesn’t establish any context about how these types are used is because this post is a summary of an interesting post I found (which I have also linked towards the end of my post) where in there are examples which show how these types are used.

  5. Arleigh Dickerson
    June 14th, 2014 at 21:15 | #9

    Thanks

  1. July 29th, 2009 at 21:05 | #1