Posts

Coroutines in Kotlin

Image
Overview Dependencies used to use Coroutines in IntelliJ Community Edition import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { // For build.gradle.kts (Kotlin DSL) kotlin ( "jvm" ) version "1.6.0" } group = "me.iamra" version = "1.0-SNAPSHOT" repositories { mavenCentral() } dependencies { testImplementation ( kotlin ( "test" )) implementation ( "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.1" ) } tasks . test { useJUnit() } tasks . withType <KotlinCompile> () { kotlinOptions . jvmTarget = "1.8" } Do's and Don'ts Reference https://stackoverflow.com/questions/52522282/using-kotlinx-coroutines-in-intellij-idea-project https://github.com/Kotlin/kotlinx.coroutines/blob/master/README.md

Naming variables in Kotlin

Image
Why naming is important? Every variable should have a unique name that identifies it. Always give descriptive and concise names to all variables. There are two sets of rules that restrict possible names for variables. Naming Rules 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. Note: If you break these rules, your program will not work. Conventions for naming variables If a variable name is a single word, put it in lowercase (for example - number, value) If a variable name includes, multiple words, put them in lowerCamelCase , so that the first word is lowercase, while other words start with a capital letter (for example - numberOfCoins) Do not start variables with an underscore _ . Technically, you can do it, though. Choose meaningful names for your variable

Standard input in Kotlin

Image
Standard input Standard input is a stream of data that goes into the program. By default, standard input gets data from the keyboard but it is possible to get it from a file. Kotlin functions Kotlin has a useful function to read data from the standard input. It is supported by the operating system. It is readline . It reads the whole line as a string. fun main () { val line = readLine ()!! println (line) } If you need to get a number from the input, you can use this construction: fun main () { val line = readLine ()?. toInt ()!! println (line) } To accept multiple words in a single use the following code val (a , b) = readLine ()!!. split ( ' ' ) println ( " $ a , $ b " ) val (c , d , e) = readLine ()!!. split ( ' ' ) println ( " $ c , $ d , $ e " ) Java Scanner Another way to obtain data from the standard input is to use the Java Scanner. Scanner allows a program to read values of different types (strings, numbers, etc) from

Values and Variables in Kotlin

Image
What is a variable? A variable is a storage for a value, which can be a string, a number, or something else. Every variable has a name (or an identifier) to distinguish it from other variables. You can access a value by the name of the variable. Declaring variables Kotlin provides two keywords val (for value) declares an immutable variable (just a named value or a constant ), which cannot be changed after is has been initialized. var (for variable) declares a mutable variable , which can be changed (as many times as needed) Both val and var keywords provide you a variable! To assign a certain value to a variable, we should use the assignment operator = It is also possible to declare and initialize a variable with the value of another variable. Storing different types of values There is one restriction for mutable variables (the ones declared with the keyword var), though. When reassigning their values, you can only use new values of the same type as the initial one. For implemen

Basic Literals in Kotlin

Image
Integer Numbers If an integer value contains a lot of digits, we can add underscores to divide the digits into blocks to make this number more readable: for example, 1_000_000 is much easier to read than 1000000 You can add as many underscores as you would like e.g. 1___000_00 , 1_2_3 Underscores can't appear at the start or at the end of the number. If you write _10 or 100_ you will get an error. Characters A single character can represent a digit, a letter or another symbol. To write a single character we wrap a symbol in single quotes. A character cannot include two or more digits or letters because it represents a single symbol. e.g. ' abc ' and ' 543 ' are incorrect character literal. Strings Strings represent text information. So strings can include letters, digits, whitespaces and other characters. To write strings, we wrap characters in double quotes instead of single ones. A string can also contain just one single character like "A" . Do not c

Kotlin Plugin used in Android

Image
Overview Add this line of code in the build.gradle (Module) file to use components in the view directly. id 'kotlin-android' id 'kotlin-android-extensions' Reference

Readline in Kotlin

Image
Overview You can use the code readline()!! to read a line in Kotlin Reference