Kotlin Tutorial: Basics learning on operators and types | Kotlin programming language | Part 1
Kotlin Tutorial: Basics learning on operators and types | Kotlin programming language | Part 1

Introduction

In this Kotlin Tutorial: Basics learning on operators and types, you will learn about operators and types in the Kotlin programming language. In this part, we will explore numeric operators, usage of variables, types, and learn about strings.

You will get the full benefits when you go through the tutorial in sequence. Reference on Kotlin Basics.

Related Post:

Prerequisite

You should be familiar with:

  • How to create a project using IntelliJ IDEA
  • How to work with the Kotlin REPL (Read-Eval-Print Loop) interactive shell.

Learn on how to install IntelliJ IDEA IDE and create project.

What you'll learn

  • Learn about operators and types.
  • Practice with sample example

Kotlin Basics: Operators and Types

In Kotlin programming language, we will look at the operators and types with examples for better understanding.

  1. Open IntelliJ IDEA.
  2. Select Tools > Kotlin > Kotlin REPL and open the console.

Numeric operators

Like other programming languages, a numeric operator like +, -, *, and / are for plus, minus, multiplication, and division used by Kotlin. Kotlin also supports numeric types like Byte, Short, Int, Double, Long, Float.

kotlin numeric types

Good to know:

If a variable initialized value is more than Int then the type is Long. To specify the Long value explicitly, append the suffix L to the value.

Examples:

Enter the following expressions in the REPL. To see the result, press Control+Enter (Command+Enter on a Mac) after each one.  indicates output from your code.

Now, let's try different expressions for integer and double.

Kotlin Tutorial: Basics learning on operators and types

Now, let's call some methods on the numbers. Kotlin keeps numbers as primitives, but it lets you call methods on numbers as if they were objects.

Underscore usage on numbers

You can use underscores to make number constants more readable.

val oneThousand = 1_000
val oneHundredThousand = 1_00_000
val oneMillion = 1_000_000
val creditCardNumber = 9874_5487_5552_1154L
val socialSecurityNumber = 999_99_9999L
val hexBytes = 0xFF_EC_DE_5E
val bytes = 0b11010010_01101001_10010100_10010010

Usage of Types

In Kotlin, there is no implicit conversion in Kotlin, meaning a Int the variable cannot be assigned to the variable directly. You can always assign values of different types by casting.

Let's take a look at some examples. Declare variable with Int type and try to assign the value to a different variable type.

operators and types | Kotlin programming language
operators and types | Kotlin programming language

Variable Types

There are two types of variable in Kotlin which are var and val. With var, you can assign a value, then change the value later in the program. With, you can assign a value once. If you try to assign something again, you get an error.

var num = 1
 num = 4
 val pi = 3.14
 pi= 6.28 // this will throw error
val and var example in kotlin programming language

About Strings

Strings works almost same as other language. using " for strings and ' for single characters, and you can concatenate strings with the + operator.

var animalName = "lion" + " tiger"
println(animalName)
lion tiger //this is the output

Create a string template with an expression in it. As in other languages, the value can be the result of an expression. Use curly braces {} to define the expression.

 val i = 5
 val j = 2
 var animalName = "There are ${i} lions" + " and ${j} tigers in this zoo"
 println(animalName)
There are 5 lions and 2 tigers in this zoo // output

usage of string in kotlin

Congratulations! Kotlin Tutorial: Basics learning on operators and types part 1 is now completed.

Conclusion

In this post, you have learned about operators and types and various ways of using operators.

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights