Nothing
Nothing is a subclass of all types, and is also a subclass of NULL. Nothing has objects, but can be used to define types. For example, if a method throws an exception, the return value type of the exception is nothing (although it is not returned).
def get(index:Int):Int = { if0thrownew Exception(...) else ....}
If the statement is an expression with a return value, there must be a return value type, if x < 0, throws an exception, the return value of type nothing,nothing is also a subclass of int, so the return type of the IF expression is the return value type of the Int,get method is also int.
Null
Null is a subclass of all Anyref, in Scala's type system, Anyref is a subclass of any, and the any subclass has anyval. All types that correspond to Java value types are subclasses of Anyval. So null can be assigned to all reference types (ANYREF) and cannot be assigned to value types, and the semantics of this Java are the same. Null is the only object that is null.
None
None is an object, which is the subtype of option and is defined as follows:
caseobject None extends Option[Nothing] { deftrue defthrownew NoSuchElementException("None.get")}
Scala recommends using Option[x] as the return type in a method that may return empty. Returns SOME[X] If there is a value (some is also the subclass of option), otherwise none is returned, for example:
def get(key: A): Option[B] = { if (contains(key)) Some(getValue(key)) else None}
After you have the option, you can use get to get the included value, or use Getorelse to get the default value if IsEmpty is true.
Nil
Nil is an empty list, defined as list[nothing], according to the list definition List[+a], all nil is a subclass of all List[t].
reprint Please indicate the author Jason Ding and its provenance
Gitcafe Blog Home page (http://jasonding1354.gitcafe.io/)
GitHub Blog Home page (http://jasonding1354.github.io/)
CSDN Blog (http://blog.csdn.net/jasonding1354)
Jane Book homepage (http://www.jianshu.com/users/2bd9b48f6ea8/latest_articles)
Google search jasonding1354 go to my blog homepage
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Scala" in Scala, Nothing,null,none,nil