SlideShare a Scribd company logo
1 of 54
This work is licensed under the Apache 2.0 License
Kotlin 101
Write better Android apps faster with Kotlin. Kotlin is a
modern statically typed programming language used by over
60% of professional Android developers that helps boost
productivity, developer satisfaction, and code safety.
This work is licensed under the Apache 2.0 License
This work is licensed under the Apache 2.0 License
Kotlin has been designed in a way that it eliminates the need for
boilerplate code. Kotlin requires fewer lines of code as compared to
java.Google got Kotlin for free because it is released under Apache
2.0 open-source license
Why the new language?
This work is licensed under the Apache 2.0 License
Differences
In Java, extension functions are not available.
Thus, java supports
implicit conversion.
Developers need to define the
constructor, getter, and setter
methods in Java to create data classes.
Java does not allow
language scripting.
Kotlin helps developers easily
add extension functions in existing classes..
Kotlin does not support implicit
conversion of data types.
Kotlin compiler creates constructor,
getter, and setter methods automatically
Kotlin enables language scripting
integration in existing scripts.
This work is licensed under the Apache 2.0 License
Let’s start learning ….
Variables
This work is licensed under the Apache 2.0 License
This work is licensed under the Apache 2.0 License
Types
Kotlin uses two different keywords to declare variables: val and var.
● Use val for a variable whose value never changes. You can't assign a
value to a variable that was declared using val.
● Use var for a variable whose value can change.
This work is licensed under the Apache 2.0 License
Use of var
This work is licensed under the Apache 2.0 License
Use of val
This work is licensed under the Apache 2.0 License
Use of null
This work is licensed under the Apache 2.0 License
Conditional Statements
This work is licensed under the Apache 2.0 License
Lets try an example
In life, it's common to do things differently based on the situation that you face. For
example, if the weather is cold, you wear a jacket, whereas if the weather is warm, you
don't wear a jacket.
This work is licensed under the Apache 2.0 License
Decision-making is also a fundamental concept in programming. You
write instructions about how a program should behave in a given
situation so that it can act or react accordingly when the situation occurs.
In Kotlin, when you want your program to perform different actions
based on a condition, you can use an if/else statement
This work is licensed under the Apache 2.0 License
Here the function main has println function which tests
for the condition that if 1 is equal to 1 , which is indeed
true. Hence results true.
This work is licensed under the Apache 2.0 License
Some basic relational operators
● Less than: <
● Greater than: >
● Less than or equal to:
<=
● Greater than or equal
to: >=
● Not equal to: !=
This work is licensed under the Apache 2.0 License
Some more code snippets
This work is licensed under the Apache 2.0 License
After the closing curly brace of the if statement, you add the else keyword followed by a pair of curly braces. Inside
the curly braces of the else statement, you can add a second body that only executes when the condition in the if
branch is false
This work is licensed under the Apache 2.0 License
Code flowchart
This work is licensed under the Apache 2.0 License
This work is licensed under the Apache 2.0 License
Different structuring of if-else
block
This work is licensed under the Apache 2.0 License
This work is licensed under the Apache 2.0 License
Example snippet
This work is licensed under the Apache 2.0 License
In Kotlin, when you deal with multiple branches, you can use the when statement
instead of the if/else statement because it improves readability, which refers to how
easy it is for human readers, typically developers, to read the code. It's very
important to consider readability when you write your code because it's likely that
other developers need to review and modify your code throughout its lifetime
When keyword
This work is licensed under the Apache 2.0 License
Flowchart
when statements are preferred
when there are more than two
branches to consider.
This work is licensed under the Apache 2.0 License
This work is licensed under the Apache 2.0 License
This work is licensed under the Apache 2.0 License
Optimised code
The prime-number program contains a lot of repetition of println()
statements. When you write a when statement, you can use a comma (,) to
denote multiple conditions that correspond to the same body.
This work is licensed under the Apache 2.0 License
This work is licensed under the Apache 2.0 License
Besides the comma (,) symbol to denote multiple conditions, you can also use the in
keyword and a range of values in when branches.To use a range of values, add a number
that denotes the start of the range followed by two periods without any spaces and then
close it with another number that denotes the end of the range.
In keyword
This work is licensed under the Apache 2.0 License
This work is licensed under the Apache 2.0 License
Converting if-else to expression
Null variables
This work is licensed under the Apache 2.0 License
This work is licensed under the Apache 2.0 License
Example scenario
● You learned that when you declare a variable, you need to assign it a
value immediately. For example, when you declare a favoriteActor
variable, you may assign it a "Sandra Oh" string value immediately.
● However, what if we don't
have a favorite actor? You
might want to assign the
variable a "Nobody" or
"None" value. This isn't a good approach because your program
interprets the favoriteActor variable to have a "Nobody" or "None"
value rather than no value at all. In Kotlin, you can use null to
indicate that there's no value associated with the variable.
Null keyword
This work is licensed under the Apache 2.0 License
Now favouriteactor has been
assigned null
This work is licensed under the Apache 2.0 License
This work is licensed under the Apache 2.0 License
So why did it happen?
A type is only nullable if you explicitly let it hold null. As the error message says, the String data type is a
non-nullable type, so you can't reassign the variable to null.To declare nullable variables in Kotlin, you
need to add a ? operator to the end of the type. For example, a String? type can hold either a string or
null, whereas a String type can only hold a string. To declare a nullable variable, you need to explicitly
add the nullable type. Without the nullable type, the Kotlin compiler infers that it's a non-nullable type.
This work is licensed under the Apache 2.0 License
This work is licensed under the Apache 2.0 License
Functions
This work is licensed under the Apache 2.0 License
Store function in a variable
This work is licensed under the Apache 2.0 License
Error is thrown
Store function in a variable
It produces an error because the
Kotlin compiler recognizes trick as the
name of the trick() function, but
expects to call the function, rather
than assign it to a variable.
This work is licensed under the Apache 2.0 License
Store function in a variable
Reference operator (: :)
This work is licensed under the Apache 2.0 License
Practice:
Kotlin
Fundamentals
This work is licensed under the Apache 2.0 License
Write a program that prints the summary message based on the number
of notifications that you received. The message should include:
● The exact number of notifications when there are less than 100 notifications.
● [99+] as the number of notifications when there are 100 notifications or more.
You have 51 notifications.
Your phone is blowing up! You have 99+ notifications.
This work is licensed under the Apache 2.0 License
This work is licensed under the Apache 2.0 License
Movie tickets are typically priced differently based on the age of moviegoers.
In the initial code provided in the following code snippet, write a program that
calculates these age-based ticket prices:
● A children's ticket price of $15 for people 12 years old or younger.
● A standard ticket price of $30 for people between 13 and 60 years old. On Mondays,
discount the standard ticket price to $25 for this same age group.
● A senior ticket price of $20 for people 61 years old and older. Assume that the
maximum age of a moviegoer is 100 years old.
● A -1 value to indicate that the price is invalid when a user inputs an age outside of the
age specifications.
The movie ticket price for a person aged 5 is $15.
The movie ticket price for a person aged 28 is $25.
The movie ticket price for a person aged 87 is $20.
This work is licensed under the Apache 2.0 License
This work is licensed under the Apache 2.0 License
There are three main temperature scales used in the world: Celsius,
Fahrenheit, and Kelvin.
In the initial code provided in the following code snippet, write a
program that converts a temperature from one scale to another with
these formulas:
● Celsius to Fahrenheit: ° F = 9/5 (° C) + 32
● Kelvin to Celsius: ° C = K - 273.15
● Fahrenheit to Kelvin: K = 5/9 (° F - 32) + 273.15
27.0 degrees Celsius is 80.60 degrees Fahrenheit.
350.0 degrees Kelvin is 76.85 degrees Celsius.
10.0 degrees Fahrenheit is 260.93 degrees Kelvin.
This work is licensed under the Apache 2.0 License
This work is licensed under the Apache 2.0 License
Classes
This work is licensed under the Apache 2.0 License
Structure
● A class definition starts with the
class keyword, followed by a name
and a set of curly braces
This work is licensed under the Apache 2.0 License
Structure
● A class definition starts with the
class keyword, followed by a name
and a set of curly braces
● Class an an object``
This work is licensed under the Apache 2.0 License
Class Properties
Device name is: Android TV
Smart device is turned on.
Smart device is turned off.
This work is licensed under the Apache 2.0 License
Constructors
This work is licensed under the Apache 2.0 License
THANK YOU
Have fun encouraging your
community in becoming
Android Developers!

More Related Content

Similar to Copy of kotlin.pptx

Compose Camp: Introduction to Kotlin.pptx
Compose Camp: Introduction to Kotlin.pptxCompose Camp: Introduction to Kotlin.pptx
Compose Camp: Introduction to Kotlin.pptxAmruthasriAmaravati
 
Compose camp 2.pptx
Compose camp 2.pptxCompose camp 2.pptx
Compose camp 2.pptxbcedsc
 
-Kotlin_Camp_Unit2.pptx
-Kotlin_Camp_Unit2.pptx-Kotlin_Camp_Unit2.pptx
-Kotlin_Camp_Unit2.pptxRishiGandhi19
 
Kotlin Basics & Introduction to Jetpack Compose.pptx
Kotlin Basics & Introduction to Jetpack Compose.pptxKotlin Basics & Introduction to Jetpack Compose.pptx
Kotlin Basics & Introduction to Jetpack Compose.pptxtakshilkunadia
 
GDSC_day_1.pptx
GDSC_day_1.pptxGDSC_day_1.pptx
GDSC_day_1.pptxGDSCICOER
 
Android Development | Compose Camp Day 1 | GDSC SEC Sasaram.pdf
Android Development | Compose Camp Day 1 | GDSC SEC Sasaram.pdfAndroid Development | Compose Camp Day 1 | GDSC SEC Sasaram.pdf
Android Development | Compose Camp Day 1 | GDSC SEC Sasaram.pdfShivamShrey1
 
Compose Camp Day 1.pdf
Compose Camp Day 1.pdfCompose Camp Day 1.pdf
Compose Camp Day 1.pdfShivamShrey1
 
Android study jam iiitv kick-off sesson
Android study jam iiitv   kick-off sessonAndroid study jam iiitv   kick-off sesson
Android study jam iiitv kick-off sessonAshutoshSingh1124
 
Compose Camp S1.pptx
Compose Camp S1.pptxCompose Camp S1.pptx
Compose Camp S1.pptxGDSCSIT
 
Prior programming experience track
Prior programming experience trackPrior programming experience track
Prior programming experience trackAshwinRaj57
 
Android Study Jams - New to Programming [27th december]
Android Study Jams - New to Programming [27th december]Android Study Jams - New to Programming [27th december]
Android Study Jams - New to Programming [27th december]PragatiVerma31
 
Compose Camp Slide Session 1
Compose Camp Slide Session 1Compose Camp Slide Session 1
Compose Camp Slide Session 1AkshatBajpai12
 

Similar to Copy of kotlin.pptx (20)

Android Study Jams - Session 3
Android Study Jams - Session 3Android Study Jams - Session 3
Android Study Jams - Session 3
 
Compose Camp: Introduction to Kotlin.pptx
Compose Camp: Introduction to Kotlin.pptxCompose Camp: Introduction to Kotlin.pptx
Compose Camp: Introduction to Kotlin.pptx
 
Compose camp 2.pptx
Compose camp 2.pptxCompose camp 2.pptx
Compose camp 2.pptx
 
day1.docx
day1.docxday1.docx
day1.docx
 
-Kotlin_Camp_Unit2.pptx
-Kotlin_Camp_Unit2.pptx-Kotlin_Camp_Unit2.pptx
-Kotlin_Camp_Unit2.pptx
 
-Kotlin Camp Unit2.pptx
-Kotlin Camp Unit2.pptx-Kotlin Camp Unit2.pptx
-Kotlin Camp Unit2.pptx
 
Kotlin Basics & Introduction to Jetpack Compose.pptx
Kotlin Basics & Introduction to Jetpack Compose.pptxKotlin Basics & Introduction to Jetpack Compose.pptx
Kotlin Basics & Introduction to Jetpack Compose.pptx
 
GDSC_day_1.pptx
GDSC_day_1.pptxGDSC_day_1.pptx
GDSC_day_1.pptx
 
Android Development | Compose Camp Day 1 | GDSC SEC Sasaram.pdf
Android Development | Compose Camp Day 1 | GDSC SEC Sasaram.pdfAndroid Development | Compose Camp Day 1 | GDSC SEC Sasaram.pdf
Android Development | Compose Camp Day 1 | GDSC SEC Sasaram.pdf
 
Compose Camp Day 1.pdf
Compose Camp Day 1.pdfCompose Camp Day 1.pdf
Compose Camp Day 1.pdf
 
Android study jam iiitv kick-off sesson
Android study jam iiitv   kick-off sessonAndroid study jam iiitv   kick-off sesson
Android study jam iiitv kick-off sesson
 
Compose Camp S1.pptx
Compose Camp S1.pptxCompose Camp S1.pptx
Compose Camp S1.pptx
 
Compose Camp 2.pdf
Compose Camp 2.pdfCompose Camp 2.pdf
Compose Camp 2.pdf
 
Compose Camp.pdf
Compose Camp.pdfCompose Camp.pdf
Compose Camp.pdf
 
Prior programming experience track
Prior programming experience trackPrior programming experience track
Prior programming experience track
 
Compose Camp Session 1.pdf
Compose Camp Session 1.pdfCompose Camp Session 1.pdf
Compose Camp Session 1.pdf
 
Android Study Jams - New to Programming [27th december]
Android Study Jams - New to Programming [27th december]Android Study Jams - New to Programming [27th december]
Android Study Jams - New to Programming [27th december]
 
Compose Camp Slide Session 1
Compose Camp Slide Session 1Compose Camp Slide Session 1
Compose Camp Slide Session 1
 
Compose Camp
Compose Camp Compose Camp
Compose Camp
 
Compose Camp Day 2.pptx
Compose Camp Day 2.pptxCompose Camp Day 2.pptx
Compose Camp Day 2.pptx
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 

Recently uploaded (20)

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 

Copy of kotlin.pptx

  • 1. This work is licensed under the Apache 2.0 License Kotlin 101
  • 2. Write better Android apps faster with Kotlin. Kotlin is a modern statically typed programming language used by over 60% of professional Android developers that helps boost productivity, developer satisfaction, and code safety. This work is licensed under the Apache 2.0 License
  • 3. This work is licensed under the Apache 2.0 License Kotlin has been designed in a way that it eliminates the need for boilerplate code. Kotlin requires fewer lines of code as compared to java.Google got Kotlin for free because it is released under Apache 2.0 open-source license Why the new language?
  • 4. This work is licensed under the Apache 2.0 License Differences In Java, extension functions are not available. Thus, java supports implicit conversion. Developers need to define the constructor, getter, and setter methods in Java to create data classes. Java does not allow language scripting. Kotlin helps developers easily add extension functions in existing classes.. Kotlin does not support implicit conversion of data types. Kotlin compiler creates constructor, getter, and setter methods automatically Kotlin enables language scripting integration in existing scripts.
  • 5. This work is licensed under the Apache 2.0 License Let’s start learning ….
  • 6. Variables This work is licensed under the Apache 2.0 License
  • 7. This work is licensed under the Apache 2.0 License Types Kotlin uses two different keywords to declare variables: val and var. ● Use val for a variable whose value never changes. You can't assign a value to a variable that was declared using val. ● Use var for a variable whose value can change.
  • 8. This work is licensed under the Apache 2.0 License Use of var
  • 9. This work is licensed under the Apache 2.0 License Use of val
  • 10. This work is licensed under the Apache 2.0 License Use of null
  • 11. This work is licensed under the Apache 2.0 License Conditional Statements
  • 12. This work is licensed under the Apache 2.0 License Lets try an example In life, it's common to do things differently based on the situation that you face. For example, if the weather is cold, you wear a jacket, whereas if the weather is warm, you don't wear a jacket.
  • 13. This work is licensed under the Apache 2.0 License Decision-making is also a fundamental concept in programming. You write instructions about how a program should behave in a given situation so that it can act or react accordingly when the situation occurs. In Kotlin, when you want your program to perform different actions based on a condition, you can use an if/else statement
  • 14. This work is licensed under the Apache 2.0 License Here the function main has println function which tests for the condition that if 1 is equal to 1 , which is indeed true. Hence results true.
  • 15. This work is licensed under the Apache 2.0 License Some basic relational operators ● Less than: < ● Greater than: > ● Less than or equal to: <= ● Greater than or equal to: >= ● Not equal to: !=
  • 16. This work is licensed under the Apache 2.0 License Some more code snippets
  • 17. This work is licensed under the Apache 2.0 License After the closing curly brace of the if statement, you add the else keyword followed by a pair of curly braces. Inside the curly braces of the else statement, you can add a second body that only executes when the condition in the if branch is false
  • 18. This work is licensed under the Apache 2.0 License Code flowchart
  • 19. This work is licensed under the Apache 2.0 License
  • 20. This work is licensed under the Apache 2.0 License Different structuring of if-else block
  • 21. This work is licensed under the Apache 2.0 License
  • 22. This work is licensed under the Apache 2.0 License Example snippet
  • 23. This work is licensed under the Apache 2.0 License In Kotlin, when you deal with multiple branches, you can use the when statement instead of the if/else statement because it improves readability, which refers to how easy it is for human readers, typically developers, to read the code. It's very important to consider readability when you write your code because it's likely that other developers need to review and modify your code throughout its lifetime When keyword
  • 24. This work is licensed under the Apache 2.0 License Flowchart when statements are preferred when there are more than two branches to consider.
  • 25. This work is licensed under the Apache 2.0 License
  • 26. This work is licensed under the Apache 2.0 License
  • 27. This work is licensed under the Apache 2.0 License Optimised code The prime-number program contains a lot of repetition of println() statements. When you write a when statement, you can use a comma (,) to denote multiple conditions that correspond to the same body.
  • 28. This work is licensed under the Apache 2.0 License
  • 29. This work is licensed under the Apache 2.0 License Besides the comma (,) symbol to denote multiple conditions, you can also use the in keyword and a range of values in when branches.To use a range of values, add a number that denotes the start of the range followed by two periods without any spaces and then close it with another number that denotes the end of the range. In keyword
  • 30. This work is licensed under the Apache 2.0 License
  • 31. This work is licensed under the Apache 2.0 License Converting if-else to expression
  • 32. Null variables This work is licensed under the Apache 2.0 License
  • 33. This work is licensed under the Apache 2.0 License Example scenario ● You learned that when you declare a variable, you need to assign it a value immediately. For example, when you declare a favoriteActor variable, you may assign it a "Sandra Oh" string value immediately. ● However, what if we don't have a favorite actor? You might want to assign the variable a "Nobody" or "None" value. This isn't a good approach because your program interprets the favoriteActor variable to have a "Nobody" or "None" value rather than no value at all. In Kotlin, you can use null to indicate that there's no value associated with the variable.
  • 34. Null keyword This work is licensed under the Apache 2.0 License Now favouriteactor has been assigned null
  • 35. This work is licensed under the Apache 2.0 License
  • 36. This work is licensed under the Apache 2.0 License So why did it happen? A type is only nullable if you explicitly let it hold null. As the error message says, the String data type is a non-nullable type, so you can't reassign the variable to null.To declare nullable variables in Kotlin, you need to add a ? operator to the end of the type. For example, a String? type can hold either a string or null, whereas a String type can only hold a string. To declare a nullable variable, you need to explicitly add the nullable type. Without the nullable type, the Kotlin compiler infers that it's a non-nullable type.
  • 37. This work is licensed under the Apache 2.0 License
  • 38. This work is licensed under the Apache 2.0 License Functions
  • 39. This work is licensed under the Apache 2.0 License Store function in a variable
  • 40. This work is licensed under the Apache 2.0 License Error is thrown Store function in a variable It produces an error because the Kotlin compiler recognizes trick as the name of the trick() function, but expects to call the function, rather than assign it to a variable.
  • 41. This work is licensed under the Apache 2.0 License Store function in a variable Reference operator (: :)
  • 42. This work is licensed under the Apache 2.0 License Practice: Kotlin Fundamentals
  • 43. This work is licensed under the Apache 2.0 License Write a program that prints the summary message based on the number of notifications that you received. The message should include: ● The exact number of notifications when there are less than 100 notifications. ● [99+] as the number of notifications when there are 100 notifications or more. You have 51 notifications. Your phone is blowing up! You have 99+ notifications.
  • 44. This work is licensed under the Apache 2.0 License
  • 45. This work is licensed under the Apache 2.0 License Movie tickets are typically priced differently based on the age of moviegoers. In the initial code provided in the following code snippet, write a program that calculates these age-based ticket prices: ● A children's ticket price of $15 for people 12 years old or younger. ● A standard ticket price of $30 for people between 13 and 60 years old. On Mondays, discount the standard ticket price to $25 for this same age group. ● A senior ticket price of $20 for people 61 years old and older. Assume that the maximum age of a moviegoer is 100 years old. ● A -1 value to indicate that the price is invalid when a user inputs an age outside of the age specifications. The movie ticket price for a person aged 5 is $15. The movie ticket price for a person aged 28 is $25. The movie ticket price for a person aged 87 is $20.
  • 46. This work is licensed under the Apache 2.0 License
  • 47. This work is licensed under the Apache 2.0 License There are three main temperature scales used in the world: Celsius, Fahrenheit, and Kelvin. In the initial code provided in the following code snippet, write a program that converts a temperature from one scale to another with these formulas: ● Celsius to Fahrenheit: ° F = 9/5 (° C) + 32 ● Kelvin to Celsius: ° C = K - 273.15 ● Fahrenheit to Kelvin: K = 5/9 (° F - 32) + 273.15 27.0 degrees Celsius is 80.60 degrees Fahrenheit. 350.0 degrees Kelvin is 76.85 degrees Celsius. 10.0 degrees Fahrenheit is 260.93 degrees Kelvin.
  • 48. This work is licensed under the Apache 2.0 License
  • 49. This work is licensed under the Apache 2.0 License Classes
  • 50. This work is licensed under the Apache 2.0 License Structure ● A class definition starts with the class keyword, followed by a name and a set of curly braces
  • 51. This work is licensed under the Apache 2.0 License Structure ● A class definition starts with the class keyword, followed by a name and a set of curly braces ● Class an an object``
  • 52. This work is licensed under the Apache 2.0 License Class Properties Device name is: Android TV Smart device is turned on. Smart device is turned off.
  • 53. This work is licensed under the Apache 2.0 License Constructors
  • 54. This work is licensed under the Apache 2.0 License THANK YOU Have fun encouraging your community in becoming Android Developers!