DEV Community

Cover image for Kotlin Basics
Amandeep Singh
Amandeep Singh

Posted on • Updated on

Kotlin Basics

In this Kotlin Basics series ,we will cover :-
Operators
Data types
Variables
Conditionals
Lists and arrays
Null safety

Today's Content:

  • Creating a new project in Android Studio
  • Hello world in Kotlin
  • Variables in Kotlin
  • Datatypes in Kotlin

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.

Lets get started...
Open Android Studio and select new project.
Select No Activity and click Next
Starting new project in Android Studio
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.
Explaining new project window in Kotlin
1: 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

2: 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. "

3: 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.

4: This is a language selector with two options: Java or Kotlin. In this course we use Kotlin.

5: 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.
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.

Now, lets dive in and select Finish.
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.

I have a different theme applied but your workspace should look more or less like this.
Right-Click on com.example.kotlin_baiscs_01 folder then go to
new->Kotlin/class file then give your file a name while the file section is selected and then press Enter.
Image description
Now your window should look like this
Image description
HELLO WORLD
lets print hello world in Kotlin. First we need to create a main function
fun main(){
println("hello world")
}

Now, there are many ways to run your code as pointed in the image
printing hello world in Kotlin
It is necessary to have the fun main() 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.

VARIABLES IN KOTLIN

Variables are simply containers for storing values(data values). To create a variable we use var or val. 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
val name= "Amandeeep"
val age= 18

we have the name "Amandeep" in a variable called name and age in a variable called "age".

DATATYPES IN KOTLIN

Image description

These are types of DataTypes that we have in Kotlin

  • Integer types: 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 ```

val one = 1 // Int
val threeBillion = 3000000000 // Long
val oneByte = 1

And if we need to specify our datatype then we can do it as the following :
Enter fullscreen mode Exit fullscreen mode

val one: Int = 1
val 3billion : Long = 3000000000
val oneByte : Byte =1

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:**<br/>
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:**<br/>
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.
Enter fullscreen mode Exit fullscreen mode

val kotlinIsFun: Boolean = true
val fishIsTasty: Boolean = false

println(kotlinIsFun) // Outputs true
println(fishIsTasty) // Outputs false


- **Character Data type:**<br/>
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'.
Enter fullscreen mode Exit fullscreen mode

val myGrade: Char = 'A'
println(myGrade) // output: A

- **String Data type:**<br/>
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)







Enter fullscreen mode Exit fullscreen mode

Top comments (0)