Is `Unit` the equivalent of Java's `Void` rather t...
# getting-started
f
Is
Unit
the equivalent of Java's
Void
rather than lowercase
void
?
s
It serves as both
k
Kind of, Java
Void
doesn't have an instance but it allows
null
so that's kind of the single instance.
r
Technically,
Void
id equivalent to Kotlin's
Nothing?
l
I think it’s the equivalent for kotlin’s
Void?
a function which return
Nothing
can’t return, only throws or keep working
r
Kotlin doesn't have
Void
, that's from java.
Nothing
can't return, but
Nothing?
can return only
null
, just like Java's
Void
.
l
Sorry, meant
Unit?
r
Unit?
can return
Unit
or
null
l
Unit
is mapped to
Void
in Java
r
No,
Unit
is mapped to
void
(lowercase) for return type in Java, but stays as
Unit
in other circumstances, such as generics.
l
Yep, and what
Unit?
is mapped to?
r
It will stay
Unit?
l
into Java code
r
Yes
l
there’s not
Unit?
in Java…..
r
Sorry, It would just be
Unit
, but with Java;s implied nullability.
l
Got it 🙂
Thanks
r
You can try it:
Copy code
// Test.kt
fun test(): Unit? {
    return null
}

// Test.java
TestKt.test();  // returns kotlin.Unit
l
Thanks 🙂
👍 1