DEV Community

Cover image for Does Kotlin have primitive data types?
Khush Panchal
Khush Panchal

Posted on • Originally published at Medium

Does Kotlin have primitive data types?

In this blog I have came to the conclusion whether Kotlin actually have primitive data types or not?
Before jumping on the conclusion, let’s start with java:-

Java Representation

Primitive data types are the basic data types that are predefined in the language. There are 8 primitive data types in Java — boolean, byte, char, double, float, int, long, short

What if I want to make any primitive data type as nullable in java. Assigning null value to primitive data type gives compile time error. Every primitive data type have a wrapper class in java, so to make it nullable we use wrapper class instead.

int i = null; //error 
Integer i = null; //works fine
Enter fullscreen mode Exit fullscreen mode

Wrapper class is used to encapsulate primitive data types in java. This way we can use primitive data types as an object.

Wrapper class is useful in many ways, such as using reflection, data manipulation, enabling null values to primitive types, generic programming.

Wrapper class for each primitive data type in java is:
boolean → Boolean, byte → Byte, char → Character, double → Double, float → Float, int → Integer, long → Long, short → Short

But everything comes with some consequences such as using wrapper class increases the memory of the program and comparatively slower.

How does Kotlin optimize this?

Kotlin doesn’t have primitive types directly.

Kotlin uses classes like Boolean, Byte, Char, Double, Float, Int, Long, Short as an object wrapper for primitives.

But whenever Kotlin code is compiled into bytecode, it converts the above classes into “primitive type” whenever possible.

Let’s understand with the example:

class KotlinPrimitive { 
  private var a: Int = 1 //variable a is declared as non nullable
  private var b: Int? = 1 //variable b is declared as nullable 
}
Enter fullscreen mode Exit fullscreen mode

Decompiled version

public final class KotlinPrimitive { 
  private int a = 1; 
  private Integer b = 1; 
}
Enter fullscreen mode Exit fullscreen mode

When we decompiled the bytecode of the above code, we observed that Kotlin is converting the nullable data type into wrapper class (Integer), but when we declared the variable as non nullable it converts the variable directly into primitive data type.

Let’s see one more example:

class KotlinPrimitive { 
  fun funA() { 
    var c: Int? = 1 
    // do something 
  } 

  fun funB() { 
    var d: Int? = 1 
    // do something 
    d = null
  }
}
Enter fullscreen mode Exit fullscreen mode

Decompiled version

public final class KotlinPrimitive { 
  public final void funA() { 
    int c = 1; 
    //do something 
  }

  public final void funB() {
    Integer d = 1; 
    //do something
    d = null;
  } 
}
Enter fullscreen mode Exit fullscreen mode

As you can see in the above example, for the local function, Kotlin smartly checks for even nullable type that whether it is getting null in any point of time inside the function, if not, it converts that nullable type variable into primitive data type at bytecode level.

Conclusion

From above examples, we can see that Kotlin converts the non nullable data types into primitive data types.

Also for the local function it will convert the nullable data type into primitive data type when the nullable value is never assigned a null value.

So the conclusion is that Kotlin doesn’t have primitives directly but the data type in Kotlin converts to primitive data type at JVM level whenever possible.

Source code: Github

Contact Me:

LinkedIn, Twitter

Happy Coding ✌️

Top comments (0)