Hi, Today I was checking Datatypes in Kotlin, and as always I took some notes and just wanted to share them with you guys.

Before we get into what data types are available in Kotlin, let’s get one thing out of the way, everything is a class. There are no lower case data types as there are in Java, but apart from that, you’re already familiar with the built-in data types available in Kotlin, because they’re the same as in Java.

Primitive data types

Int

Int is the default data type for whole numbers, just as it is in Java. So giving a whole number as a value will be equal to telling the compiler that this is an Int.

Important Notice

Kotlin does not automatically widen numbers. Now, what do I mean by that? Well in Java, we can do this:

int myInt = 10;
long myLong  = myint;

that works in Java because myInt can fit into a long variable, so Java just automatically converts this to a long when it assigns value. In other words, Java automatically widens the integer, but Kotlin doesn’t do that.  Let’s try the same thing in Kotlin.

myLong = myInt

you’ll see we have an error, and it says type mismatch requiring a long and it found an int.

Now, because of that, Java has the same methods but they’re even more important in Kotlin now, every datatype has two functions that convert to the other data types. So in order to fix this error, we just have to call the toLong method.

myLong = myInt.toLong()

Byte, Short

Let’s start by creating a byte variable, but to do so we have to specify the datatype, otherwise, the compiler will assign it to an Int.

val myByte: Byte = 111

if we also created a Short variable, we will be facing the same problem we faced with double and int, we cant do this

var myShort: Short
    myShort = myByte

we will get the same error and we have to go with the method toShort,

myShort = myByte.toShort()

Double, Float, Long

as in Java, double is the default datatype, so you don’t have to specify a variable declaration.

var myDouble = 22.1577

also just like java, putting an ‘L’ after the value will make it long, same goes for float ‘f’.

var myLong = 22L
var myfloat = 22f

this is equal to

var myLong : Long  = 22
var myfloat: Float = 22

again we can’t do this

myDouble = myFloat

we have to use the  toDouble method

myDouble = myFloat.toDouble()

Char

now let’s take a look at the char data type, so we can say

val char = 'b'

now an important note here is that in Java we can do this

char myChar = 65;

which will assign the letter A  which equals to 65 in the character set that ASCII, but in kotlin this won’t work!

So if we did this

val myChar : Char = 65

we will get the error we were getting in our previous examples, type mismatch.

To solve this we have to assign 65 to an Int and convert it to a char using the toChar method.

val myCharInt = 65
    println(myCharInt.toChar())

which will print the character A

Boolean

I think you know this after all these examples but let me show it also

val myBoolean = true

 

Interoperability with Java

Now one of the most features of Kotlin is that it’s interoperable with Java, which means that you can call Java methods from Kotlin without having to do anything special, but what happens if you want to call a Java method and it wants a primitive type as a parameter, like let’s say it wants a boolean parameter but it wants the primitive boolean type? It doesn’t want the Boolean class. Well let’s try it out, I will create a Java test class and put it in the file TestClass.java

public class TestClass{

    public String bestLanguage(boolean isJava) {
        return isJava ? "I Love Java!" : "I Love Kotlin!";
    }

}

and notice how the method is expecting a primitive type, now back to the kotlin file,

val isJavaLanguage= true
val lemmeGuess= TestClass().bestLanguage(isJavaLanguage)
println(lemmeGuess)

which will print out “I Love Java!”.

but wait, how did it work without having to do anything special ??

because when it comes to the primitive classes, the Kotlin classes actually compile to the primitive types under the covers.

Now how about if the Java method wanted boolean big B boolean, would that work?

YES!, and that’s because of course, Java has its boxing and unboxing, so that all works just the way it does in Java where there are boxing and unboxing that goes on for you.

The any class

The root of the Kotlin class hierarchy. Every Kotlin class has Any as a superclass.

val anything: Any

we also have the nothing class and there’s no equivalent for this in Java. This is going to sound strange, but nothing is a subclass of every class, which means that you can use the nothing class wherever you can use any other class.

so why would you do this?

Well, one common use case would be when you know a function is never going to return, like for example if a function intentionally contains an infinite loop, you can tell the compiler that you don’t expect this function ever to return by having the function return type nothing , so that’s one use case for it.

The unit class

The type with only one value: the Unit object. This type corresponds to the void type in Java.

void isn’t really a datatype, it’s a way of telling the compiler that a method doesn’t return anything.

So, if you have a function that doesn’t return anything, then you can declare it as returning type unit, but unit actually exists in Kotlin, it’s not just you know a way of telling the compiler that nothing gets returned.

It’s a singleton, so if a function returns unit, it will actually return the singleton unit instance and this is an important difference between Kotlin and Java.

In Java a void method truly doesn’t return anything, but in Kotlin a function that returns unit, actually returns the singleton unit instance.