What is Unit Object in Kotlin

Viewed 1984

Right now I am learning Kotlin, in this guide I run into these two methods, one withUnit and another one doesn't

Method withunit

fun printSum(a: Int, b: Int): Unit {
  println("sum of $a and $b is ${a + b}")
}

Method without Unit

fun printSum(a: Int, b: Int) {
  println("sum of $a and $b is ${a + b}")
}

My question is, what is Unit do actually? is it something like void in Java? if it's like void why one of the method above doesn't have Unit run just fine.

which one should I use when I want my method returning nothing? And also when the best time to use Unit.

anyone can explain? cause it's really confusing me. Thanks in advance.

1 Answers
Related