Main Function as an entry point

fun main() {
    println("Hello, Android!")
}


Data types

  1. String
  2. Int
  3. Double
  4. Float
  5. Boolean


Variable declaration


Immutable variable using val


val name: dataType = initialValue


fun main() {
    val count: Int = 2
    println(count)
}

 Sign ($) followed by a variable name for the template expression

fun main() {
    val count: Int = 2
    println("You have $count unread messages.")
}


Type inference

  • Type inference is when the Kotlin compiler can infer (or determine) what data type a variable should be, without the type being explicitly written in the code.
  • That means you can omit the data type in a variable declaration, if you provide an initial value for the variable.
  • The Kotlin compiler looks at the data type of the initial value, and assumes that you want the variable to hold data of that type.
val name = initial value


val count: Int = 2


val count = 2

Note: If you don't provide an initial value when you declare a variable, you must specify the type.

In this line of code, no initial value is provided, so you must specify the data type:

val count: Int

In this line of code, an assigned value is provided, so you can omit the data type:

val count = 2


Basic math operations with integers

fun main() {
    val unreadCount = 5
    val readCount = 100
    println("You have ${unreadCount + readCount} total messages in your inbox.")
}

Output

You have 105 total messages in your inbox.


fun main() {
    val numberOfPhotos = 100
    val photosDeleted = 10
    println("$numberOfPhotos photos")
    println("$photosDeleted photos deleted")
    println("${numberOfPhotos - photosDeleted} photos left")
}

Output

100 photos
10 photos deleted
90 photos left


Update variables


fun main() {
    val cartTotal = 0
    cartTotal = 20
    println("Total: $cartTotal")
}

Output

Val cannot be reassigned

If you need to update the value of a variable, declare the variable with the Kotlin keyword var, instead of val.

  • val keyword - Use when you expect the variable value will not change.
  • var keyword - Use when you expect the variable value can change.

With val, the variable is read-only, which means you can only read, or access, the value of the variable. Once the value is set, you cannot edit or modify its value. With var, the variable is mutable, which means the value can be changed or modified. The value can be mutated.

To remember the difference, think of val as a fixed value and var as variable. In Kotlin, it's recommended to use the val keyword over the var keyword when possible.


var name: Int

or

var name = 5

To update its value

var = updated_value


fun main() {
    var cartTotal = 0
    cartTotal = 20
    println("Total: $cartTotal")
}


fun main() {
    var cartTotal = 0
    println("Total: $cartTotal")

    cartTotal = 20
    println("Total: $cartTotal")
}

Output

Total: 0
Total: 20

Increment and decrement operators

fun main() {
    var count = 10
    println("You have $count unread messages.")
    count = count + 1
    println("You have $count unread messages.")
}

Output

You have 10 unread messages.
You have 11 unread messages.

For shorthand,

count = count + 1

or

count++


fun main() {
    var count = 10
    println("You have $count unread messages.")
    count++
    println("You have $count unread messages.")
}

Output

You have 10 unread messages.
You have 11 unread messages.


fun main() {
    var count = 10
    println("You have $count unread messages.")
    count--
    println("You have $count unread messages.")
}

Output

You have 10 unread messages.
You have 9 unread messages.

Summary

  • count++ is the same as count = count + 1 and
  • count-- is the same as count = count - 1


Commenting in your code

// This is a comment.

height = 1 // Assume the height is 1 to start with

/*
 * This is a very long comment that can
 * take up multiple lines.
 */


/*
 * This program displays the number of messages
 * in the user's inbox.
 */
fun main() {
    // Create a variable for the number of unread messages.
    var count = 10
    println("You have $count unread messages.")

    // Decrease the number of messages by 1.
    count--
    println("You have $count unread messages.")
}


Define and call a function

fun name() : returnType {
	body
	return statement
}


fun main() {
    birthdayGreeting()
}

fun birthdayGreeting() {
    println("Happy Birthday, Rover!")
    println("You are now 5 years old!")
}

Output

Happy Birthday, Rover!
You are now 5 years old!


The Unit type (void in Java )

fun main() {
    birthdayGreeting()
}

fun birthdayGreeting(): Unit {
    println("Happy Birthday, Rover!")
    println("You are now 5 years old!")
}

Output

Happy Birthday, Rover!
You are now 5 years old!


fun main() {
    val greeting = birthdayGreeting()
    println(greeting)
}

Or 

fun main() {
    println(birthdayGreeting())
}

fun birthdayGreeting(): String {
    val nameGreeting = "Happy Birthday, Rover!"
    val ageGreeting = "You are now 5 years old!"
    return "$nameGreeting\n$ageGreeting"
}

Output

Happy Birthday, Rover!
You are now 5 years old!


Function Parameter


fun name(parameter) : return type {
	body
}


fun main() {
    println(birthdayGreeting("Rover"))
}
fun birthdayGreeting(name: String): String {
    val nameGreeting = "Happy Birthday, $name!"
    val ageGreeting = "You are now 5 years old!"
    return "$nameGreeting\n$ageGreeting"
}

Output

Happy Birthday, Rover!
You are now 5 years old!


Functions with multiple parameters


Positional arguments and named arguments also Work in Kotlin!

fun name (first parameter, secend parameter,...)

Function Signature

fun birthdayGreeting(name: String, age: Int)


fun main() {
    println(birthdayGreeting("Rover", 5))
    println(birthdayGreeting("Rex", 2))
}

fun birthdayGreeting(name: String, age: Int): String {
    val nameGreeting = "Happy Birthday, $name!"
    val ageGreeting = "You are now $age years old!"
    return "$nameGreeting\n$ageGreeting"
}

Output

Happy Birthday, Rover!
You are now 5 years old!
Happy Birthday, Rex!
You are now 2 years old!


Named arguments

println(birthdayGreeting(name = "Rex", age = 2))

fun main() {
    println(birthdayGreeting("Rover", 5))
    println(birthdayGreeting("Rex", 2))
}


Output

Happy Birthday, Rover!
You are now 5 years old!
Happy Birthday, Rex!
You are now 2 years old!


Default arguments

fun birthdayGreeting(name: String = "Rover", age: Int): String {
    return "Happy Birthday, $name! You are now $age years old!"
}


println(birthdayGreeting(age = 5))
println(birthdayGreeting("Rex", 2))


Summary

  • A Kotlin program requires a main function as the entry point of the program.
  • To define a function in Kotlin, use the fun keyword, followed by the name of the function, any inputs enclosed in parentheses, followed by the function body enclosed in curly braces.
  • The name of a function should follow camel case convention and start with a lowercase letter.
  • Use the println() function call to print some text to the output.
  • Refer to the Kotlin style guide for formatting and code conventions to follow when coding in Kotlin.
  • Troubleshooting is the process of resolving errors in your code.


← Back to Learning Journey