Kotlin Flashcards
modern but already mature programming language designed to make developers happier
kotlin
Kotlin was built by software development tool creators ___ in 2010
JetBrains
Why use ‘Kotlin‘?
- Kotlin is fully compatible with Java
- Kotlin works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
- Kotlin is concise and safe
- Kotlin is easy to learn, especially if you already know Java
- Kotlin is free to use Big community/support
What is the IDE used for Kotlin (in our case)?
IntelliJ
is the entry point for a Kotlin application
Function Declaration
This keyword is short for “function” and is used to declare a function in Kotlin.
fun
This is the name of the function. By convention, it’s called main and serves as the entry point for the program
main
How do we write comments?
/**/ - multiline comments
// - one liner comments
is used to declare a mutable variable, which means its value can be changed or reassigned after it’s initially assigned
var
is used to declare an immutable variable, which means its value cannot be changed or reassigned after it’s initially assigned. It’s essentially a constant.
val
Represents 32-bit signed integers
int
Represents 64-bit signed integer
long
Represents 16-bit signed integers
short
Represents 8-bit signed integers
byte
epresents 32-bit floating-point numbers
float
Represents 64-bit floating-point numbers
double
Represents true or false values
boolean
Represents a single character
char
Represents a sequence of characters
string
2 ways to input in kotlin
1.) val scanner = Scanner(System.in
)
val name = scanner.next()
2.) val name = readLine()
are used to assign values to variables
Assignment operators
are used to compare two values, and returns a Boolean value: either true or false
Comparison operators
are used to determine the logic between variables or values
Logical operators
(also called as Type casting) refers to changing the entity of one data type variable into another data type.
Type conversion
how to type cast?
varname.toString()
varname.toFloat()
An expression consists of __,___,___ etc that produce a single value
enumerate 3
variables, operators, methods calls
A ___ is the syntactic unit of any programming language that expresses some action to be carried out. A program is formed by the sequence of one or more statements.
statement
A ___ is a section of software code enclosed with curly braces ({…}). A block can consist of one or more statements, preceded by the declarations of variables.
block
The ___ specifies a block of code and executes it only if a given condition is true. Otherwise, it ignores the block of code.
if statement
loops
The codes inside the body of ___ is executed once (without checking the testExpression). Then, the test expression is checked.
do
construct
loops
If the test expression is evaluated to
true, codes inside the body of the loop are ___, and test expression is evaluated again. This process goes on until the test expression is evaluated to false.
executed
There are 2 ways to use break and continue in Kotlin programming.
a. With Label(Labeled).
b. Without Label(UnLabeled)
is used to come out of a loop once a certain condition is met. This loop could be a for, while or do…while loop.
labeled break statement
Kotlin labeled break statement is used to terminate the specific loop. This is done by using break expression with what format?
@ sign followed by label name (break@LabelName)
breaks the loop iteration in between (skips the part next to the continue statement till end of the loop) and continues with the next iteration in the loop.
Labeled continue statement
Kotlin labeled continue statement is used to skip the part of a specific loop. This is done by using continue expression with what format?
@ sign followed by label name (continue@LabelName)
When break expression encounters in a program it terminates to nearest enclosing loop
unlabeled break statement
is to used to exit the loop when it satisfies a specific condition without checking the test expression. Then, transfers the control to the following statement of while block
Unlabelled break
In do-while loop also we can use the ___ to exit the loop without checking the test expression
break expression
Continue is used to repeat the loop for a specific condition. It skips the following statements and continues with the next iteration of the loop.
Kotlin unlabelled continue
___ are used to store multiple values in a single variable, instead of creating separate variables for each value.
Arrays
To create an array, use the __ function, and place the values in a comma-separated list inside it.
arrayOf()
format of array creation?
val arrayName = arrayOf(ElementType)
You can access an array element by referring to the ___, inside square brackets.
index number
You can use the __ operator to check if an element exists in an array
in
You can loop through the array elements with the __
for loop
contain a group of related statements that perform specific tasks. They are designed to be reusable, eliminating the need to rewrite the piece of code every time it is used.
Functions
Built-in functions that are immediately ready for use. The Kotlin Standard Library has several packages for different uses
Kotlin Standard Library Functions
Give examples of Kotlin Standard Library Functions
- kotlin
- kotlin.annotation
- kotlin.browser
- kotlin.collections
- kotlin.js
- kotlin.jvm
- kotlin.jvm.optionals
- kotlin.math
as the name implies, functions that you define and write yourself.
User-defined Functions
also known as formal arguments, are placeholders for input values (arguments) that the function will use.
Parameters
In Kotlin, parameters are declared using the Pascal notation.
name:type
If the function has a return type, a ___ must be used. The return statement signals the end of the function definition. The variable returned is the output of the function.
return statement
___ are called using their identifier followed by a __ which may contain arguments based on its declaration
Functions, set of parentheses “()”
are values that were assigned to parameters at the function’s definition. This allows us to call functions with missing arguments. If an argument is not passed to a parameter with a default value, it will use the default value instead.
Default arguments
allow you to specify arguments using the name of the parameter instead of relying on its position.
Named Arguments
A ___ is a function that calls
itself, either directly or
indirectly, to solve a problem. Recursion is
a powerful technique used in
programming when a problem can be broken down into smaller, similar subproblems.
recursive function
3 advantages of recursive function
- Improved code readability and maintainability
- Easy implementation of algorithms
- Reusability
2 disadvantages of recursive functions
- Increased memory usage
- Performance
A ___ is a concise way to represent an anonymous function (a function that has no name) that can be used as a variable, argument, or return value in higher-order functions.
lambda expression
___ are a fundamental feature of functional programming in Kotlin and are used extensively when working with functions, collections, and other higher-order constructs.
Lambda expressions
lambda format
{parameters -> body}
always between curly braces!
are a powerful feature that allows you to treat functions as first-class citizens, meaning you can pass functions as arguments, return them from other functions, and assign them to variables.
Higher-order functions
enable functional programming paradigms and are a fundamental part of the language’s expressiveness and flexibility.
Higher-order functions