Variables & Data Types
Learn how to declare variables with val and var, understand Kotlin's basic types (Int, String, Boolean, Double), and write your first Kotlin program.
Variables & Data Types is a free Android Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Welcome to Kotlin
Kotlin is the official language for Android development, created by JetBrains and backed by Google.
- Concise: less boilerplate than Java
- Safe: null safety built in
- 100% interoperable with Java
Every Android app you write today starts with Kotlin.
val vs var
Kotlin has two ways to declare a variable:
val— immutable (read-only). Use this by default.var— mutable (can be reassigned).
Prefer val whenever possible — it prevents accidental changes.
Declaring Variables
Run this example to see val and var in action:
fun main() {
val name = "Android"
var version = 14
println(name) // Android
println(version) // 14
version = 15 // OK — var is mutable
println(version) // 15
// name = "iOS" // ERROR — val is immutable
}Basic Data Types
Kotlin's most common types:
Int— whole numbers (e.g. 42)Double— decimals (e.g. 3.14)String— text (e.g. "Hello")Boolean— true or false
Kotlin infers the type from the value — you rarely need to write it explicitly.
Type Inference Example
Kotlin figures out the type automatically:
fun main() {
val age: Int = 25 // explicit
val pi = 3.14 // inferred Double
val greeting = "Hi!" // inferred String
val isActive = true // inferred Boolean
println(age)
println(pi)
println(greeting)
println(isActive)
}String Templates
String templates let you embed variables inside strings with $:
"Hello, $name"— embeds a variable"${user.firstName}"— embeds an expression
No more string concatenation with +.
String Templates in Action
Use $ and ${} to build strings:
fun main() {
val name = "Alice"
val score = 98
println("Hello, $name!")
println("Your score: $score")
println("Double your score: ${score * 2}")
val msg = "$name scored $score points"
println(msg)
}Numeric Operations
Kotlin supports all standard arithmetic operators:
+add,-subtract*multiply,/divide%remainder (modulo)
Integer division truncates: 7 / 2 = 3, not 3.5.
Constants with const val
Use const val for compile-time constants — values known before the app runs:
- Must be a primitive type or String
- Defined at the top level or inside an
object - More efficient than regular
val
Quick Check
Which keyword should you use when a variable's value will never change?
Recap: Variables & Types
Great work! Here's what you learned:
val= immutable,var= mutable — preferval- Kotlin infers types automatically
- Common types: Int, Double, String, Boolean
- String templates with
$and${}
Next: writing reusable code with functions and lambdas.
Frequently asked questions
Is the “Variables & Data Types” lesson free?
Yes — the full text of “Variables & Data Types” is free to read here on the web, and the Android Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.
What will I learn in “Variables & Data Types”?
Learn how to declare variables with val and var, understand Kotlin's basic types (Int, String, Boolean, Double), and write your first Kotlin program. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Android Academy?
No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Variables & Data Types” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Android Academy lesson?
Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Variables & Data Types
- Functions & Lambdas
- Control Flow