This document contains multiple sections about Kotlin programming concepts including:
- Kotlin allows developers to write code more concisely and build more stable apps.
- A Kotlin program requires a main function as the entry point. Functions are defined with a name, optional inputs, and a body of code.
- Variables are used to store data and can be declared with specific data types like Int or String.
- Conditionals like if/else and when statements allow code to run conditionally.
- Kotlin supports null safety with nullable types, the safe call operator (?.), and not-null assertion operator (!!).
3. This work is licensed under the Apache 2.0 License
Kotlin is a modern programming language that helps
developers be more productive. For example, Kotlin allows you
to be more concise and write fewer lines of code for the same
functionality compared to other programming languages. Apps
that are built with Kotlin are also less likely to crash, resulting
in a more stable and robust app for users. Essentially, with
Kotlin, you can write better Android apps in a shorter amount
of time
4. This work is licensed under the Apache 2.0 License
Kotlin Playground
5. This work is licensed under the Apache 2.0 License
A Kotlin program is required to have a main function, which is the
specific place in your code where the Kotlin compiler starts. The
main function is the entry point, or starting point, of the program.
6. This work is licensed under the Apache 2.0 License
Define a function
• The function needs a name, so you can
call it later.
• The function can also require
some inputs, or information that needs
to be provided when the function is
called. The function uses these inputs to
accomplish its purpose. Requiring
inputs are optional, and some functions
require no inputs.
• The function also has a body which
contains the instructions to perform the
task.
7. This work is licensed under the Apache 2.0 License
• The function definition starts with the
word fun.
• Then the name of the function is main.
• There are no inputs to the function, so
the parentheses are empty.
• There is one line of code in the function
body, println("Hello, world!"), which is
located between the opening and closing
curly braces of the function.
8. This work is licensed under the Apache 2.0 License
Variables and Data Types
• A variable, which is a container
for a single piece of data.
9. This work is licensed under the Apache 2.0 License
Data Types:
10. This work is licensed under the Apache 2.0 License
Variable declaration:
For Example:
val count: Int = 2
11. This work is licensed under the Apache 2.0 License
Update Variables
fun main() {
val cartTotal = 0
cartTotal = 20
println("Total:
$cartTotal")
}
fun main() {
var cartTotal = 0
cartTotal = 20
println("Total:
$cartTotal")
}
12. This work is licensed under the Apache 2.0 License
Coding Convention
• Variable names should be in camel case and start
with a lowercase letter.
• In a variable declaration, there should be a spacafter
a colon when you specify the data type.
• There should be a space before and after an operator
like the assignment (=), addition (+), subtraction (-),
multiplication (*), division (/) operators and more.
13. This work is licensed under the Apache 2.0 License
Comments
Single Line: // This is a comment.
/*
Multi Line: * This is a very long comment
that can
* take up multiple lines.
*/
14. This work is licensed under the Apache 2.0 License
Stargazing at State
Conditionals in Kotlin
15. This work is licensed under the Apache 2.0 License
If-else statement
16. This work is licensed under the Apache 2.0 License
when statement
17. This work is licensed under the Apache 2.0 License
if/else and when as expressions
19. This work is licensed under the Apache 2.0 License
What is Null?
Chadwick Boseman
20. This work is licensed under the Apache 2.0 License
Syntax of nullable Datatype
• Nullable types are variables that can hold null.
• Non-null types are variables that can't hold null.
21. This work is licensed under the Apache 2.0 License
?. Safe-call operator
The ?. safe-call operator allows safer access to nullable variables
because the Kotlin compiler stops any attempt of member access
to null references and returns null for the member accessed.
22. This work is licensed under the Apache 2.0 License
!! not-null assertion operator
you use the !! not-null assertion, it means that you assert that the
value of the variable isn’t null, regardless of whether it is or isn’t.