DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป is a community of 966,155 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for Variables in kotlin
Kluivert
Kluivert

Posted on

Variables in kotlin

Welcome back everyone to another brief learning of kotlin and today we will be talking about variables.

TADA!!!!

Alt Text

Now let's start, first i wanna believe you aren't a newbie in programming but if you are, no problemo.

Alright you'll need Inteli J Idea installed on your computer for starts, don't forget java jdk as well. Here are the respective links to get them.

https://www.jetbrains.com/idea/download/index.html#section=linux

https://www.oracle.com/technetwork/java/javase/downloads/jdk12-downloads-5295953.html

If you already had them, great let's begin right away.

So kotlin has data types, what are data types you ask?
consider them as containers carrying different information/data.

How exactly?

Say we different cups lined up, those cups are the containers but what they have inside is what makes them different. We use these different data in building cool stuffs and passing information to our computer.

Examples of data types are:

1.Int = Integers

  1. String = Bunch of characters such as texts or words.
  2. Double = Decimal digits
  3. Float = Decimal digits too but smaller than double

As the name implies, a double has 2x the precision of float[1]. In general a double has 15 decimal digits of precision, while float has 7.

Here's how the number of digits are calculated:

double has 52 mantissa bits + 1 hidden bit: log(253)รทlog(10) = 15.95 digits

float has 23 mantissa bits + 1 hidden bit: log(224)รทlog(10) = 7.22 digits

This precision loss could lead to truncation errors much easier to float up

  1. Char = characters of the 26 alphabets
  2. Boolean = True or false

Next: How to define variables in kotlin

Alt Text

So open your Inteli J idea and create your kotlin project.

You declare variables in two ways in kotlin.

Using "var" or "val"

Difference:
var means you can assign a different value different from the initial value to the variable whenever you want.

example:

//initial value
var kot:Int = 8

//new value

kot = 10

println(kot)

Output = 10

Using val

val kot:Int = 8

kot = 6

(Editor throws error "kot cannot be assigned a value").

That's for today. I'm sorry guys but i promise more posts.
Feel free to ask your questions as well.

Here's how you'd define variables of different data types

val a:Double = 4.8d
val b:Float = 5.0
val c:Char = 'g'
val d:Strinfg = "Hello"
val e:Boolean = true

Thank you

Top comments (0)

๐ŸŒš Friends don't let friends browse without dark mode.

Sorry, it's true.