What is Unit in Scala? What is the difference between Java's void and Scala's Unit?
In Scala, Unit is used to represent “No value
” or “No Useful value
”. Unit is a final class defined in “scala
” package that is “scala.Unit
”. Unit is something similar to Java’s void. But they have few differences.
* Java’s void does not any value. It is nothing.
* Scala’s Unit has one value ()
* ()
is the one and only value of type Unit in Scala. However, there are no values of type void in Java.
* Java’s void is a keyword. Scala’s Unit is a final class.
Both are used to represent a method or function is not returning anything.