X
    Categories: Kotlin

Android Kotlin Tutorials on Variables

 

Kotlin Variables :

Kotlin has the same variable types we use in programming but the way we initialize them is a bit different from normal i.e., traditional way of declaring in java.

Let’s have a look at the continuation of the previous tutorial on android Kotlin variables. We have learnt about String, Integer declarations previously now will look at more kotlin variables in this tutorial. May have a look at the previous tutorial

https://androidcoding.in/2019/01/24/android-kotlin-variables-declaration-kotlin-tutorial-2/

 

 

Double Variable :

Double variable is represented with 64 bits. This Kotlin variable is much more precisely used for representing the accurate amount in terms of decimal values in necessary scenarios.

When we want to show the marks of the person we say 56 or 73 …

But when we want to show the average of all the marks then we will get a decimal value for sure we cannot represent is in Integer format as it gets round up excluding after decimal values.

java:

Double percentage;

 

Kotlin:

var percentage: Double

 

Float Variable :

Float variable is represented with 32 bits. And yes its a light weight format of representing decimal values compared to double.

They same implementation as of double but double has more precision than float which means an accurate answer including more decimal values can be observed using double.

 

java:

Float percentage;

 

Kotlin

var percentage : Float

 

Double Or Float ?

After going through the tutorial on Kotlin variables you may get this doubt for sure, both double and float have their own importance.

Sometimes you may not require much decimal values and may be satisfied which less decimal representation the you may go for float.

-> The main advantage of using float is it’s memory usage floats can represent decimal values up to 7 digits of precision.

-> The main advantage of double is it’s precision it can represent 16 digits of precision.

 

So the conclusion is if you have a memory constraints / limits or you need a faster processing then go for float else you need precision i.e., accurate results then go for double.

 

For more details visit this video tutorial on Kotlin variables.

If you have any query’s on this tutorial on Kotlin variables do let us know in the comment section below.If you like this tutorial do like and share us for more interesting tutorials.

 

abhishek:
Related Post

This website uses cookies.