<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Amandeep Singh</title>
    <description>The latest articles on Forem by Amandeep Singh (@amandeep404).</description>
    <link>https://forem.com/amandeep404</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F883805%2F80450569-6878-4210-b466-ec582bd893f3.png</url>
      <title>Forem: Amandeep Singh</title>
      <link>https://forem.com/amandeep404</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/amandeep404"/>
    <language>en</language>
    <item>
      <title>Higher-order functions and lambdas in Kotlin</title>
      <dc:creator>Amandeep Singh</dc:creator>
      <pubDate>Wed, 02 Nov 2022 19:32:20 +0000</pubDate>
      <link>https://forem.com/amandeep404/higher-order-functions-and-lambdas-in-kotlin-2agm</link>
      <guid>https://forem.com/amandeep404/higher-order-functions-and-lambdas-in-kotlin-2agm</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzrodzieg3w6anulwmmr1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzrodzieg3w6anulwmmr1.png" alt="Functions in Kotlin By Amandeep" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Higher-Order Functions:
&lt;/h2&gt;

&lt;p&gt;Functions that return a function as their result, and function types which enable you to define functions as values. Some programming languages are purely functional, meaning the entire application is structured in this&lt;br&gt;
style. &lt;br&gt;
While Kotlin is not a pure functional programming language, it does provide many functional tools to allow developers to express logic in a functional paradigm. Here, we write our code-part after &lt;code&gt;-&amp;gt;&lt;/code&gt; sign where type annotation is optional.&lt;br&gt;
We must explicitly declare the type of our lambda expression. If lambda returns no value then we can use Unit &lt;br&gt;
pattern : &lt;strong&gt;(Input) -&amp;gt; Output&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;But to understand Higher-Order functions with examples we need to learn about lambda expressions.&lt;/p&gt;
&lt;h2&gt;
  
  
  Lambda Expression:
&lt;/h2&gt;

&lt;p&gt;A lambda expression is always surrounded by curly braces &lt;code&gt;{}&lt;/code&gt; and arguments are declared inside the curly braces.&lt;br&gt;
&lt;strong&gt;Syntax of lambda functions:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;val lambda_name : Data_type = { variable -&amp;gt; body_of_function }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will be easy to understand with an example:&lt;br&gt;
We can use lambda functions to simply add to numbers 'a' and 'b'.&lt;/p&gt;

&lt;p&gt;example with type annotation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;val add = { a: Int, b: Int -&amp;gt;
     val num = a + b 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;example without type annotation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;val add:(Int,Int)-&amp;gt; Int  = { a , b -&amp;gt; 
       val num = a + b
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Adding two numbers using lambda-functions:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
val add = { a: Int, b: Int -&amp;gt;
    val num = a + b
    num.toString()     //convert Integer to String
}
fun main() {
    val result = add(2,3)
    println("The sum of two numbers is: $result")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The sum of two numbers is: 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can pass a lambda expression as a parameter to Higher-Order Function. Now we will go through &lt;strong&gt;Higher-Order functions:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var greeting = {println("Hello, My name is Amandeep Singh") }

      // using higher-order function
fun higherfunction( greet: () -&amp;gt; Unit ) {     // use of lambda as a parameter

    greet()                               //invokes lambda function

}

fun main() {

     //call higher-order function
    higherfunction(greeting)                 // passing lambda as parameter

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello, My name is Amandeep Singh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it for lambda and Higher-Order functions. Now you have a tight grip over them.&lt;/p&gt;

&lt;p&gt;If you have any doubt or suggestion you can share it in the discussion section.&lt;br&gt;
Wanna connect? connect with me on &lt;a href="https://www.linkedin.com/in/amandeep-singh-452949246/"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp4rt65w2vb1o4dx7ccqu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp4rt65w2vb1o4dx7ccqu.png" alt="Thank You Image" width="800" height="567"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>programming</category>
      <category>beginners</category>
      <category>functions</category>
    </item>
    <item>
      <title>Functions in Kotlin</title>
      <dc:creator>Amandeep Singh</dc:creator>
      <pubDate>Sun, 04 Sep 2022 05:48:06 +0000</pubDate>
      <link>https://forem.com/amandeep404/functions-in-kotlin-4h6l</link>
      <guid>https://forem.com/amandeep404/functions-in-kotlin-4h6l</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzrodzieg3w6anulwmmr1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzrodzieg3w6anulwmmr1.png" alt="Functions in Kotlin By Amandeep" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Functions are something that we have used already for example the &lt;code&gt;main()&lt;/code&gt; function. Functions are used to reuse code very easily. But today we will learn how to create our very own functions.&lt;/p&gt;

&lt;h3&gt;
  
  
  About Functions:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;A block of code that performs a specific task&lt;/li&gt;
&lt;li&gt;Breaks a large program into smaller modular chunks&lt;/li&gt;
&lt;li&gt;Can take arguments with either named or default values&lt;/li&gt;
&lt;li&gt;Functions are declared using the &lt;code&gt;fun&lt;/code&gt; keyword and their general syntax is
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun myFunction(parameter1: Int, parameter2: String){

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Parameters:&lt;/strong&gt; Function parameters are defined using Pascal notation i.e., First the name of the parameter (example parameter1) followed by the Datatype of the parameter (example: Int). Parameters are separated using commas, and each parameter must be explicitly typed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Function Arguments
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Default Parameters&lt;/li&gt;
&lt;li&gt;Required Parameters&lt;/li&gt;
&lt;li&gt;Named arguments&lt;/li&gt;
&lt;li&gt;Single-expressed functions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Default Parameters:&lt;/strong&gt; &lt;a&gt;&lt;/a&gt; Function parameters can have default values, which are used when you skip the corresponding argument. This reduces the number of overloads.&lt;br&gt;
Default values provide a fallback if no parameter value is passed. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Froutbxvi7uaixi0hd7um.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Froutbxvi7uaixi0hd7um.png" alt="Default Parameters" width="800" height="401"&gt;&lt;/a&gt;  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Required Parameters:&lt;/strong&gt;&lt;a&gt;&lt;/a&gt; If no default is specified for a parameter, the corresponding argument is required.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fet9iy9axjpv1zwnkfjoo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fet9iy9axjpv1zwnkfjoo.png" alt="Required Parameters" width="800" height="406"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Default vs Required parameters
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkr7iu19c8vrfdj4eoo5y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkr7iu19c8vrfdj4eoo5y.png" alt="Default versus required parameters&amp;lt;br&amp;gt;
" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Named Arguments &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;When calling a function, you can name one or more of its arguments. This can be helpful when a function has many arguments and it's difficult to associate a value with an argument, especially if it's a boolean or null value.&lt;/p&gt;

&lt;p&gt;When you use named arguments in a function call, you can freely change the order they are listed in, and if you want to use their default values, you can just leave these arguments out altogether.&lt;/p&gt;

&lt;p&gt;Here we have a function reformat() which has 4 arguments with default values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun reformat(
    str: String,
    normalizeCase: Boolean = true,
    upperCaseFirstLetter: Boolean = true,
    divideByCamelHumps: Boolean = false,
    wordSeparator: Char = ' ',
) {
   //Your code
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's considered good style to put default arguments after positional arguments, that way callers only have to specify the required arguments.&lt;/p&gt;

&lt;p&gt;The advantage of named functions is that we don’t have to name all its arguments when calling the function. you can skip all the default values and call the function like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reformat("This is a long String!")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Single-Expressed functions:&lt;/strong&gt; &lt;a&gt;&lt;/a&gt; Kotlin single-expression function is a function where a single expression is assigned to the function, and the expression’s evaluated value is returned when this function is called .&lt;br&gt;
Single-expression functions are compact functions that make your code more concise and readable. &lt;br&gt;
The syntax of Kotlin Single-Expression Function is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun functionName(parameters) = expression
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An example of single-expressed of function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun double(x: Int):Int = x * 2 //single-expressed function

fun main(){
   val x =2 
   val square = double(x)
   println("The square of $x is $square")
}

OUTPUT: The square of 2 is 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it for this Blog. Now you have a tight grip over Kotlin functions. We almost covered functions is Kotlin except &lt;code&gt;Lambdas and higher-order functions&lt;/code&gt;, which we will cover in the next blog&lt;/p&gt;

&lt;p&gt;If you have doubt in any part you can ask it in the discussion section.&lt;br&gt;
Wanna connect? connect with me on &lt;a href="https://www.linkedin.com/in/amandeep-singh-452949246/"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp4rt65w2vb1o4dx7ccqu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp4rt65w2vb1o4dx7ccqu.png" alt="Thank You Image" width="800" height="567"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>functions</category>
      <category>beginners</category>
      <category>android</category>
      <category>kotlin</category>
    </item>
    <item>
      <title>Objects and Classes in Kotlin</title>
      <dc:creator>Amandeep Singh</dc:creator>
      <pubDate>Tue, 16 Aug 2022 15:46:00 +0000</pubDate>
      <link>https://forem.com/amandeep404/objects-and-classes-in-kotlin-2ojp</link>
      <guid>https://forem.com/amandeep404/objects-and-classes-in-kotlin-2ojp</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fab82y7ueyz0v4welz999.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fab82y7ueyz0v4welz999.png" alt="Classes and objects" width="800" height="450"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Kotlin supports both object oriented programming (OOP) as well as functional programming. Object oriented programming is based on real time objects and classes.&lt;br&gt;
We have seen classes before, for example our &lt;code&gt;MainActicity()&lt;/code&gt; class.&lt;br&gt;
&lt;strong&gt;Class is a blueprints of an objects&lt;/strong&gt;. Classes define methods that operate on their object instances.&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcp34nbl51clzhl78a5z7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcp34nbl51clzhl78a5z7.png" alt="Classes are blueprints" width="800" height="407"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Syntax to use classes:
&lt;/h3&gt;

&lt;p&gt;Kotlin classes are declared using keyword &lt;strong&gt;class&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class House {
  var color: String = "white"
  var numberOfWindows: Int = 2
  var isForSale: Boolean = false

  fun updateColor(newColor: String){
             newColor = Color
}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Objects in Kotlin:
&lt;/h3&gt;

&lt;p&gt;Object is an instance of a class. Usually, you define a class and then create multiple instances of that class. &lt;strong&gt;Object is used to access the properties and member function of a class&lt;/strong&gt;. Kotlin allows to create multiple object of a class.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax of Using Object in Kotlin:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var obj = House()  

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Using a class:-&lt;/strong&gt; We use a class by creating a new Object Instance&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class House {
    var color: String = "white"

    fun updateColor(newColor: String){
        color = newColor
    }
}

fun main(){

    var myHouse = House()
    myHouse.updateColor("Green") //Updates the color of the House

    println(myHouse.color) // Will Print the new color of the House

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Green
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvja6w1irhc6j2bnbobr0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvja6w1irhc6j2bnbobr0.png" alt="Thank You" width="800" height="567"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you have doubt in any part you can ask it in the discussion section.&lt;br&gt;
Wanna connect? connect with me on &lt;a href="https://www.linkedin.com/in/amandeep-singh-452949246/"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

</description>
      <category>objects</category>
      <category>kotlin</category>
      <category>classes</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Operators in Kotlin</title>
      <dc:creator>Amandeep Singh</dc:creator>
      <pubDate>Sun, 07 Aug 2022 12:12:00 +0000</pubDate>
      <link>https://forem.com/amandeep404/lesson-12-operators-in-kotlin-646</link>
      <guid>https://forem.com/amandeep404/lesson-12-operators-in-kotlin-646</guid>
      <description>&lt;p&gt;In this blog we will cover:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What are operators&lt;/li&gt;
&lt;li&gt;Types of operators&lt;/li&gt;
&lt;li&gt;Arithmetic Operators&lt;/li&gt;
&lt;li&gt;Increment and Decrement Operators&lt;/li&gt;
&lt;li&gt;Assignment Operators&lt;/li&gt;
&lt;li&gt;Logical Operators&lt;/li&gt;
&lt;li&gt;Bitwise Operators&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffv1rbqiiqh28nsnjfdn9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffv1rbqiiqh28nsnjfdn9.png" alt="Operators in Kotlin" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is an Operator?&lt;/strong&gt;&lt;br&gt;
An operator, in computer programing, is a symbol that usually represents an action or process. These symbols were adapted from mathematics and logic.&lt;br&gt;
Operators are the backbone of any program and they are used for everything from very simple functions like counting to complex algorithms like security encryption.&lt;/p&gt;

&lt;p&gt;We have different types of operators&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcidskhas9cbuyzis6ags.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcidskhas9cbuyzis6ags.png" alt="Different types of operators" width="800" height="401"&gt;&lt;/a&gt;&lt;br&gt;
Now, we will go through each type of operator one by one.&lt;br&gt;
&lt;strong&gt;1.Arithmetic Operator:&lt;/strong&gt; &lt;em&gt;An operator that performs arithmetic operations on groups of variables and numbers is called&lt;/em&gt; &lt;strong&gt;Arithmetic operator&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun main(){
    var a = 20 var b = 4 
        println("a + b = " + (a + b))
        println("a - b = " + (a - b))
        println("a * b = " + (a*b))
        println("a / b = " + (a / b))
        println("a % b = " + (a % b))
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a + b = 24
a - b = 16
a * b = 80
a / b = 5
a % b = 0

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You are familiar with all the operators because you use them in your daily life..but what does "%" this sign does?&lt;br&gt;
This sign is called &lt;strong&gt;modulo(%)&lt;/strong&gt;. &lt;br&gt;
OK, but what does it do?&lt;br&gt;
The modulo division operator produces the remainder of an integer division. For example, If 5 and 2 are integers, then the expression: &lt;br&gt;&lt;code&gt;5 % 2&lt;/code&gt;&lt;br&gt;produces the remainder when &lt;strong&gt;5 is divided by 2&lt;/strong&gt;, which is &lt;strong&gt;1&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.Increment and Decrement Operators:&lt;/strong&gt; &lt;em&gt;The increment(++) and decrement operators(--) are important unary operators in Kotlin. Unary operators are those which are applied on a single operand. The increment operator increases the value of the variable by one and the decrement operator decreases the value of the variable by one.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Types of Increment Operators:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prefix Increment Operator: If you use the ++ operator as a prefix like: &lt;code&gt;++var&lt;/code&gt;, the value of var is incremented by 1, then it returns the value.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun main(){
    val num = 5
    println("num = "+ num++)//First the value of num is printed and then a1 increases by 1.
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;num = 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Postfix Increment Operator: If you use the ++ operator as a postfix like: &lt;code&gt;var++&lt;/code&gt;, the original value of var is returned first, then var is incremented by 1.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun main(){
    val num = 5
    println("num = "+ ++num) //First the value of a2 increases by 1 and then the new value of num is printed.
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;num = 6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3.Assignment Operators:&lt;/strong&gt; &lt;em&gt;Assignment operators are used to assigning value to a variable. The left side operand of the assignment operator is a variable and right side operand of the assignment operator is a value. The value on the right side must be of the same data-type of the variable on the left side otherwise the compiler will raise an error.&lt;/em&gt; Assignment operator return a Boolean Values.&lt;br&gt;
Different types of assignment operators are shown below:&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhmkzil03ou435djocmnf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhmkzil03ou435djocmnf.png" alt="Different types of assignment operators" width="525" height="483"&gt;&lt;/a&gt;&lt;br&gt;
Here is the code for your better understanding.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun main(){
    var c = 30
    var d = 40
    println("c &amp;gt; d = "+(c&amp;gt;d))
    println("c &amp;lt; d = "+(c&amp;lt;d))
    println("c &amp;gt;= d = "+(c&amp;gt;=d))
    println("c &amp;lt;= d = "+(c&amp;lt;=d))
    println("c == d = "+(c==d))
    println("c != d = "+(c!=d))
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;c &amp;gt; d = false
c &amp;lt; d = true
c &amp;gt;= d = false
c &amp;lt;= d = true
c == d = false
c != d = true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4.Logical Operators:&lt;/strong&gt; &lt;em&gt;Logical operators are generally used for combining two or more relational statements. They return Boolean values. The logical operators are used primarily in the expression evaluation to make a decision. These operators allow the evaluation and manipulation of specific bits within the integer.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The types of Logical operators with their description are tabulated as follows -&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fioha7e0k2okyrbm29cm8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fioha7e0k2okyrbm29cm8.png" alt="types of Logical operators" width="800" height="439"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun main(){
    var x = 100
    var y = 25
    var z = 10
    var result = false
    if(x &amp;gt; y &amp;amp;&amp;amp; x &amp;gt; z){
    println(x)
}
    if(x &amp;lt; y || x &amp;gt; z){
    println(y)
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;100
25
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5.Bitwise Operators:&lt;/strong&gt;&lt;em&gt;The bitwise operators are the operators used to perform the operations on the data at the bit-level. When we perform the bitwise operations, then it is also known as bit-level programming. It consists of two digits, either 0 or 1. It is mainly used in numerical computations to make the calculations faster.&lt;/em&gt; Bitwise and bit shift operators are used on only two integral types (Int and Long) to perform bit-level operations.&lt;/p&gt;

&lt;p&gt;We have different types of bitwise operators in the Kotlin. The following is the list of the bitwise operators:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhf06hp1dk4elgudd221b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhf06hp1dk4elgudd221b.png" alt="different types of bitwise operators" width="800" height="315"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;or:&lt;/strong&gt; The &lt;code&gt;or&lt;/code&gt; function compares corresponding bits of two values. If either of the bits is 1, it gives 1. If not, it gives 0. For example,
&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnga5bktx4zha8ytche2h.png" alt="example of or bitwise" width="800" height="415"&gt;
Let's implement this in our code!
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun main() {

    val number1 = 12
    val number2 = 25
    val result: Int

    result = number1 or number2   // result = number1.or(number2)
    println(result)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;29
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;and:&lt;/strong&gt; The &lt;code&gt;and&lt;/code&gt; function compares corresponding bits of two values. If both bits are 1, it is evaluated to 1. If either of the bits is 0, it is evaluated to 0. For example,
&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6a84kuq7bj5f6i2ywvxs.png" alt="example of and bitwise" width="678" height="381"&gt;
Let's implement this in our code!
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun main() {

    val number1 = 12
    val number2 = 25
    val result: Int

    result = number1 and number2   // result= number1.and(number2)
    println(result)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;8
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;xor:&lt;/strong&gt; The &lt;code&gt;xor&lt;/code&gt; function compares corresponding bits of two values. If corresponding bits are different, it gives 1. If corresponding bits are same, it gives 0. For example,
&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6r4tqk85yhmipf6bfs46.png" alt="example of xor" width="611" height="295"&gt;
Let's implement this in our code!
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun main() {

    val number1 = 12
    val number2 = 25
    val result: Int

    result = number1 xor number2   // result= number1.xor(number2)
    println(result)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;21
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;inv():&lt;/strong&gt; The &lt;code&gt;inv()&lt;/code&gt; function inverts the bit pattern. It makes every 0 to 1, and every 1 to 0. For example,
&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr0zyaed9delowy70ibwv.png" alt="example of inv()" width="567" height="230"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this in our code!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun main() {

    val number = 35
    val result: Int

    result = number.inv()
    println(result)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-36
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why are we getting output -36 instead of 220?&lt;/strong&gt;&lt;br&gt;
It's because the compiler is showing 2's complement of that number; negative notation of the binary number. &lt;/p&gt;

&lt;p&gt;For any integer &lt;code&gt;n&lt;/code&gt;, 2's complement of &lt;code&gt;n&lt;/code&gt; will be &lt;code&gt;-(n+1)&lt;/code&gt;.&lt;br&gt;
The bitwise complement of 35 is 220 (in decimal). The 2's complement of 220 is -36. Hence, the output is -36 instead of 220.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;shl:&lt;/strong&gt; The &lt;code&gt;shl&lt;/code&gt; function shifts bit pattern to the left by certain number of specified bits, and zero bits are shifted into the low-order positions. For example,
&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcfexd1j079mh4l1of9yh.png" alt="example of shl" width="800" height="191"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this in our code!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun main() {

    val number = 212

    println(number shl 1)
    println(number shl 0)
    println(number shl 4)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;424
212
3392
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;shr:&lt;/strong&gt; The &lt;code&gt;shr&lt;/code&gt; function shifts bit pattern to the right by certain number of specified bits. For example,
&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq652l2oxpc7l2czmltpf.png" alt="example of shr" width="800" height="207"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this in our code!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun main() {

    val number = 212

    println(number shr 1)
    println(number shr 0)
    println(number shr 8)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;106
212
0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;ushr:&lt;/strong&gt; The &lt;code&gt;ushr&lt;/code&gt; function shifts zero into the leftmost position. For example,
&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcfexd1j079mh4l1of9yh.png" alt="example of ushr" width="800" height="191"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this in our code!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun main() {

    val number1 = 5
    val number2 = -5

    // Signed right shift
    println(number1 shr 1)

    // Unsigned right shift
    println(number1 ushr 1)

    // Signed right shift
    println(number2 shr 1)

    // Unsigned right shift
    println(number2 ushr 1)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2
2
-3
2147483645
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice, how signed and unsigned right shift function works differently for 2's complement.&lt;/p&gt;

&lt;p&gt;The 2's complement of &lt;code&gt;2147483645&lt;/code&gt; is &lt;code&gt;3&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;These are they types of operators we have in Kotlin. Hope you understood them well as you will be using them on a regular basis as an android developer😊.&lt;/p&gt;

&lt;p&gt;You have doubt in any part you can ask it in the discussion section.&lt;br&gt;
Wanna connect? then connect with me on &lt;a href="https://www.linkedin.com/in/amandeep-singh-452949246/"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9facsecp3kserio0bek0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9facsecp3kserio0bek0.png" alt="Thank You" width="800" height="567"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>android</category>
      <category>operatorsinkotlin</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Kotlin Basics</title>
      <dc:creator>Amandeep Singh</dc:creator>
      <pubDate>Wed, 27 Jul 2022 19:39:00 +0000</pubDate>
      <link>https://forem.com/amandeep404/lesson-11-kotlin-basics-3j9c</link>
      <guid>https://forem.com/amandeep404/lesson-11-kotlin-basics-3j9c</guid>
      <description>&lt;p&gt;In this &lt;strong&gt;Kotlin Basics&lt;/strong&gt; series ,we will cover :-&lt;br&gt;
Operators&lt;br&gt;
Data types&lt;br&gt;
Variables&lt;br&gt;
Conditionals&lt;br&gt;
Lists and arrays&lt;br&gt;
Null safety&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Today's Content:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creating a new project in Android Studio&lt;/li&gt;
&lt;li&gt;Hello world in Kotlin &lt;/li&gt;
&lt;li&gt;Variables in Kotlin&lt;/li&gt;
&lt;li&gt;Datatypes in Kotlin&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So to start we need an IDE. I will use Android Studio but you all can use some other IDE's as well such as IntelliJ , Visual Studio Code and others.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lets get started...&lt;/strong&gt;&lt;br&gt;
Open Android Studio and select new project.&lt;br&gt;
Select &lt;strong&gt;No Activity&lt;/strong&gt; and click &lt;strong&gt;Next&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr7bnrcpfrftuimksq3e8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr7bnrcpfrftuimksq3e8.png" alt="Starting new project in Android Studio"&gt;&lt;/a&gt;&lt;br&gt;
Now a new project window appears with lot of details and it feels very overwhelming to the students seeing it for the first time, but don't worry we will decode it.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1knr8o75lpv0yskg3x0b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1knr8o75lpv0yskg3x0b.png" alt="Explaining new project window in Kotlin"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;1:&lt;/strong&gt; It is the name by which Android Studio will save your project. Since this is the Kotlin basics lesson I have named it Kotlin-basics&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2:&lt;/strong&gt; You can understand package name as an unique Id which helps to distinguish apps on other devices and Google Playstore. Each project you create on Android will have an unique package name. You don't need to own a .com domain in order to release apps on Play store. It is just a standard. So, Pick some unique name and suffix it with "com. "&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3:&lt;/strong&gt; Save location is the location of the directory and folders in which your current project will be saved. You can use the default location or select your own. As you wish.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4:&lt;/strong&gt; This is a language selector with two options: Java or Kotlin. In this course we use Kotlin.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5:&lt;/strong&gt; Minimum SDK is the minimum android version in which your App will run. In this lesson I am using API 21 which is Android-5(Lollipop) and it covers almost 98.6% of Android devices on this planet and that is a huge number.&lt;br&gt;
Using very low API means you will cover a lot more of Android devices but you will not be able to use a lot of latest libraries introduced in higher API's by Google. So, I think API 21 will be a balanced option for most of you all.&lt;/p&gt;

&lt;p&gt;Now, lets dive in and select &lt;strong&gt;Finish&lt;/strong&gt;.&lt;br&gt;
If its your first time Android Studio will take time to build Gradle files and import libraries, but the next time onwards it will not take so much time and it is recommended you have a high speed internet connection to load the project as quick as possible.&lt;/p&gt;

&lt;p&gt;I have a different theme applied but your workspace should look more or less like this.&lt;br&gt;
Right-Click on &lt;strong&gt;com.example.kotlin_baiscs_01&lt;/strong&gt; folder then go to &lt;br&gt;&lt;strong&gt;new&lt;/strong&gt;-&amp;gt;&lt;strong&gt;Kotlin/class file&lt;/strong&gt; then give your file a name while the &lt;strong&gt;file&lt;/strong&gt; section is selected and then press &lt;strong&gt;Enter&lt;/strong&gt;. &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4vtb0naa8y6wqm1he3y1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4vtb0naa8y6wqm1he3y1.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Now your window should look like this&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1bck97xls44806gl2jym.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1bck97xls44806gl2jym.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;HELLO WORLD&lt;/strong&gt;&lt;br&gt;
lets print hello world in Kotlin. First we need to create a main function &lt;br&gt; &lt;code&gt;fun main(){&lt;br&gt;
println("hello world")&lt;br&gt;
}&lt;/code&gt;&lt;br&gt;
Now, there are many ways to run your code as pointed in the image&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe93mo8f8qlqcjuy2rtc6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe93mo8f8qlqcjuy2rtc6.png" alt="printing hello world in Kotlin"&gt;&lt;/a&gt;&lt;br&gt;
It is necessary to have the &lt;strong&gt;fun main()&lt;/strong&gt; block, this is where your code runs. Though you can have several functions but the main() function is must as it is the starting point of our application. It is a special type of function and not a generic function.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;VARIABLES IN KOTLIN&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Variables are simply containers for storing values(data values). To create a variable we use &lt;strong&gt;var&lt;/strong&gt; or &lt;strong&gt;val&lt;/strong&gt;. We use var for storing or initializing such type of data that will or may change in our code. On the other hand data stored in val cannot be changed again or we do not want to change once initialized .For example&lt;br&gt;
&lt;code&gt;val name= "Amandeeep"&lt;br&gt;
val age= 18&lt;/code&gt;&lt;br&gt;
we have the name "Amandeep" in a variable called &lt;strong&gt;name&lt;/strong&gt; and age in a variable called "age".&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;u&gt;DATATYPES IN KOTLIN&lt;/u&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvzvsr4axxms0tgod9hi7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvzvsr4axxms0tgod9hi7.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These are types of DataTypes that we have in Kotlin&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Integer types:&lt;/strong&gt;﻿
Kotlin provides a set of built-in types that represent numbers.
Unlike other languages we do not need to define the datatype for our variables in Kotlin. For example 
```
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;val one = 1 // Int&lt;br&gt;
val threeBillion = 3000000000 // Long&lt;br&gt;
val oneByte = 1&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;And if we need to specify our datatype then we can do it as the following :
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;val one: Int = 1&lt;br&gt;
val 3billion : Long = 3000000000&lt;br&gt;
val oneByte : Byte =1&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;For integer numbers, there are four types with different sizes and, hence, value ranges.
![range of int datatype](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/97szw0bxa1y8im0a1uj6.png)

- **Floating-point types﻿:**&amp;lt;br/&amp;gt;
Kotlin provides floating-point types **Float** and **Double** for Real numbers[example:(0.4, 3.1415927, 1/2)]
floating point types differ by their decimal place, that is, how many decimal digits they can store.**Float** can store 6 to 7 digits places after decimal while **Double** is more precise as it can store 15 to 16 digits after decimal point.
![range of float and double](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xj8qotnd7nl7x55kh4fh.png)

- **Boolean Data type﻿:**&amp;lt;br/&amp;gt;
The Boolean data type and can only take the values true or false. Boolean values are mostly used for conditional testing, which you will learn more about in a later chapter.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;val kotlinIsFun: Boolean = true&lt;br&gt;
val fishIsTasty: Boolean = false&lt;/p&gt;

&lt;p&gt;println(kotlinIsFun)   // Outputs true&lt;br&gt;
println(fishIsTasty)   // Outputs false &lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
- **Character Data type﻿:**&amp;lt;br/&amp;gt;
The Character data type(represented by **char**) is used to store a single character. 
Char data type represents the small letters(a-z), Capital letters(A-Z), digits(0-9) and other symbols. A **char** value must be surrounded by single quotes, like 'A' or 'c'.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;val myGrade: Char = 'A'&lt;br&gt;
println(myGrade)  // output: A&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- **String Data type﻿:**&amp;lt;br/&amp;gt;
The String data type is used to store a sequence of characters (text). String values must be surrounded by double quotes.

- Strings are any sequence of characters enclosed by double quotes.
  val s1 = "Hello world!"
- String literals can contain escape characters
  val s2 = "Hello world!\n" 
_Here, "\n" is an escape character which inserts a newline in the text where it appears._
- Or any arbitrary text delimited by a triple quote (""")
  val text = """ var bikes = 50 """

**That's all for Today. Thank You!!😃**
Till then **keep coding!!**
![Thank You](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wwtohfmjqzep67x70oek.png)







&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>kotlin</category>
      <category>andri</category>
      <category>tutorial</category>
      <category>mobile</category>
    </item>
    <item>
      <title>Intro to Android Development</title>
      <dc:creator>Amandeep Singh</dc:creator>
      <pubDate>Thu, 14 Jul 2022 20:56:00 +0000</pubDate>
      <link>https://forem.com/amandeep404/android-development-bootcamp-day-1-46l3</link>
      <guid>https://forem.com/amandeep404/android-development-bootcamp-day-1-46l3</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvh7ced94xz1i32d5xz55.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvh7ced94xz1i32d5xz55.png" alt="Android Development Bootcamp" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TODAY'S CONTENT&lt;/strong&gt;: Course overview and Android Studio Setup&lt;/p&gt;

&lt;p&gt;&lt;u&gt;What you'll learn in this Bootcamp:&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;[] How to build a variety of Android apps in Kotlin&lt;br&gt;
[] Kotlin language essentials&lt;br&gt;
[] Best practices for app development&lt;br&gt;
[] Resources to keep learning&lt;br&gt;
[] You will build a lot of apps during this whole Bootcamp and some will be of Industry Standard&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9grv2y3c0cskwqowpuan.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9grv2y3c0cskwqowpuan.png" alt="course structure" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3xymsrmogl7pt9yg2gkw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3xymsrmogl7pt9yg2gkw.png" alt="Prerequisites to start learning" width="800" height="450"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;What is Android Studio?&lt;/strong&gt;&lt;br&gt;
To write code for your Android application, you need some kind of environment or code editor that will make your task easy. No doubt, you can write the same code by using any normal text editor but Android Studio is required to build Android applications.You can surely code in other IDE'S(Integrated Developer Environment)&lt;br&gt;
but cannot build apps on them. As the course is structured in teaching what is necessary to be a proficient developer we will not waste time on unnecessary knowledge.&lt;/p&gt;

&lt;p&gt;So, Let's Setup Android Studio on your machine!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to install Android Studio?&lt;/strong&gt;&lt;br&gt;
In order to install Android Studio in your system, make sure to download the latest version of the Android Studio from &lt;a href="https://developer.android.com/studio/"&gt;Android Studio website&lt;/a&gt;. Also, check the system requirements of Android Studio here.&lt;/p&gt;

&lt;p&gt;To install Android Studio, you can follow the steps mentioned in the &lt;a href="https://developer.android.com/studio/install"&gt;Android Developer website&lt;/a&gt; for Android Studio Installation.&lt;/p&gt;

&lt;p&gt;After installing, Open Android Studio and it should show you this screen.&lt;br&gt;
Click on "Start a new project"&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fboc75pbw7lwcldtl2wdx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fboc75pbw7lwcldtl2wdx.png" alt="Android Studio welcome screen" width="800" height="536"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's all for today!! Today was just a overview of this Bootcamp. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stay Connected and keep learning🙌&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>bootcammp</category>
      <category>tutorial</category>
      <category>installandroidstudio</category>
    </item>
  </channel>
</rss>
