Achievement 4 by @Odogwu001 Task : Applying Markdowns

in hive-172186 •  3 years ago  (edited)

After going through task 4 which is about formatting our content nicely for easy reading, I have been laughing at myself because I never knew most of the markups actually works on this platform, most times I have to just post my work un organize because I do not know all of these tags work here.
I am happy I found them today and from now on, I can now format my work to my taste.

today I shall be writing a short piece to show depict my understanding of these markups.

Different Between Var, Val and const in kotlin

kotlin-img.png

Image Source

In kotlin programming language which is google most preferred language for Android Development, there are various keywords used to declare variables and so young developers like myself sometimes mistake one for other.
my intention in this short piece is to make you understand the difference in each of these Keywords and also when best to use each of them.

Var

(1.) var name = "peter"

(2.) var name: String = "peter"

var is basically used to name declare a mutable variable, that is variable that can be changed or that can be reassigned in the course of the program.

in the example above, the number 1 and 2 are basically the same but the type of Data type was indicated in example number 2. unlike Java, Indicating datatype in kotlin is optional, as Kotlin can infer data type to the variable according to the value assigned to it. var assigns value during run time

Val

Val is used to declaring Immutable variables. Once a value to assign to a variable using the val keyword, it cannot be altered or re-assigned throughout the program. Val can be likened to the "final" keyword in Java

val age = 35

throughout the program, it is expected that the value of age remains 35, if you try to assign another value to age, it throws an error.
Val is also a run-time constant. Meaning that its value has to be assigned during run time.

const

const is also used to declare variables that do not change in the course of the program, but const is a compile-time constant, meaning its value has to be assigned at compile-time, unlike var and var. so because const needs to know its value at compile-time, you can not assign functions to them or constructors since values of functions are determined at run-time.

const are mostly declared globally at the top of the file like

const val language = "en"

or you can declare const in an object like

object Utils {
const val URL = "google"
}

the "object" keyword is just like the "static" keyword in Java, it makes it possible for you to access that variable anywhere in your program.

so from the above explanations, we can now see the differences in those various keywords used in declaring variable sin kotlin

you can also see this take to make it clearer.

Var Val Const
They are Mutable they are Immutable they are Immutable
values are assigned at run time values are assigned at run time values are assigned at compile time
can be assigned to primitive data types can be assigned to primitive data types can be assigned to primitive data types
can be assigned to functions can be assigned to functions can not be assigned to functions

your Reviews would be appreciated


Below are some of the markup used in this post

  • header
  • tables
  • images with link
  • bold text
  • align center
  • ruler
  • block quotes
  • bullets
  • code
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  
  ·  3 years ago (edited)

I am delighted to inform you that you have completed this achievement task. You are cleared to start achievement 5 task. Congratulations. @odogwu001.

Rating: 3

Hi, @odogwu001,

Your post has been supported by @focusnow from the Steem Greeter Team.