<?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: Chetan</title>
    <description>The latest articles on Forem by Chetan (@chetansj27).</description>
    <link>https://forem.com/chetansj27</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%2F616184%2F868de76d-50e8-4baf-8813-85100e259306.jpeg</url>
      <title>Forem: Chetan</title>
      <link>https://forem.com/chetansj27</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/chetansj27"/>
    <language>en</language>
    <item>
      <title>SOLID Principles</title>
      <dc:creator>Chetan</dc:creator>
      <pubDate>Mon, 13 May 2024 05:26:17 +0000</pubDate>
      <link>https://forem.com/chetansj27/solid-principles-207k</link>
      <guid>https://forem.com/chetansj27/solid-principles-207k</guid>
      <description>&lt;p&gt;&lt;strong&gt;S (Single Responsibility Principle):&lt;/strong&gt;&lt;br&gt;
A Class should have only one responsibility. Ex: User class should have functions only related to users.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;O (Open/Closed Principle):&lt;/strong&gt;&lt;br&gt;
Open for extension but closed for modification. Means create interface and then extend it to new and old classes but don't change the old class.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;L (Liskov Substitution Principle):&lt;/strong&gt;&lt;br&gt;
If class B is a subtype of class A then we should be able to replace the object of A with B without breaking the behaviour. Means subclass should extend the capabilities of parent class not narrow it down (If base class have 3 capabilities/functions then subclass should have at least 3 capabilities/functions).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;I (Interface Segmented Principle):&lt;/strong&gt;&lt;br&gt;
Interface should have only necessary functions for subclasses.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;D (Dependency Inversion Principle):&lt;/strong&gt;&lt;br&gt;
Class should depend on interfaces rather than concrete classes. Means always create an object of the interface.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>designpatterns</category>
    </item>
    <item>
      <title>CRUD API with Go and PostgreSQL</title>
      <dc:creator>Chetan</dc:creator>
      <pubDate>Sat, 29 Oct 2022 13:28:47 +0000</pubDate>
      <link>https://forem.com/chetansj27/crud-api-with-go-and-postgresql-411n</link>
      <guid>https://forem.com/chetansj27/crud-api-with-go-and-postgresql-411n</guid>
      <description>&lt;p&gt;In this tutorial, we're gonna build a CRUD API using GO and PostgreSQL database. We will use GORM to interact with database. You'll know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How to perform CRUD operations using Go.&lt;/li&gt;
&lt;li&gt;How to use Gorilla Mux for creating routes. &lt;/li&gt;
&lt;li&gt;How to perform database operation using GORM.&lt;/li&gt;
&lt;li&gt;How to marshal and unmarshal the data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Overview of what we are going to build in this article&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%2Ffsx7kde629y6disxqorr.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%2Ffsx7kde629y6disxqorr.png" alt="Controller Functions"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;GetUsers&lt;/strong&gt; - This function will list all users.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AddUsers&lt;/strong&gt; - It will accept user's details as request body and save the user in database.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GetUserById&lt;/strong&gt; - This function will accept userId as path variable and will list the user by userId.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;UpdateUser&lt;/strong&gt; - It will accepts user's updated details as request body and accept userId as path variable then this will save the updated details in database.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DeleteUser&lt;/strong&gt; - This function will accept userId as path variable and will delete the user from database.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Steps to create project
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Create a empty folder.&lt;/li&gt;
&lt;li&gt;Open Command Prompt in that folder and run &lt;code&gt;go mod init modulepath&lt;/code&gt;. Modulepath can be the github url of code repository. This command will create a &lt;strong&gt;go.mod&lt;/strong&gt; file to track your code's dependencies. As of now we will see only name of your module and the Go version your code supports.&lt;/li&gt;
&lt;li&gt;Run this command &lt;code&gt;go get -u gorm.io/gorm&lt;/code&gt; and &lt;code&gt;go get -u gorm.io/driver/postgres&lt;/code&gt; to install GORM module for interacting with database.&lt;/li&gt;
&lt;li&gt;Again run this command &lt;code&gt;go get -u github.com/gorilla/mux&lt;/code&gt; to install Gorilla Mux for creating routes.&lt;/li&gt;
&lt;li&gt;Now create the necessary folders. Our project structure should look like this.
&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%2Fscenbaubuiotrhv6k201.png" alt="Folder Structure"&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Let's start writing the code
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Creating Routes:
&lt;/h3&gt;

&lt;p&gt;First we will create the routes for our project. Open &lt;strong&gt;users-routes.go&lt;/strong&gt;. Import &lt;strong&gt;gorilla/mux&lt;/strong&gt; for routes. Import &lt;strong&gt;controllers&lt;/strong&gt; folder from your project. users-routes.go will look 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;package routes

import (
    "github.com/chetansj27/crud-go/pkg/controllers"
    "github.com/gorilla/mux"
)

var RegisterUserRoutes = func(router *mux.Router) {
    router.HandleFunc("/users", controllers.GetUsers).Methods("GET")
    router.HandleFunc("/users", controllers.AddUsers).Methods("POST")
    router.HandleFunc("/users/{userId}", controllers.GetUserById).Methods("GET")
    router.HandleFunc("/users/{userId}", controllers.UpdateUser).Methods("PUT")
    router.HandleFunc("/users/{userId}", controllers.DeleteUser).Methods("DELETE")
}

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

&lt;/div&gt;



&lt;p&gt;When any user hit the GET request on &lt;strong&gt;/users&lt;/strong&gt; path, our route will transfer that request to GetUsers method in controller.&lt;br&gt;
&lt;code&gt;router.HandleFunc("/users", controllers.GetUsers).Methods("GET")&lt;/code&gt;&lt;br&gt;
Same will happen for others requests also.&lt;/p&gt;
&lt;h3&gt;
  
  
  Connecting With Database:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;config.go&lt;/strong&gt; file will be used to connect with database. This file will contains database name, username and password. Below code will be used in config.go&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package config

import (
    "gorm.io/driver/postgres"
    "gorm.io/gorm"
)

var db *gorm.DB

func Connect() {
    dsn := "host=localhost user=username password=password dbname=gorm port=9920 sslmode=disable "
    d, err := gorm.Open(postgres.Open(dsn), &amp;amp;gorm.Config{})
    if err != nil {
        panic(err)
    }
    db = d
}
func GetDB() *gorm.DB {
    return db
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Creating Utility Class:
&lt;/h3&gt;

&lt;p&gt;Now we will open &lt;strong&gt;utility.go&lt;/strong&gt; file. This file will be used for &lt;strong&gt;marshalling&lt;/strong&gt; and &lt;strong&gt;unmarshalling&lt;/strong&gt; the data. Marshalling means encoding Go Objects into JSON and Unmarshalling means converting json(Byte data) into Struct. Below code will handle this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package utils

import (
    "encoding/json"
    "io/ioutil"
    "net/http"
)

func ParseBody(r *http.Request, x interface{}) {
    if body, err := ioutil.ReadAll(r.Body); err == nil {
        if err := json.Unmarshal([]byte(body), x); err != nil {
            return
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Models and Database Query:
&lt;/h3&gt;

&lt;p&gt;Our &lt;strong&gt;user.go&lt;/strong&gt; file will contains user model and all database queries.&lt;br&gt;
First we will create &lt;strong&gt;User&lt;/strong&gt; struct&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type User struct {
    Id      uint64 `gorm:"primaryKey" json:"id"`
    Name    string `json:"name"`
    Email   string `json:"email"`
    Address string `json:"address"`
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Id&lt;/code&gt; is the primary key.&lt;br&gt;
Now we will create a &lt;code&gt;init&lt;/code&gt; function for initializing the database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var db *gorm.DB
func init() {
    config.Connect()
    db = config.GetDB()
    db.AutoMigrate(&amp;amp;User{})
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In above code we are initializing the database connection which we defined in &lt;strong&gt;config.go&lt;/strong&gt; file.&lt;br&gt;
&lt;code&gt;db.AutoMigrate(&amp;amp;User{})&lt;/code&gt; will create a user table in database where fields of user struct will be converted into columns.&lt;/p&gt;
&lt;h4&gt;
  
  
  Add User Query
&lt;/h4&gt;

&lt;p&gt;We will create a &lt;em&gt;AddUser&lt;/em&gt; function which will take User struct as request body and will return the saved User.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func AddUser(us *User) *User {
    db.Create(&amp;amp;us)
    return us
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the same way we can create all function according to our need.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating function in controllers:
&lt;/h3&gt;

&lt;p&gt;Controllers are very important for any api. user-routes.go will transfer requests to controller. &lt;br&gt;
We will create a function in controller for adding the user.&lt;br&gt;
user-controllers.go will contain the code for controllers.&lt;/p&gt;
&lt;h5&gt;
  
  
  Add User
&lt;/h5&gt;

&lt;p&gt;We will AddUser function for creating new user and saving them to database. AddUser function will have two parameters one for returning the response and other for request body.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func AddUser(writer http.ResponseWriter, request *http.Request) {
    addUser := &amp;amp;models.User{}
    utils.ParseBody(request, addUser)
    user := models.AddUser(addUser)
    res, _ := json.Marshal(user)
    writer.Header().Set("Content-Type", "application/json")
    writer.WriteHeader(http.StatusOK)
    writer.Write(res)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In above code first we create a addUser variable of User struct type. Then we get the request body from request and parsed it into addUser variable. After that we saved the user in database. Then we marshaled the response. In end we set content-type and statusOk in header and then we write the response into writer. That's how user will added in database.&lt;br&gt;
Same way we can create function for getting the user, updating the user and deleting the user.&lt;/p&gt;
&lt;h3&gt;
  
  
  Last Step:
&lt;/h3&gt;

&lt;p&gt;After creating all function, now we will configure our &lt;strong&gt;main.go&lt;/strong&gt; file. In this file we will register our routes and define host and port for running the applicatio.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func main() {
    route := mux.NewRouter()
    routes.RegisterUserRoutes(route)
    http.Handle("/", route)
    log.Fatal(http.ListenAndServe("localhost:8182", route))
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are all set for running our application. Now open terminal and cd into cmd\main package and run &lt;code&gt;go run main.go&lt;/code&gt; this command. Now our application is live at &lt;strong&gt;localhost:8182&lt;/strong&gt;.&lt;br&gt;
For complete code you can visit &lt;a href="https://github.com/chetansj27/crud-go" rel="noopener noreferrer"&gt;Github Link&lt;/a&gt;&lt;br&gt;
Below are screenshots of application.&lt;/p&gt;

&lt;h4&gt;
  
  
  Add User
&lt;/h4&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%2Fnf108jqj5ch43xtv23c7.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%2Fnf108jqj5ch43xtv23c7.png" alt="Add User"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Get All Users
&lt;/h4&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%2Frr9yipyghbbpbldu2l4q.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%2Frr9yipyghbbpbldu2l4q.png" alt="Get All Users"&gt;&lt;/a&gt;&lt;br&gt;
Happy learning!&lt;/p&gt;

</description>
      <category>go</category>
      <category>beginners</category>
      <category>api</category>
      <category>postgres</category>
    </item>
    <item>
      <title>Create Database Role in PostgreSQL</title>
      <dc:creator>Chetan</dc:creator>
      <pubDate>Thu, 23 Dec 2021 17:44:25 +0000</pubDate>
      <link>https://forem.com/chetansj27/create-database-role-in-postgresql-36ag</link>
      <guid>https://forem.com/chetansj27/create-database-role-in-postgresql-36ag</guid>
      <description>&lt;p&gt;Database role is the person who have access to perform action on database.&lt;br&gt;
We can create new database role in PostgreSQL by running below query:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---SR65Opx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x7mno3lzk0s81326t3hp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---SR65Opx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x7mno3lzk0s81326t3hp.png" alt="Query" width="776" height="238"&gt;&lt;/a&gt;&lt;br&gt;
For creating new role you must have CREATEROLE privilege or be a database superuser to use this command.&lt;/p&gt;

&lt;h2&gt;
  
  
  Parameters
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;name&lt;/strong&gt;: This will be the name of new role.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;SUPERUSER/NOSUPERUSER:&lt;/strong&gt; This is clause is used to determine whether the new role will can override all access restrictions within the database. &lt;strong&gt;SUPERUSER&lt;/strong&gt; clause will give all access to new role.  If not specified, &lt;strong&gt;NOSUPERUSER&lt;/strong&gt; is the default.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CREATEDB/NOCREATEDB:&lt;/strong&gt; These are used to determine whether the new role can create Database or not. &lt;strong&gt;CREATEDB&lt;/strong&gt; will give permission to create database to new role whereas &lt;strong&gt;NOCREATEDB&lt;/strong&gt; will deny the ability to create database. If not specified &lt;strong&gt;NOCREATEDB&lt;/strong&gt; is the default.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;INHERIT/NOINHERIT:&lt;/strong&gt; These clause define whether the new role can inherit privileges of roles it is a member of. &lt;strong&gt;INHERIT&lt;/strong&gt; will give the all privileges that have been granted to all roles it is directly or indirectly a member of. If not specified &lt;strong&gt;NOINHERIT&lt;/strong&gt; is the default. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;LOGIN/NOLOGIN:&lt;/strong&gt; These clauses determine whether a role is allowed to log in; that is, whether the role can be given as the initial session authorization name during client connection. A role having &lt;strong&gt;LOGIN&lt;/strong&gt; attribute can be thought as user.  If not specified &lt;strong&gt;NOLOGIN&lt;/strong&gt; is default.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;REPLICATION/NOREPLICATION:&lt;/strong&gt; These clause define whether the role is a replication(replication means copying data from a PostgreSQL database server to another server) role. &lt;strong&gt;REPLICATION&lt;/strong&gt; clause will give the permission to create replication. If not specified &lt;strong&gt;NOREPLICATION&lt;/strong&gt; is default.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CONNECTION LIMIT:&lt;/strong&gt;  This clause specifies how many concurrent connections role can make. By default it is -1 means no limit and maximum limit is 115 connections.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;PASSWORD&lt;/strong&gt;:  This will create a login password for the role. If you don't want to specify password you can omit. If no password is specified the password will be set to null and password authentication will always fail for this role. Password is always stored in encrypted form in system.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;VALID UNTIL:&lt;/strong&gt; This clause set the date and time after which password will be expired. If no value is specified for &lt;strong&gt;VALID UNTIL&lt;/strong&gt; then password will never expire.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JFZDou17--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i0cspywgbdhs8gnsw8zu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JFZDou17--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i0cspywgbdhs8gnsw8zu.png" alt="Query" width="880" height="230"&gt;&lt;/a&gt;&lt;br&gt;
For example above query will create role with &lt;strong&gt;chetan&lt;/strong&gt; name with &lt;strong&gt;login&lt;/strong&gt;, &lt;strong&gt;inherit&lt;/strong&gt;, &lt;strong&gt;createdb&lt;/strong&gt; access and don't have &lt;strong&gt;superuser&lt;/strong&gt;, &lt;strong&gt;replication&lt;/strong&gt; and &lt;strong&gt;createrole&lt;/strong&gt; access. The connection limit is -1 means no limit and password is chetan.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8r324tOZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/faxkrt6xynfs81ctiol2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8r324tOZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/faxkrt6xynfs81ctiol2.png" alt="Query" width="880" height="230"&gt;&lt;/a&gt;&lt;br&gt;
For example above query will create role with &lt;strong&gt;chetan&lt;/strong&gt; name with all privilege. The password will expire after 30 Dec 2021 at 02:10. In same way we can create role with different parameters.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>database</category>
      <category>sql</category>
    </item>
    <item>
      <title>Basics of Kotlin - Part 4</title>
      <dc:creator>Chetan</dc:creator>
      <pubDate>Mon, 26 Apr 2021 08:44:53 +0000</pubDate>
      <link>https://forem.com/chetansj27/basics-of-kotlin-part-4-2h81</link>
      <guid>https://forem.com/chetansj27/basics-of-kotlin-part-4-2h81</guid>
      <description>&lt;p&gt;In the last article, we learnt about some basic concepts of Kotlin like conditional statements, loops and jump statements. Before reading this article make sure you had read the last article &lt;a href="https://dev.to/csj5483/basics-of-kotlin-part-3-1nia"&gt;Basics of Kotlin- Part 3&lt;/a&gt;. Let's learn more about Kotlin.&lt;/p&gt;

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

&lt;p&gt;Functions in any programming language is a group of an interrelated block of code that performs a specific task. Functions allow us to break a program into various small sub-module. Functions increase the readability of code, reusability of code, and makes a program easy to manage.&lt;/p&gt;

&lt;p&gt;In Kotlin &lt;b&gt;fun&lt;/b&gt; keyword is used to declare the function. There are two types of functions in Kotlin depending on whether it is available in the standard library or defined by the user.&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;
&lt;h4&gt;Standard library function:&lt;/h4&gt; Standard library function is built-in library functions that can be defined implicitly and available for use.&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(args: Array&amp;lt;String&amp;gt;){  
var number = 16
var result = Math.sqrt(number.toDouble())  
print("$result")  
}
Output : 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In the above code snippet :&lt;br&gt;
&lt;strong&gt;sqrt()&lt;/strong&gt; is a function defined in the library which returns the square root of a number.&lt;br&gt;
&lt;strong&gt;print()&lt;/strong&gt; function prints message to a standard output stream.&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;
&lt;h4&gt;User-Defined Functions:&lt;/h4&gt; User-defined functions are created by the user and can be used for advanced programming. Here functions are declared by using &lt;b&gt;fun&lt;/b&gt; keyword.&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(){
   functionName()
}
fun functionName(){
     //body of function
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Here, we call the function into the main function to run codes inside the body functionName().&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Template for function in Kotlin:
fun functionName(argument name:argument type):return type{
      //body of function
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Below is an example of a user-defined 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 main() {
  val x=6
  val y=8
  println("Sum of $x and $y is "+add(x,y))
}
fun add(x:Int, y:Int):Int{
    return x+y
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Here we create a function &lt;b&gt;add&lt;/b&gt; which is taking two arguments of int type and return type of this function is also int. We had called this function inside our main function.&lt;/p&gt;

&lt;h3&gt;Tail-recursion:&lt;/h3&gt;

&lt;p&gt;Kotlin supports a style of functional programming known as tail recursion. When we try to do a large number of recursive function call, we get an error &lt;b&gt;java.lang.StackOverflowError&lt;/b&gt;. To handle this Kotlin have tailrec function. When a function is marked with the &lt;b&gt;tailrec&lt;/b&gt; modifier and meets the required form, the compiler optimizes out the recursion, leaving behind a fast and efficient loop based version instead. Tail recursion follows one rule for implementation. This rule is as follow:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;The recursive call must be the last call of the method&lt;/b&gt;. To declare a recursion as tail recursion we need to use tailrec modifier before the recursive function.&lt;/p&gt;

&lt;p&gt;For example, to calculate the factorial of any number we can use the below method:&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(args: Array&amp;lt;String&amp;gt;) {  
    val number = 10  
    val result: Long  
    result = factorial(number)  
    println("Factorial of $number = $result")  
}  

tailrec fun factorial(n: Int, run: Int = 1): Long {  
    return if (n == 1){  
        run.toLong()  
    } else {  
        factorial(n-1, run*n)  
    }  
}
Output : 
Factorial of 10 = 3628800
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;h4&gt;Lambda Expressions:&lt;/h4&gt;

&lt;p&gt;Lambda is a function that has no name. Lambda is defined with curly braces {} which takes variable as a parameter (if any) and body of the function. The body of the function is written after the variable (if any) followed by the -&amp;gt; operator.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Syntax of lambda expression:
{ variable -&amp;gt; body_of_function}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;b&gt;Lambda expression syntax&lt;/b&gt;&lt;br&gt;
The full syntactic form of lambda expressions is as follows:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;val sum: (Int, Int) -&amp;gt; Int = { x: Int, y: Int -&amp;gt; x + y }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;A lambda expression is always surrounded by curly braces.&lt;/li&gt;
&lt;li&gt;Parameter declarations in the full syntactic form go inside curly braces and have optional type annotations.&lt;/li&gt;
&lt;li&gt;The body goes after an -&amp;gt; sign.&lt;/li&gt;
&lt;li&gt;If the inferred return type of the lambda is not Unit, the last (or possibly single) expression inside the lambda body is treated as the return value.&lt;/li&gt;
&lt;/ul&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Program for adding two numbers using lambda
fun main(args: Array&amp;lt;String&amp;gt;) {  
    println(sum(5,6))
}  
val sum = { x: Int, y: Int -&amp;gt; x + y }
Output : 11
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;Exception Handling in Kotlin:&lt;/h3&gt;

&lt;p&gt;Exception in programming is defined as a run time problem that occurs in a program and leads to the termination of the program. Exceptions can occur due to less memory space, array out of bound, conditions like division by zero. To solve this type of error in the program exception handling is used.&lt;/p&gt;

&lt;p&gt;Exception handling is defined as a process which handles the runtime problems and also maintains the flow of the program during execution.&lt;/p&gt;

&lt;p&gt;In Kotlin all exception classes are descendants of class Throwable. Kotlin uses the 'throw' expression to throw an exception object.&lt;/p&gt;

&lt;p&gt;There are four types of keywords used in exception handling. These are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;b&gt;try:&lt;/b&gt; try block contains a block of statements which might create the exception. It is always followed by either catch or finally or both.&lt;/li&gt;
&lt;li&gt;
&lt;b&gt;catch:&lt;/b&gt; It is used to catch an exception thrown from the try block.&lt;/li&gt;
&lt;li&gt;
&lt;b&gt;finally:&lt;/b&gt; It is used to checks whether the exception is handled or not.&lt;/li&gt;
&lt;li&gt;
&lt;b&gt;throw:&lt;/b&gt; It is used to throw an exception explicitly.&lt;/li&gt;
&lt;/ul&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Example of try-catch
fun main (args: Array&amp;lt;String&amp;gt;){  
    try {   
        val res =  9/ 0  

    } catch (e: ArithmeticException) {  
        println(e)  
    }
}
Output :
java.lang.ArithmeticException: / by zero
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can throw an exception explicitly by using the throw keyword.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Example of try-catch using throw keyword
fun main (args: Array&amp;lt;String&amp;gt;){  
    try {   
       println("Inside throw")
       throw Exception("Exception throwed")
} catch (e: Exception) {  
        println(e)  
    }
}
Output:
Inside throw 
java.lang.Exception: Exception throwed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it for this article. We will continue in the next article.&lt;/p&gt;

&lt;p&gt;Happy Learning!&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>programming</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Basics of Kotlin - Part 3</title>
      <dc:creator>Chetan</dc:creator>
      <pubDate>Sat, 24 Apr 2021 10:36:38 +0000</pubDate>
      <link>https://forem.com/chetansj27/basics-of-kotlin-part-3-1nia</link>
      <guid>https://forem.com/chetansj27/basics-of-kotlin-part-3-1nia</guid>
      <description>&lt;p&gt;In the last article, we learnt about some basic concepts of Kotlin like type casting, operators, how to take input and comments. Before reading this article make sure you had read the last article &lt;a href="https://dev.to/csj5483/basics-of-kotlin-part-2-51o3"&gt;Basics of Kotlin- Part 2&lt;/a&gt;. Let's learn more about Kotlin.&lt;/p&gt;

&lt;h4&gt;if expression:&lt;/h4&gt;

&lt;p&gt;In Kotlin, &lt;b&gt;if&lt;/b&gt; is an expression which returns a value. It is used to control the flow of the program structure.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;For example:
var a=7;
if(a%2==0){
    print("even number")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code snippet, the reminder of a with 2 is not equal to 0 so it will not execute the code of &lt;b&gt;if&lt;/b&gt; block and will not give any output.&lt;/p&gt;

&lt;h4&gt;if-else:&lt;/h4&gt;

&lt;p&gt;In &lt;b&gt;if-else&lt;/b&gt;, if the condition of &lt;b&gt;if&lt;/b&gt; expression is true then it will execute the code of the &lt;b&gt;if&lt;/b&gt; block otherwise it will execute the code of &lt;b&gt;else&lt;/b&gt; block.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var a=7;
 if(a%2==0){
     print("even number")
 }
 else{
    print("odd number")
 }
Output : odd number
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code snippet, the reminder of a with 2 is not equal to 0 so it will not execute the code of &lt;b&gt;if&lt;/b&gt; block and will execute the code of &lt;b&gt;else&lt;/b&gt; block.&lt;/p&gt;

&lt;h4&gt;if-else ladder:&lt;/h4&gt;

&lt;p&gt;In this, we will check multiple conditions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var num=7;
 if(num&amp;gt;0){
    print("positive number")
 }
 else if(num&amp;lt;0){
    print("negative number")
 }
 else{
    print("zero")
 }
Output : positive number
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;Nested if:&lt;/h4&gt;

&lt;p&gt;In this, we have conditions inside the block of a condition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;val num1=10
val num2=20
val num3=15
if(num1&amp;gt;num2){
   if(num1&amp;gt;num3){
        print("num1 is largest")
   }
   else{
      print("num3 is largest")
   }
}
else if(num2 &amp;gt; num3){  
   print("num2 is largest") 
}
else{  
   print("num3 is largest")
}
Output :
num2 is largest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code snippet will print the largest number of 3 numbers.&lt;/p&gt;

&lt;h4&gt;when expression:&lt;/h4&gt;

&lt;p&gt;&lt;b&gt;when&lt;/b&gt; expression in Kotlin is similar to &lt;b&gt;switch&lt;/b&gt; statement in Java/C++. It is basically a conditional operator in which multiple conditions can be applied on a particular variable. &lt;b&gt;when&lt;/b&gt; operator matches the variable value against the branch conditions. If it is satisfying the branch condition then it will execute the statement inside that scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;val num=3
    when(num){
        1 -&amp;gt; println("One")  
        2 -&amp;gt; println("Two")  
        3 -&amp;gt; println("Three")  
        4 -&amp;gt; println("Four")  
        5 -&amp;gt; println("Five")  
        else -&amp;gt; println("invalid number")  
    }
Output :
Three
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can have multiple statements enclosed within a block of condition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;val num=1
when(num){
     1-&amp;gt;{
       println("first")
       println("day")
    }
    else-&amp;gt;print("not first")
}
Output :
first
day
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can use multiple branches of condition separated with a comma. It is used, when we need to run the same logic for multiple choices.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var num=4
when(num){
  0,2,4,6,8-&amp;gt;println("even")
  1,3,5,7,9-&amp;gt;println("odd")
  else-&amp;gt;println("number greater than 9")
}
Output : even
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The when expression also checks the ranges of input provided in &lt;b&gt;when&lt;/b&gt; condition. A range is created using .. (double dot) operator. The in operator is used to check if a value belongs to a range.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var num=8
when(num){
   in 1..5-&amp;gt;print("num is from 1 to 5")
   in 6..10-&amp;gt;print("num is from 6 to 10")
   else-&amp;gt;print("num is greater than 10")
}
Output : num is from 6 to 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;for loop:&lt;/h4&gt;

&lt;p&gt;for loop is used to iterate through any kind of data structure. It iterates through arrays, ranges, collections, or anything which is provided for iteration. This is equivalent to the &lt;code&gt;foreach&lt;/code&gt; loop in languages like Java, C#.&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%2Fxheucz2qhoroj6917n0j.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%2Fxheucz2qhoroj6917n0j.png" alt="image"&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;var array=arrayOf(1,2,3,4,5)
for(item in array)
  println(item)
Output :
1
2
3
4
5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code snippet will print each item of the array.&lt;/p&gt;

&lt;p&gt;To iterate over a range of numbers, we can use a range expression:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for( i in 1..3)
    println(i)
Output :
1
2
3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can modify the for loop according to our requirements. For example, if we want to print the first five even number, then we can use the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for(i in 2 .. 10 step 2)
    println(i)
Output :
2
4
6
8
10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here step is the increment in the counter variable.&lt;/p&gt;

&lt;h4&gt;while loop:&lt;/h4&gt;

&lt;p&gt;while loop will continuously execute the body code while their condition is satisfied.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var num=5
 while(num&amp;gt;0){
      println(num)
      num--
 }
Output : 
5
4
3
2
1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;do-while loop:&lt;/h4&gt;

&lt;p&gt;do-while loop will continuously execute the body code while their condition is satisfied.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var num=10
do{
  println(num)
  num--
}while(num&amp;gt;5)
Output :
10
9
8
7
6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The difference between while and do-while is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;while&lt;/code&gt; checks the condition and, if it's satisfied, executes the body and then returns to the condition check.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;do-while&lt;/code&gt; executes the body and then checks the condition. If it's satisfied, the loop repeats. So, the body of do-while executes at least once regardless of the condition.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;Return and Jump:&lt;/h3&gt;

&lt;p&gt;Kotlin has three jump expressions.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;b&gt;return&lt;/b&gt; by default returns from the nearest enclosing function or anonymous function.&lt;/li&gt;
&lt;li&gt;
&lt;b&gt;break&lt;/b&gt; terminates the nearest enclosing loop.&lt;/li&gt;
&lt;li&gt;
&lt;b&gt;continue&lt;/b&gt; proceeds to the next step of the nearest enclosing loop.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;b&gt;return:&lt;/b&gt;&lt;br&gt;
It is a keyword that returns some value to the calling function from the called 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 main() {
    var x=5
    println(square(x))

}
fun square(x:Int):Int{
    return x*x
}
Output : 25
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, the square function will return the square value of the variable.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;break:&lt;/b&gt;&lt;br&gt;
break is used to terminate the controller flow. In the below example, for loop will terminate its loop when &lt;b&gt;if&lt;/b&gt; condition executes break expression.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for(i in 1..5){
      if(i==4){
          break
      }
      println(i)
}
Output :
1
2
3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see in the above code when i==4 become true loop gets terminated and we don't get any output after that.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;continue:&lt;/b&gt;&lt;br&gt;
continue is used to skip the current iteration of the loop and jumps the control to the end of the loop for the next iteration. It is generally used with if-else to skip the current iteration of the loop for a specific condition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for(i in 1..5){
        println("$i before continue")
        if(i==4)
         continue
        println("$i after continue") 
}
Output:
1 before continue
1 after continue
2 before continue
2 after continue 
3 before continue 
3 after continue 
4 before continue 
5 before continue 
5 after continue
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see for i=4 after continue statement doesn't get printed because continue had escaped the remaining code and the control goes to the end of the loop for the next iteration.&lt;/p&gt;

&lt;p&gt;That's it for this article. We will continue in the next article.&lt;/p&gt;

&lt;p&gt;Happy Learning!&lt;/p&gt;

</description>
      <category>android</category>
      <category>computerscience</category>
      <category>programming</category>
      <category>kotlin</category>
    </item>
    <item>
      <title>Basics of Kotlin - Part 2</title>
      <dc:creator>Chetan</dc:creator>
      <pubDate>Fri, 23 Apr 2021 18:31:28 +0000</pubDate>
      <link>https://forem.com/chetansj27/basics-of-kotlin-part-2-51o3</link>
      <guid>https://forem.com/chetansj27/basics-of-kotlin-part-2-51o3</guid>
      <description>&lt;p&gt;In the last article, we learnt about some basic concepts of Kotlin like what is Kotlin, features of Kotlin, how to declare variables and data types in Kotlin. Before reading this article make sure you had read the last article &lt;a href="https://dev.to/csj5483/basics-of-kotlin-part-1-jh"&gt;Basics of Kotlin- Part 1&lt;/a&gt;. Let's learn more about Kotlin.&lt;/p&gt;

&lt;h3&gt;Kotlin Type Conversion:&lt;/h3&gt;

&lt;p&gt;Type Conversion is a procedure of converting one type of data variable into another data type. It is also known as Type Casting. Unlike Java, Kotlin does not support the implicit conversion of a smaller data type into a larger data type. It means we can not assign Int into Long or Double.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;In Java:&lt;/b&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int a=10;
long b=a; //no error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;b&gt;In Kotlin:&lt;/b&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 a=10;
var b:Long=a //compiler error Type mismatch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, we can do the explicit conversion with the help of predefined helper functions. The list of helper functions used for numeric conversion in Kotlin is given below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;toInt()&lt;/li&gt;
&lt;li&gt;toByte()&lt;/li&gt;
&lt;li&gt;toShort()&lt;/li&gt;
&lt;li&gt;toChar()&lt;/li&gt;
&lt;li&gt;toLong()&lt;/li&gt;
&lt;li&gt;toFloat()&lt;/li&gt;
&lt;li&gt;toDouble()&lt;/li&gt;
&lt;/ul&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;For example:
var a=10;
var b:Long=a.toLong() //compiles successfully no error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;Operators:&lt;/h3&gt;

&lt;p&gt;Operators are the special characters or symbols which performs the operation on operand(value or variable). There is various kind of operators in Kotlin:&lt;/p&gt;

&lt;h4&gt;Arithmetic Operators:&lt;/h4&gt;

&lt;p&gt;Arithmetic operators are used to perform basic mathematical operations such as addition (+), subtraction (-), multiplication (*), division (/) and modulo(%)&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%2Fljb7lj8rmqfo87w69nhl.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%2Fljb7lj8rmqfo87w69nhl.png" alt="image"&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;Example:
var a=11
var b=5
print(a+b) // Output : 16
print(a-b) // Output : 6
print(a*b) // Output : 55
print(a/b) // Output : 2
print(a%b) // Output : 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;Relation Operators:&lt;/h4&gt;

&lt;p&gt;Relation Operator is used to compare two values, two variables or two numbers. It always gives output as true or false.&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%2Fsagaqo96zxzxq7lhhxkc.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%2Fsagaqo96zxzxq7lhhxkc.png" alt="image"&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;For example:
 var a=11
 var b=5
 print(a&amp;lt;b) //Output : false
 print(a&amp;gt;b) //Output : true
 print(a&amp;lt;=b) //Output : false
 print(a&amp;gt;=b) //Output : true
 print(a==b) //Output : false
 print(a!=b) //Output : true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;Assignment Operators:&lt;/h4&gt;

&lt;p&gt;Assignment Operators are used to assigning the arithmetic operated values. The assignment of value takes from right to left.&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%2F2rf30o41852qjqxlizyj.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%2F2rf30o41852qjqxlizyj.png" alt="image"&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;For example:
var x = 10
var y = 2
var x + = y // Output : 12
Var x - = y // Output : 8
Var x * = y // Output : 20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;Unary Operators:&lt;/h4&gt;

&lt;p&gt;Unary operators are used with only a single operand. &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%2Fl8kjmynun11bzt3tdwjs.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%2Fl8kjmynun11bzt3tdwjs.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In pre increment/decrement the value will be updated before assigning to the variable and in post increment/decrement the value will be updated after assigning to the variable&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var a=10
print(a++) //Output : 10
print(a) //Output : 11
print(++a) //Output : 12
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;Logical Operators:&lt;/h4&gt;

&lt;p&gt;Logical Operators are used to check conditions between operands.&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%2Fu4xszlthz9dlg9y2lwzl.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%2Fu4xszlthz9dlg9y2lwzl.png" alt="image"&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;For example
var a=10
var b=5
var c=16
print((a&amp;gt;b)&amp;amp;&amp;amp;(a&amp;gt;c)) //Outout : false
print((a&amp;gt;b)||(a&amp;gt;c)) //Output : true
var flag=true
print(!flag) //Output : false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;Input in Kotlin:&lt;/h4&gt;

&lt;p&gt;Kotlin uses the standard library function &lt;b&gt;readLine()&lt;/b&gt; which read the line of string input from the standard input stream.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var name=readLine() //For string input
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we want the input of other data types, then we can use Scanner object. For that, we need to import Scanner class from Java standard library using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.Scanner
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, we need to create Scannerobject from this class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;val scannerObj= Scanner(System.`in`)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can use this scannerObj for taking the input from the user&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var name=scannerObj.next() // For String input
var age = scannerObj.nextInt() // For Integer input
var charInput=scannerObj.next().single() //For char input
var doubleInput=scannerObj.nextDouble() //For double input
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;Comments in Kotlin:&lt;/h4&gt;

&lt;p&gt;Comments are used for making the source code easier for humans to understand. They provide explanatory information about the source code. Comments are ignored by the compiler so they will not execute. Kotlin has two types of comments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Single Line: Single line comment is used for commenting single line of the statement.&lt;/li&gt;
&lt;li&gt;Multi-line: Multi-line comment is used for commenting multiple lines of the statement.&lt;/li&gt;
&lt;/ul&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//This is single line comment
/* This is
   multiline
   comment */
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it for this article. We will continue in the next article.&lt;br&gt;
Happy Learning! &lt;/p&gt;

</description>
      <category>android</category>
      <category>programming</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Basics of Kotlin - Part 1</title>
      <dc:creator>Chetan</dc:creator>
      <pubDate>Thu, 22 Apr 2021 10:31:04 +0000</pubDate>
      <link>https://forem.com/chetansj27/basics-of-kotlin-part-1-jh</link>
      <guid>https://forem.com/chetansj27/basics-of-kotlin-part-1-jh</guid>
      <description>&lt;p&gt;In the last article, we try to learn about Gradle and App Folder. If you haven't read the previous article, visit this link and read it&lt;br&gt;
&lt;a href="https://dev.to/csj5483/exploring-the-android-studio-part-1-1d2m"&gt;Exploring the Android Studio&lt;/a&gt;&lt;br&gt;
Before we start working on Android, let's learn about Kotlin first. In the next few articles, we will try to learn the basics of Kotlin like the loop, conditional statements, function, class.&lt;/p&gt;

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

&lt;p&gt;Kotlin is an open-source, statically typed and general-purpose language that runs on Java Virtual Machine(JVM). It was developed by JetBrains Team, the official designer of the most intelligent Java IDE, named Intellij IDEA. It was officially released in February 2016 and in 2017 Google announced it as the official language for Android Development. It combines the features of Object-Oriented Programming (OOPs) and functional programming into a unique platform.&lt;/p&gt;

&lt;h4&gt;Features of Kotlin:&lt;/h4&gt;

&lt;p&gt;&lt;b&gt;Concise:&lt;/b&gt; Kotlin is the language where lines of code can be reduced up to 40%. This makes Kotlin an ideal choice for software development.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Null Safety:&lt;/b&gt; Kotlin aimed to eliminate the NullPointerException (null reference) from the code. Kotlin system can easily distinguish between null references and not null references. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var name:String="Aman"
name=null //This is will give an error 
To permit null value we can declare it as:
var name:String?="Aman"
name=null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;b&gt;Interoperable:&lt;/b&gt; Kotlin is fully compatible with Java Code which means existing Java code can be naturally called from Kotlin, and Kotlin code can be used from Java.&lt;br&gt;
&lt;b&gt;Fast Compilation:&lt;/b&gt; It is easy to compile the Kotlin code which results in better performance and fast compilation time.&lt;br&gt;
&lt;b&gt;Smart cast:&lt;/b&gt; The Kotlin compiler automatically converts the variable to a particular class reference once it's passed through any conditional operator.&lt;br&gt;
&lt;b&gt;Extension function:&lt;/b&gt; Kotlin supports a variety of extension function which means it helps to extend the functionality of classes without touching their code.&lt;/p&gt;

&lt;h4&gt;Variables:&lt;/h4&gt;

&lt;p&gt;Variables are the name we give to computer memory location. These are used to store and manipulate the data. We can declare a variable by using &lt;b&gt;var&lt;/b&gt; and &lt;b&gt;val&lt;/b&gt; keyword.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var name="aman" //string
val age=10 //integer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also explicitly specify the datatype of the variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var name:String="aman" //string
val age:Int=10 //integer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;b&gt;var:&lt;/b&gt; We can change the value of the variable declared using var keyword. Therefore it is known as a &lt;b&gt;mutable variable&lt;/b&gt;. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var age:Int=8
age=10
print(age)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will give 10 as output&lt;/p&gt;

&lt;p&gt;&lt;b&gt;val:&lt;/b&gt; We can not change the value of the variable declared using val keyword. Therefore it is known as an &lt;b&gt;immutable variable&lt;/b&gt;. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;val age:Int=8
age=10
print(age)
This will give an error(Val cannot be reassigned)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;Data Types in Kotlin:&lt;/h4&gt;

&lt;p&gt;Data types are set of relatable values and describe the operations that can be operated on them. It refers to the type and size of data associated with variables and functions. In Kotlin every data type is considered as an object.&lt;br&gt;
Kotlin has the following categories of data type:&lt;br&gt;
&lt;b&gt;Number:&lt;/b&gt; &lt;br&gt;
This data type holds only number type data variable. It is further sub-categorised as Integers and Floating-Point Numbers.&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;
&lt;b&gt;Integers:&lt;/b&gt; It contains numeric value without decimal. It has four types:&lt;/li&gt;&lt;/ul&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%2F53hjb8q6u44c1y9kq7ct.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%2F53hjb8q6u44c1y9kq7ct.png" alt="image"&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;var byteVar:Byte=29 //byte data type
var shortVar:Short=323 // short data type
var intVar:Int=40000 //int data type
var longVar:Long=8673463 // long data type
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;&lt;li&gt;
&lt;b&gt;Floating point:&lt;/b&gt; It contains non Integer numbers that carry some decimal values. It has two types- &lt;b&gt;Float&lt;/b&gt; which has a 32-bit single-precision floating-point value and &lt;b&gt;Double&lt;/b&gt; which has a 64-bit single-precision floating-point value.
&lt;/li&gt;&lt;/ul&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var floatVar:Float=29.0f // float data type
var doubleVar:Double=8450.445 //double data type
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;b&gt;Boolean:&lt;/b&gt;&lt;br&gt;
It represents the logical value. It contains either true or false.&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%2Fmfslab0sshmt356evz6g.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%2Fmfslab0sshmt356evz6g.png" alt="image"&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;var boolVar:Boolean=true //boolean data type
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;b&gt;Char:&lt;/b&gt;&lt;br&gt;
Character in Kotlin is represented with the help of &lt;b&gt;char&lt;/b&gt; keyword. Character can be declared using a single quote &lt;b&gt;'c'&lt;/b&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%2Fvwbb415oxhnfddgwdk7v.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%2Fvwbb415oxhnfddgwdk7v.png" alt="image"&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;var charVar:Char='c' //char data type
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;b&gt;String:&lt;/b&gt;&lt;br&gt;
Strings are the arrays of character means a string will contain one or more than one characters. Strings in Kotlin are immutable, which means we cannot change the elements in String. Kotlin has two kinds of string &lt;b&gt;raw string&lt;/b&gt; and &lt;b&gt;escaped string&lt;/b&gt;. Escaped String allows providing extra line space after the first print statement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var escapedString:String="first name \n" //escapedString
var rawString:String="last name" //rawString
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;b&gt;Arrays:&lt;/b&gt;&lt;br&gt;
Arrays are used to store homogenous data. We can create arrays of different data types like an array to store int value and another array to store string value. Arrays are represented by &lt;b&gt;Array&lt;/b&gt; class. We can create array using library function arrayOf() and Array() constructor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Differents approach to declare array
val stringArray=arrayOf("name","age")
val stringArray=arrayOf&amp;lt;String&amp;gt;("name","age")
val stringArray=Array(2){"name";"age"}
val stringArray=Array&amp;lt;String&amp;gt;(2){"name";"age"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can access elements of an array using the index. For example, if we want to access the first element then we can access it with 0 index.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;val stringArray=Array&amp;lt;String&amp;gt;(3){"first";"second";"third";}
print(stringArray[1])
This will third as output
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Array index starts from 0. So if we are accessing the first element then we have to use the index 0.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's it for this article. We will continue in the next article.&lt;br&gt;
Happy Learning!!&lt;/p&gt;

</description>
      <category>android</category>
      <category>computerscience</category>
      <category>programming</category>
    </item>
    <item>
      <title>Basic concepts of Android-Part 1</title>
      <dc:creator>Chetan</dc:creator>
      <pubDate>Wed, 21 Apr 2021 11:23:40 +0000</pubDate>
      <link>https://forem.com/chetansj27/basic-concepts-of-android-part-1-1eb7</link>
      <guid>https://forem.com/chetansj27/basic-concepts-of-android-part-1-1eb7</guid>
      <description>&lt;p&gt;In this article, we are going to discuss some concepts of Android that you might face in your interview or during the project.&lt;/p&gt;

&lt;h2&gt;Package Name:&lt;/h2&gt;

&lt;p&gt;The package name is used for unique identification for your application.&lt;br&gt;
Android uses the package name to determine if the application has been installed or not. Generally, the package name of an app is in the format&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;domian.company.appname
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here domain portion is the domain extension like .com, .org, or .eu. The company portion is generally the name of the developer's company and appname describe the app itself. Appname could be of a single word or multiple words separated by a dot. For example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;com.google.android.apps.photos
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code snippet:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;com is the domain&lt;/li&gt;
&lt;li&gt;google is the developer's company name&lt;/li&gt;
&lt;li&gt;android.app.photos is the app name&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Minimum SDK:&lt;/h2&gt;

&lt;p&gt;It is an integer that defines the minimum API level(version of the Android operating system) required to run your application. If the system's API Level is lower than the value specified in this attribute, then the Android system will prevent the user from installing the application.&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%2Fin0mcpfl9ijq8cqgk85u.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%2Fin0mcpfl9ijq8cqgk85u.png" alt="image"&gt;&lt;/a&gt; &lt;br&gt;
In the above image, we had selected the Minimum SDK as 27 and Android Studio shows us that it will run on 53.5% of devices.&lt;/p&gt;

&lt;h2&gt;Activity:&lt;/h2&gt;

&lt;p&gt;Activity class is one of the most important parts of Android. No matter how small an app is, it will have at least an Activity. An Android activity is one screen of the Android app's user interface. An app can have multiple activities means multiple screens. Android activity is the subclass of ContextThemeWrapper class. &lt;br&gt;
Every activity contains a layout, which has a user interface to interact with the user. In this layout, we define what a user will see when (s)he open this activity and in Java/Kotlin we define the functionality of the activity.&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%2Fobip7fdtkg1vsahgf8lj.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%2Fobip7fdtkg1vsahgf8lj.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above image, we had setContentView method and in this, we had set R.layout.activity_main as the layout for this activity.&lt;/p&gt;

&lt;h5&gt;Activity Life Cycle:&lt;/h5&gt;

&lt;p&gt;Unlike other programming languages where &lt;strong&gt;main()&lt;/strong&gt; method is the entry point of the program, the android operating system initiates the code in an Activity instance by invoking specific callback methods that correspond to specific stages of its lifecycle. In the below diagram we can see the life cycle of an Activity.&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%2Fn6rcpidtoq70hlawcodk.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%2Fn6rcpidtoq70hlawcodk.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;b&gt;onCreate()-&lt;/b&gt; This is the first callback and called when the activity is first created.&lt;/li&gt;
&lt;li&gt;
&lt;b&gt;onStart()-&lt;/b&gt; This is called when the activity becomes visible to the user.&lt;/li&gt;
&lt;li&gt;
&lt;b&gt;onResume()-&lt;/b&gt; This is called when the user starts interacting with the application.&lt;/li&gt;
&lt;li&gt;
&lt;b&gt;onPause()-&lt;/b&gt; The paused activity does not receive user input and cannot execute any code and called when the current activity is being paused and the previous activity is being resumed.&lt;/li&gt;
&lt;li&gt;
&lt;b&gt;onStop()-&lt;/b&gt; This callback is called when the activity is no longer visible.&lt;/li&gt;
&lt;li&gt;
&lt;b&gt;onDestroy()-&lt;/b&gt; This callback is called before the activity is destroyed by the system.&lt;/li&gt;
&lt;li&gt;
&lt;b&gt;onRestart()-&lt;/b&gt; This callback is called when the activity restarts after stopping it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Service:&lt;/h2&gt;

&lt;p&gt;A service is a component that runs in the background to perform long-running operations such as playing music, handle network transactions, interacting with content providers without needing to interact with the user and it works even if the application is destroyed. It doesn't have any UI (user interface). The android.app.Service is a subclass of ContextWrapper class.&lt;/p&gt;

&lt;p&gt;A service can essentially take two states:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;b&gt;Started:&lt;/b&gt; A service is started when a component (like activity) calls the startService() method, now it runs in the background indefinitely. It is stopped by the stopService() method. The service can stop itself by calling the stopSelf() method.&lt;/li&gt;

&lt;li&gt;
&lt;b&gt;Bound:&lt;/b&gt; A service is bound when another component (e.g. client) calls the bindService() method. A bound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC). The client can unbind the service by calling the unbindService() method.&lt;/li&gt;
&lt;/ul&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%2Fjbkl0kf57nzq3i23077s.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%2Fjbkl0kf57nzq3i23077s.png" alt="image"&gt;&lt;/a&gt;&lt;br&gt;
 While creating a service following are the most important callback methods that you should override:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;b&gt;onStartCommand()-&lt;/b&gt; The system calls this method when another component, such as an activity, requests that the service be started, by invoking startService(). When this method executes, the service is started and can run in the background indefinitely. If you implement make sure to stop the service when its work is done, by calling stopSelf() or stopService() methods.&lt;/li&gt;
&lt;li&gt;
&lt;b&gt;onBind()-&lt;/b&gt; The system calls this method when another component wants to bind with the service by calling bindService(). If you implement this method, you must provide an interface that clients use to communicate with the service, by returning an IBinder object. You must always implement this method, but if you don't want to allow binding, then you should return null.
&lt;/li&gt;
&lt;li&gt;
&lt;b&gt;onUnbind()-&lt;/b&gt; The system calls this method when all clients have disconnected from a particular interface published by the service.&lt;/li&gt;
&lt;li&gt;
&lt;b&gt;onRebind()-&lt;/b&gt; The system calls this method when new clients have connected to the service after it had previously been notified that all had disconnected in its onUnbind(Intent).&lt;/li&gt;
&lt;li&gt;
&lt;b&gt;onCreate()-&lt;/b&gt; The system calls this method when the service is first created using onStartCommand() or onBind(). This call is required to perform a one-time set-up.&lt;/li&gt;
&lt;li&gt;
&lt;b&gt;onDestroy()-&lt;/b&gt; The system calls this method when the service is no longer used and is being destroyed. Your service should implement this to clean up any resources such as threads, registered listeners, receivers, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will continue this in the next article.&lt;br&gt;
Happy Learning!&lt;/p&gt;

</description>
      <category>android</category>
      <category>computerscience</category>
      <category>programming</category>
      <category>devops</category>
    </item>
    <item>
      <title>Exploring the Android Studio</title>
      <dc:creator>Chetan</dc:creator>
      <pubDate>Tue, 20 Apr 2021 06:43:08 +0000</pubDate>
      <link>https://forem.com/chetansj27/exploring-the-android-studio-part-1-1d2m</link>
      <guid>https://forem.com/chetansj27/exploring-the-android-studio-part-1-1d2m</guid>
      <description>&lt;p&gt;In the last article, we learn &lt;b&gt;&lt;a href="https://dev.to/csj5483/how-to-create-a-project-in-android-studio-1a0c"&gt;How to create a project in Android Studio&lt;/a&gt;&lt;/b&gt; and in this article we are going to explore the Android Studio.&lt;/p&gt;

&lt;p&gt;After creating the project this screen will appear in Android Studio&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VcCSBa-G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vwpk8si7ipc8ci75ve1f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VcCSBa-G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vwpk8si7ipc8ci75ve1f.png" alt="Exploring the Android Studio"&gt;&lt;/a&gt;&lt;br&gt;
In this article, we will try to learn about the App folder and Gradle Scripts&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TWkn4ILj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sc91iknb2224lkrugqt0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TWkn4ILj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sc91iknb2224lkrugqt0.png" alt="Exploring the Android Studio"&gt;&lt;/a&gt;&lt;br&gt;
As we can see in the above image there is &lt;b&gt;manifests&lt;/b&gt; folder, inside this folder there is a file &lt;b&gt;AndroidManifests.xml&lt;/b&gt;. When we click on this file, we will see something like this on our screen&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VLlthECV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qy7j6w54v7bil112vlco.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VLlthECV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qy7j6w54v7bil112vlco.png" alt="Exploring the Android Studio"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;AndroidManifests.xml:&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;This file describes essential information about our app to Operating System and Gooogle Play.&lt;/li&gt;
&lt;li&gt;Manifest file is required to declare the app's package name
&lt;/li&gt;
&lt;li&gt;It is required to declare the component of the app like:
&lt;ul&gt;
&lt;li&gt;activity&lt;/li&gt;
&lt;li&gt;service&lt;/li&gt;
&lt;li&gt;receiver&lt;/li&gt;
&lt;li&gt;provider&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DPb8LO9e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lm9i9fq77554fl6fw7me.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DPb8LO9e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lm9i9fq77554fl6fw7me.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;/li&gt;

&lt;li&gt;This is the file where we have to add all uses-permission that we are using in our app. For example, if we want to read the external storage of use, then we have to add the following line in our AndroidManifests.xml file,
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--COMJkZVi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/miuy5bxaqgn08wnmh1r1.png" alt="Exploring the Android Studio"&gt;
If we want to access other things, we have to follow the same process.
&lt;/li&gt;


&lt;li&gt;We can change the launcher icon of our app from this file.
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qmuRgiHj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6vgr3m3hc7h36an2miiy.png" alt="Alt Text"&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;b&gt;android:roundIcon&lt;/b&gt; will be used on a device that uses roundIcon otherwise &lt;strong&gt;android:icon&lt;/strong&gt; will be used&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;
&lt;/li&gt;




&lt;li&gt;In this file we declare what types of Hardware or Software feature our app requires. For example, if our app requires Bluetooth and without it, our app will not work, then we can declare this in the below way:
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WhZska6Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v6dxptphvud47431oons.png" alt="Alt Text"&gt;
&lt;/li&gt;

&lt;li&gt;We can define the intent-filter in this file.

&lt;blockquote&gt;
&lt;p&gt;App activities, services, and broadcast receivers are activated by intents.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For example, we can change what activity will open when our app will open means we can change the launcher activity.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--caAD2a54--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p7qb66l21s0fewgpnrif.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--caAD2a54--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p7qb66l21s0fewgpnrif.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
In the above image, we had chosen SecondActivity as our launcher activity.&lt;/p&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;b&gt;Java Folder&lt;/b&gt; :&lt;br&gt;
In this folder, we will write the code for our apps like what will happen when we click on the button and all.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jx65lhjf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o65rr04dmalv0arecmlo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jx65lhjf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o65rr04dmalv0arecmlo.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
For example, in the above image, we have the file MainActivity. This file will handle the functionality of the MainActivity Screen.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;res folder&lt;/b&gt;:&lt;br&gt;
This folder is used to store the value of resources that we are using in our project like colors, strings, layout, styles and drawable.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FJYVWG6B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/64n4xbxmx96pywnmvi2c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FJYVWG6B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/64n4xbxmx96pywnmvi2c.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;b&gt;drawable:&lt;/b&gt; This folder contains the different types of images used for the development of the application. It also contains the XML drawable which are used to describe shapes (color, border, gradient), state, transitions and more.

&lt;/li&gt;

&lt;li&gt;
&lt;b&gt;layout:&lt;/b&gt; It contains the XML code of all layouts of our project.
&lt;/li&gt;

&lt;li&gt;
&lt;b&gt;mipmap:&lt;/b&gt; It contains our app/launcher icons (which are shown on the home screen).
&amp;gt;It’s best practice to place our app icons in mipmap- folders (not the drawable- folders) because they are used at resolutions different from the device’s current density.
&lt;/li&gt;
&lt;li&gt;
&lt;b&gt;values:&lt;/b&gt; This folder contains the value of color, strings and style.
&lt;ul&gt;
&lt;li&gt;
&lt;b&gt;colors.xml:&lt;/b&gt;
The colors.xml is an XML file that is used to store the colors for the resources. We can use these colors throughout our app.

&lt;/li&gt;
&lt;li&gt;
&lt;b&gt;strings.xml:&lt;/b&gt; It is used todefine the strings in one file so that it is easy to use same string in different positions in the android project&lt;/li&gt;
&lt;li&gt;
&lt;b&gt;styles.xml:&lt;/b&gt; In this file all the themes of android project are defined.&lt;/li&gt;
&lt;/ul&gt;




&lt;/li&gt;

&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--e1zqSf0p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8pr907m8qcjn7u9hunm3.png" alt="image"&gt;
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hIR9vkSA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ktsfh1cxpy8ltutp9ek.png" alt="Alt Text"&gt;
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kRrM-Kbb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wdi7eiinpe631pn8lkmj.png" alt="Alt Text"&gt;




&lt;/ul&gt;

&lt;h4&gt;Gradle Script&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pKdHwvdM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rugsdreyz4c8bl9sqtb0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pKdHwvdM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rugsdreyz4c8bl9sqtb0.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
&lt;b&gt;build.gradle(Project:applicationName)&lt;/b&gt; This file is in the root folder of the project and its configuration settings apply to every module in the project. A module is an isolated piece of the bigger project. In a multi-module project, these modules have their jobs but work together to form the whole project. Most Android projects only have one module, the app module.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--t2VciOil--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/58k51s2635ub4tl2mxgi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--t2VciOil--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/58k51s2635ub4tl2mxgi.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;build.gradle(Module:applicationName.app):&lt;/b&gt;: The file here is in the app folder. Its build settings apply only to the app module. If there were another module, then that module would have its build.gradle file, too.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mwfkJ6WY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ais9gmpbmkin4xk9x8g0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mwfkJ6WY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ais9gmpbmkin4xk9x8g0.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
In this file we can define:&lt;/p&gt;
&lt;ul&gt;

&lt;li&gt;
&lt;b&gt;applicationId:&lt;/b&gt; This is the package name of our app&lt;/li&gt;

&lt;li&gt;
&lt;b&gt;minSdkVersion:&lt;/b&gt; It specifies the minimum API Level on which the application is able to run.&lt;/li&gt;

&lt;li&gt;
&lt;b&gt;targetSdkVersion:&lt;/b&gt; It specifies the API Level on which the application is designed to run&lt;/li&gt;

&lt;li&gt;
&lt;b&gt;versionCode:&lt;/b&gt; It's a positive integer that's used for comparison with other version codes. It's not shown to the user, it's just for record-keeping in a way.&lt;/li&gt;

&lt;li&gt;
&lt;b&gt;versionName:&lt;/b&gt; This is the version string seen by the user. It isn't used for internal comparisons or anything, it's just for users to see.&lt;/li&gt;

&lt;li&gt;
&lt;b&gt;dependencies:&lt;/b&gt; Dependencies refer to the things that supports in building your project, such as required JAR file from other projects and external JARs like JDBC JAR or Eh-cache JAR in the class path&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Happy Learning!&lt;/p&gt;

</description>
      <category>android</category>
      <category>programming</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>How to create a project in Android Studio</title>
      <dc:creator>Chetan</dc:creator>
      <pubDate>Sat, 17 Apr 2021 18:34:17 +0000</pubDate>
      <link>https://forem.com/chetansj27/how-to-create-a-project-in-android-studio-1a0c</link>
      <guid>https://forem.com/chetansj27/how-to-create-a-project-in-android-studio-1a0c</guid>
      <description>&lt;p&gt;In this article, you are going to learn How to create a project in Android Studio.&lt;br&gt;
I assume that you have Android Studio installed on your device.&lt;br&gt;
In the first step, you have to open Android Studio and click on &lt;b&gt;Create New Project&lt;/b&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--l5QuMTbL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/envot7zp5ofhm801lutr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--l5QuMTbL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/envot7zp5ofhm801lutr.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
After doing this, you have to select a project template for our app. There are many project templates in Android Studio like:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;No Activity&lt;/b&gt;- It will create empty projects means you have to add the MainActivity by yourself.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Basic Activity&lt;/b&gt;- It will create a new basic activity with Navigation Component.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Bottom Navigation Activity&lt;/b&gt;- It will create a new activity with bottom navigation.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Empty Activity&lt;/b&gt;- It will create a new empty activity. Most of the time you will use this activity.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Navigation Drawer Activity&lt;/b&gt;- This will create a new activity with navigation activity.&lt;/p&gt;

&lt;p&gt;Apart from the above templates, there are also other templates available in Android Studio.&lt;br&gt;
You can know the use of each template by just clicking on them.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qqRdlF-t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eum06koe782mzqex2o3i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qqRdlF-t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eum06koe782mzqex2o3i.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
After selecting the required template click on the Next button, then a &lt;b&gt;Configure Your Project&lt;/b&gt; screen will open.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wC1gm39P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ik3smgqeouvw35vh3xx5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wC1gm39P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ik3smgqeouvw35vh3xx5.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
From this You can change:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Project Name&lt;/b&gt;- This will the name that you want to give your project&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Package Name&lt;/b&gt;- It will uniquely identify the app on the device. Make sure to change your package name. Your package name shouldn't be like com.example.appname because Google will not allow you to publish the app with such package names.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Save location&lt;/b&gt;- This is the location where you want to save your project.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Language&lt;/b&gt;- The language in which you want to write the code for the app. Java and Kotlin are the two available options.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Minimum  SDK&lt;/b&gt;- This the minimum version of Android that your app can run on. It will show for the specific version, your app will run on what % of devices like&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8KZ6f37G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gvu6rpsrg7r2g5u8gbgt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8KZ6f37G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gvu6rpsrg7r2g5u8gbgt.png" alt="Alt Text"&gt;&lt;/a&gt;for Android 8.1 it will run on 53.5% of devices&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pyC7wi4x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gatyp14zh6nspow7hu0z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pyC7wi4x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gatyp14zh6nspow7hu0z.png" alt="Alt Text"&gt;&lt;/a&gt;and for Android 4.1 it will run on 99.8% of devices.&lt;/p&gt;

&lt;p&gt;After filling all the fields click on the finish and your project will be created.&lt;/p&gt;

&lt;p&gt;Happy Learning!&lt;/p&gt;

</description>
      <category>android</category>
      <category>project</category>
      <category>app</category>
    </item>
  </channel>
</rss>
