Skip to content

Kotlin basics

Hadi Mehrpouya edited this page Jan 28, 2025 · 2 revisions

Kotlin Basics

Best resource to get your head around Kotlin programming language is here: Kotlin Basic Syntax study up to https://hyperskill.org/learn/step/4515

Variables

Val stands for value, read-only value var stands for variable, mutable variable

Int, Long, Byte, and Short, add a U to the beginning of each and you have unsigned Integer: UInt and so on.

Use val variables by default and when the need comes, change it to var.

Kotlin uses a mechanism called type inference when defining variables, which means you don't have to specify types. This means, you can define an integer like

var myAge = 20 var myAge2: Int = 20

/images/CaseStudyDTW/typeCoercion.png

val num: Int = 100 val longNum: Long = 1000 val result = num + longNum // 1100, Long

Relational Operations

  • == — equal to X
  • != — not equal to X
  • — greater than X

  • = — greater than or equal to X

  • < — less than X
  • <= — less than or equal to X

Nullability

var name: String = null // This can not be null because you didn't define it as a variable that can have null values.

Can you find out what Non-Null Assertion is? you can add it to your code by using "!!""

Naming variables

  • Names are case-sensitive (number is not the same as Number)
  • Each name can include only letters, digits, and underscores
  • A name cannot start with a digit
  • A name cannot be a keyword (for example, val, var, fun are illegal)
  • no whitespaces are allowed in a variable's name.
  • Do not start variables with an underscore _
  • Avoid using numbers and data directly in your code and instead put them in immutable variables.

Simple Outputs

You can use print and println to present values and messages. They both support value insersion. e.g. var myName : String You can also use const + val to declare a compile-time constant.It is advised that you define this kind of values using screaming_snake_case naming convention. They can only be used for primitive types such as strings and numbers. remember, const variables need to be declared on top level, outside of any functions.

variables can store different types of values: strings, numbers, characters, and other data types

Integers can have underscores to make them more readable. E.g. 120_000_000 is the same as 120000000 in Kotlin.

When reassigning their values, you can only use new values of the same type as the initial one.

val ten = 10_000  //ten thousand
val greeting = "Hello"
val firstLetter = 'A'

var mutableTen = 10
mutableTen = 20
mutableTen = "error" // mutable types variables can only be reassigned to a new value of the same type


val immutableInt : Int // Creates the immutable variable immutableInt of type Int
println(immutableInt) // Compiler error as you haven't initialised immutableInt
immutableInt = 10 // initialises immutableInt with the value 10

Remember variable type restriction in Kotlin!

Remember val does not mean immutable. best to exaplain this with an example.

functions

https://hyperskill.org/learn/step/5991

A program is a sequence of instructions (called statements), which are executed one after another in a predictable manner. Sequential flow is the most common and straightforward situation when statements are executed in the order they are written, i.e., from top to bottom, one after another;

A statement (or a programming statement) is a single command to be executed (like printing a text);

An expression is a piece of code that produces a single value (for example, 2*2 is an expression);

A block is a group of zero or more statements enclosed in a pair of curly braces {...}; our program consists of a single block;

A keyword is a word that has a special meaning in the programming language. Names of keywords can't be changed;

An identifier (or a name) is a word written by the programmer to identify something;

A comment is a piece of text that is ignored when executing the program – it just explains a part of the code. Comments start with //;

Whitespace is a blank area, a tab, or a newline; it is used to separate words in the program and to improve readability.

Clone this wiki locally