SlideShare a Scribd company logo
Variables, Constants, Types, and
Conditionals
Kotlin Study Group
John
Hi, I am John
- Android Developer
- Work on Rakuten Viki
- Use Kotlin for 2 years
- Linkedin:
https://www.linkedin.com/in/chang-john-0a95237a/
Outline
- What’s the structure of declaring a variable?
- Val v.s Var
- Constants
- Conditionals
- If keyword
- When keyword
- Statement and Expression
- Supplement
- Inspecting Kotlin Bytecode
- Range
- String templates
- Q&A
What’s the structure of declaring a variable?
var experiencePoints: Int = 5
val playerName: String = “Estragon”
val name = “Madrigal”
var healthPoint = 100
var name: String
Why type can ignore? Because of type inference
Built-in types
Val v.s Var
Var usage
Val usage
Once variable is declared by val, it is read-only
variable
Once variable is declared by var, it is able to be
modified variable
We recommend that you use a val any time you do not need a var.
Why need to distinguish var and val?
- Almost all of cases on production, value(data) is decided already when variable is
declared.
For example, get response from backend or get information from database,
generate financial report...
- Avoid others to destroy your design
For example, data class Person(val id: Int, val name: String)
val p1 = Person(“123456789”, “John”)
p1.name = “Johnny”
What are benefits using val often? [Advanced]
- When you need the variable, you just declare.
- Due to feature of val is ready-only, you can track origin first on debug(easy
debug)
- Due to variable by val must assign value while you declare and disallow be
modified, you will build constructor-injection architecture unawarely(easy
testing)
- According to above benefits, you will build a clear(immutable) codebase to
decrease software complexity(easy maintain)
Compile-Time Constant
Cannot exist ‘const’ only
Cannot correspond with ‘var’ keyword
Cannot enclose on function
Cannot declare non primitive type
Compile-Time Constant
Success - const is used for non-enclosed block(function, class) and primitive type
Primitive Type: String, Int, Double, Float, Long, Short, Byte, Char, Boolean
Conditionals - If keyword
One line if expression so
that remove braces
Append comparison
and logical operators
comparison
logical
Support nested
Conditionals - when keyword
Conditionals - statement and expression
This is a statement This is an expression
What’s different when i use the expression?
- Code become more straightforward
statement expression
What’s different when i use the expression?
- Code become more exhaustive
statement expression
Inspecting Kotlin Bytecode
Inspecting Kotlin Bytecode
Kotlin Java
Range
Ctrl + Shift + P can understand its type
String template
String template
Q&A

More Related Content

Similar to Variables, constants, types, and conditionals

JAVA in Artificial intelligent
JAVA in Artificial intelligentJAVA in Artificial intelligent
JAVA in Artificial intelligent
Virat Andodariya
 
Let's talk about certification: SCJA
Let's talk about certification: SCJALet's talk about certification: SCJA
Let's talk about certification: SCJA
José Maria Silveira Neto
 
Enumerations in java.pptx
Enumerations in java.pptxEnumerations in java.pptx
Enumerations in java.pptx
Srizan Pokrel
 
Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1
SURBHI SAROHA
 
Code Testability
Code TestabilityCode Testability
Code Testability
Yury Kisliak
 
Java 2.pptx
Java 2.pptxJava 2.pptx
Java 2.pptx
usmanusman720379
 
05. Control Structures.ppt
05. Control Structures.ppt05. Control Structures.ppt
05. Control Structures.ppt
AyushDut
 
Vue.js Getting Started
Vue.js Getting StartedVue.js Getting Started
Vue.js Getting Started
Murat Doğan
 
Principled And Clean Coding
Principled And Clean CodingPrincipled And Clean Coding
Principled And Clean Coding
Metin Ogurlu
 
Ppt chapter06
Ppt chapter06Ppt chapter06
Ppt chapter06
Richard Styner
 
imperative programming language, java, android
imperative programming language, java, androidimperative programming language, java, android
imperative programming language, java, android
i i
 
JAVA(module1).pptx
JAVA(module1).pptxJAVA(module1).pptx
JAVA(module1).pptx
SRKCREATIONS
 
Intro to Scala
 Intro to Scala Intro to Scala
Intro to Scala
manaswinimysore
 
Introduction to Kotlin for Android developers
Introduction to Kotlin for Android developersIntroduction to Kotlin for Android developers
Introduction to Kotlin for Android developers
Mohamed Wael
 
Java class 1
Java class 1Java class 1
Java class 1
Edureka!
 
Overview of Java
Overview of JavaOverview of Java
Overview of Java
tawi123
 
The Many Ways to Test Your React App
The Many Ways to Test Your React AppThe Many Ways to Test Your React App
The Many Ways to Test Your React App
All Things Open
 
Guia de Estudo OCA Java SE 5 - SE6
Guia de Estudo OCA Java SE 5 - SE6Guia de Estudo OCA Java SE 5 - SE6
Guia de Estudo OCA Java SE 5 - SE6
Daniel Takabayashi, MSc
 
From Java to Kotlin
From Java to KotlinFrom Java to Kotlin
From Java to Kotlin
João Ferrão
 
Eo gaddis java_chapter_05_5e
Eo gaddis java_chapter_05_5eEo gaddis java_chapter_05_5e
Eo gaddis java_chapter_05_5e
Gina Bullock
 

Similar to Variables, constants, types, and conditionals (20)

JAVA in Artificial intelligent
JAVA in Artificial intelligentJAVA in Artificial intelligent
JAVA in Artificial intelligent
 
Let's talk about certification: SCJA
Let's talk about certification: SCJALet's talk about certification: SCJA
Let's talk about certification: SCJA
 
Enumerations in java.pptx
Enumerations in java.pptxEnumerations in java.pptx
Enumerations in java.pptx
 
Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1
 
Code Testability
Code TestabilityCode Testability
Code Testability
 
Java 2.pptx
Java 2.pptxJava 2.pptx
Java 2.pptx
 
05. Control Structures.ppt
05. Control Structures.ppt05. Control Structures.ppt
05. Control Structures.ppt
 
Vue.js Getting Started
Vue.js Getting StartedVue.js Getting Started
Vue.js Getting Started
 
Principled And Clean Coding
Principled And Clean CodingPrincipled And Clean Coding
Principled And Clean Coding
 
Ppt chapter06
Ppt chapter06Ppt chapter06
Ppt chapter06
 
imperative programming language, java, android
imperative programming language, java, androidimperative programming language, java, android
imperative programming language, java, android
 
JAVA(module1).pptx
JAVA(module1).pptxJAVA(module1).pptx
JAVA(module1).pptx
 
Intro to Scala
 Intro to Scala Intro to Scala
Intro to Scala
 
Introduction to Kotlin for Android developers
Introduction to Kotlin for Android developersIntroduction to Kotlin for Android developers
Introduction to Kotlin for Android developers
 
Java class 1
Java class 1Java class 1
Java class 1
 
Overview of Java
Overview of JavaOverview of Java
Overview of Java
 
The Many Ways to Test Your React App
The Many Ways to Test Your React AppThe Many Ways to Test Your React App
The Many Ways to Test Your React App
 
Guia de Estudo OCA Java SE 5 - SE6
Guia de Estudo OCA Java SE 5 - SE6Guia de Estudo OCA Java SE 5 - SE6
Guia de Estudo OCA Java SE 5 - SE6
 
From Java to Kotlin
From Java to KotlinFrom Java to Kotlin
From Java to Kotlin
 
Eo gaddis java_chapter_05_5e
Eo gaddis java_chapter_05_5eEo gaddis java_chapter_05_5e
Eo gaddis java_chapter_05_5e
 

More from Chang John

Whats resources compat_contextcompat_and_appcompat
Whats resources compat_contextcompat_and_appcompatWhats resources compat_contextcompat_and_appcompat
Whats resources compat_contextcompat_and_appcompat
Chang John
 
Java interoperability
Java interoperabilityJava interoperability
Java interoperability
Chang John
 
Interface and abstract classes
Interface and abstract classesInterface and abstract classes
Interface and abstract classes
Chang John
 
Objects
ObjectsObjects
Objects
Chang John
 
Inheritance
InheritanceInheritance
Inheritance
Chang John
 
Initialization
InitializationInitialization
Initialization
Chang John
 
Defining classes
Defining classesDefining classes
Defining classes
Chang John
 
Strings & numbers
Strings & numbersStrings & numbers
Strings & numbers
Chang John
 
Functions, anonymous functions and the function type
Functions, anonymous functions and the function typeFunctions, anonymous functions and the function type
Functions, anonymous functions and the function type
Chang John
 
Google Play Instant: Improving User Engagement
Google Play Instant: Improving User EngagementGoogle Play Instant: Improving User Engagement
Google Play Instant: Improving User Engagement
Chang John
 

More from Chang John (10)

Whats resources compat_contextcompat_and_appcompat
Whats resources compat_contextcompat_and_appcompatWhats resources compat_contextcompat_and_appcompat
Whats resources compat_contextcompat_and_appcompat
 
Java interoperability
Java interoperabilityJava interoperability
Java interoperability
 
Interface and abstract classes
Interface and abstract classesInterface and abstract classes
Interface and abstract classes
 
Objects
ObjectsObjects
Objects
 
Inheritance
InheritanceInheritance
Inheritance
 
Initialization
InitializationInitialization
Initialization
 
Defining classes
Defining classesDefining classes
Defining classes
 
Strings & numbers
Strings & numbersStrings & numbers
Strings & numbers
 
Functions, anonymous functions and the function type
Functions, anonymous functions and the function typeFunctions, anonymous functions and the function type
Functions, anonymous functions and the function type
 
Google Play Instant: Improving User Engagement
Google Play Instant: Improving User EngagementGoogle Play Instant: Improving User Engagement
Google Play Instant: Improving User Engagement
 

Recently uploaded

5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
ihlasbinance2003
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
ssuser36d3051
 
Low power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniquesLow power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniques
nooriasukmaningtyas
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
SUTEJAS
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
NidhalKahouli2
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
gerogepatton
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
mamunhossenbd75
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
MIGUELANGEL966976
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 
Series of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.pptSeries of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.ppt
PauloRodrigues104553
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
bank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdfbank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdf
Divyam548318
 
PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
anoopmanoharan2
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
drwaing
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
wisnuprabawa3
 
Swimming pool mechanical components design.pptx
Swimming pool  mechanical components design.pptxSwimming pool  mechanical components design.pptx
Swimming pool mechanical components design.pptx
yokeleetan1
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
Hitesh Mohapatra
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
heavyhaig
 

Recently uploaded (20)

5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
 
Low power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniquesLow power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniques
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 
Series of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.pptSeries of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.ppt
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
bank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdfbank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdf
 
PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
 
Swimming pool mechanical components design.pptx
Swimming pool  mechanical components design.pptxSwimming pool  mechanical components design.pptx
Swimming pool mechanical components design.pptx
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
 

Variables, constants, types, and conditionals

  • 1. Variables, Constants, Types, and Conditionals Kotlin Study Group John
  • 2. Hi, I am John - Android Developer - Work on Rakuten Viki - Use Kotlin for 2 years - Linkedin: https://www.linkedin.com/in/chang-john-0a95237a/
  • 3. Outline - What’s the structure of declaring a variable? - Val v.s Var - Constants - Conditionals - If keyword - When keyword - Statement and Expression - Supplement - Inspecting Kotlin Bytecode - Range - String templates - Q&A
  • 4. What’s the structure of declaring a variable? var experiencePoints: Int = 5 val playerName: String = “Estragon” val name = “Madrigal” var healthPoint = 100 var name: String Why type can ignore? Because of type inference
  • 6. Val v.s Var Var usage Val usage Once variable is declared by val, it is read-only variable Once variable is declared by var, it is able to be modified variable We recommend that you use a val any time you do not need a var.
  • 7. Why need to distinguish var and val? - Almost all of cases on production, value(data) is decided already when variable is declared. For example, get response from backend or get information from database, generate financial report... - Avoid others to destroy your design For example, data class Person(val id: Int, val name: String) val p1 = Person(“123456789”, “John”) p1.name = “Johnny”
  • 8. What are benefits using val often? [Advanced] - When you need the variable, you just declare. - Due to feature of val is ready-only, you can track origin first on debug(easy debug) - Due to variable by val must assign value while you declare and disallow be modified, you will build constructor-injection architecture unawarely(easy testing) - According to above benefits, you will build a clear(immutable) codebase to decrease software complexity(easy maintain)
  • 9. Compile-Time Constant Cannot exist ‘const’ only Cannot correspond with ‘var’ keyword Cannot enclose on function Cannot declare non primitive type
  • 10. Compile-Time Constant Success - const is used for non-enclosed block(function, class) and primitive type Primitive Type: String, Int, Double, Float, Long, Short, Byte, Char, Boolean
  • 11. Conditionals - If keyword One line if expression so that remove braces Append comparison and logical operators comparison logical Support nested
  • 13. Conditionals - statement and expression This is a statement This is an expression
  • 14. What’s different when i use the expression? - Code become more straightforward statement expression
  • 15. What’s different when i use the expression? - Code become more exhaustive statement expression
  • 18. Range Ctrl + Shift + P can understand its type
  • 21. Q&A