Sunday, April 5, 2020

Kotlin - Basic Types and Variables

Basic Types and Variables

Here I wills how you how Kotlin variables really work. So you will know about Kotlin's basic types such as Ints, Floats, and Booleans. I will also tell how to create arrays to hold multiple values and Finally you will discover why Objects are so important in Kotlin.

Your code needs a variable like

var x =5

So what is behind the scene ?.

So variables are like cup right. it can be small, Big based on our requirements but it will have one thing in common that it will hold the value.

So here if you say like x = 5 that means

1)What the variable name is.
2)whether or not the viable can be reused.
3)What type of variable it is.

You have to tell these things to the compiler.

So our compiler cares about the type of our variables. For this type safety to work, the compiler needs to know.

Let see how this works:

when you declare a variable using code like:

var x = 5 

the value you are assigning to the variable is used to create a new object And the compiler infers the variable's type from the object. So the compiler knows that you need a variable with a type of int so that it matches the type of object.

The variable holds a reference to the object when an object is assigned to a variable, the object itself doesn't go into the variable. A reference to the object goes into the variable instead.

val vs. var 

if you declare the variable using val, the reference to the object stays in the variable forever and can not be replaced but if you use the var keyword instead you can assign another value to the variable.

var x = 5
x = 6

So we can replace the reference held by the variable because its been declared using var but at the same time, it would not be possible if it declared the variable using val.

Bullet Points:


  • In java numbers are primitives so a variable holds the actual number. Is that not the case with Kotlin? No, it is not the case with Kotlin because in  Kotlin , numbers are objects and the variables hold a reference to the object, not the object itself.
  • Why does kotlin care so much about the variable's type? Because it makes your code safer and less prone to error.
  • In java, you can treat Char primitives as numbers. Can you do the same for Chars in kotlin.No Chars in Kotlin are characters not he numbers.
Some Examples:

fun main(args: Arrays<String>)
{
   var x : Int = 65.2  // this is not valid Int value
   var isPunk = true
   var message = 'Hellow'   // Single quotes are used to define Chars which holds single charecters.

    var y =7
    var bigNum: Long = y.toLong()
    var b: Byte = 2
  
 }

Now we see how to create an Array:

you can create an array using the arrayOf() function. for example:

fun main(args: Array<String>)
{
   val wordArray = arrayOf("24/7","Multi-Tier","B-To-B")
   var arraySize = wordArray.size
   
   val rand = (Math.random * arraySize).toInt  // This generates a random number returns a random number between 0 and 1 .We sue use Int() to force the result to be an integer.

   val phrase = "${wordArray[rand]}"
   println(phrase)
}

Note: Arrays hold items of a specific type.you can either let the compiler infer the type from the array's values or explicitly define the type using Array<Type>.

var vs val concept in Array :

So we need to understand the effect val and var have when you declare an array.
As you know a variable holds a reference to an object. when you declare a variable using var you can update the variable so that it holds a reference to a different instead.
If the variable holds a reference to an array this means that you can update the variable so that if it refers to a different array of the same type. for example:

var myArray = arrayOf(1,2,3)
myArray = arrayOf(4,5)

So let's understand what happens:

var myArray = arrayOf(1,2,3) This creates an array of Ints and a variable names myArray that holds a reference to it.

myArray = arrayOf(4,5) This creates a new array of Ints. A reference to the new array gets put into the myArray variable, replacing the previous references.

So what happens if we use the variable val instead?

val means the variable points to the same array forever. when you declare an array using val.you no longer update the variable so that it holds a reference to a different array. for example:

val myArray = arrayOf(1,2,3)
my Array = (4,5,6)

Once the viable is assigned an array. it holds a reference to that array forever but even though the variable maintains to the same array. the array itself update the variables in the array.

But there is catch you can still update the variables in the array.

Declaring a variable using val means that you can not reuse the variable for another object.you can however still update the object itself.

for example:

val myArray = arrayOf(1,2,3)
mayArray[2] = 6    // This updates the 3rd item in the array

Bullet Points:

  1. If the variable type is not explicitly defined the compiler infers it from its value.
  2. A variable holds a reference to an object
  3. An object has state and behavior. its behavior is exposed through its functions.
  4. Kotlin has a number of basics types: Byte,Short,Int,Long,Float,Double,boolean,Char and String.
  5. You can only assign a value to a variable that has a compatible type.
  6. If you define an array using val. you can still update the items in the array.
















No comments: