top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the difference between Java’s void and Scala’s Unit?

+1 vote
952 views

What is Unit in Scala? What is the difference between Java’s void and Scala’s Unit?

posted Oct 7, 2016 by Joy Nelson

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

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.

answer Oct 20, 2016 by Karthick.c
Similar Questions
+1 vote

What is REPL in Scala? What is the use of Scala’s REPL? How to access Scala REPL from CMD Prompt?

0 votes

What is the equivalent construct of Scala’s Option in Java SE 8? What is the use of Option in Scala?

...