Android App Development Mastery From Scratch
Introduction to Programming
Giving the Computer Instructions
At its heart, programming is about giving a computer a set of instructions to follow. Think of it like a very detailed, logical recipe. The computer will follow your instructions exactly as you write them, so clarity is key. We'll be using a language called Kotlin, which is modern, readable, and the preferred language for building Android apps.
Variables and Data Types
To work with information, we need a way to store it. In programming, we use variables for this. A variable is like a labeled box where you can keep a piece of data. You give the box a name, and you can look at what's inside or change its contents later.
In Kotlin, there are two main ways to declare a variable:
// `val` is for a value that won't change (immutable).
// Think of it as a constant.
val name = "Alice"
// `var` is for a value that can change (mutable).
var score = 0
score = 10 // This is okay
Notice that we can change the value of score, but if we tried to change name, the program would give us an error. Using val whenever possible is a good practice, as it makes code safer and easier to understand.
Every piece of data has a type. Kotlin is smart and can usually figure out the type on its own, but it's good to know the most common ones.
| Data Type | What it Represents | Example |
|---|---|---|
String | Text | "Hello, world!" |
Int | Whole numbers | 42 |
Double | Numbers with decimals | 3.14159 |
Boolean | True or false values | true |
When you create a variable, it gets assigned a type. The variable name from our earlier example is a String, and score is an Int.
Controlling the Flow
Programs rarely run straight from top to bottom. Often, they need to make decisions or repeat actions. This is called control flow.
The most basic way to make a decision is with an if/else statement. It checks if a condition is true. If it is, the program does one thing. If not, it can do something else.
val temperature = 15
if (temperature > 20) {
println("It's a warm day!")
} else {
println("It's a bit chilly.")
}
What if you need to do something over and over? That's where loops come in. A for loop is great for repeating an action for each item in a collection, like a list of names.
val names = listOf("Anna", "Ben", "Carla")
for (name in names) {
println("Hello, $name!")
}
This code will go through the list names one by one, printing a greeting for each person.
Functions and Methods
As your programs get bigger, you'll find yourself writing the same bits of code repeatedly. Functions let you package up a piece of logic, give it a name, and reuse it whenever you need it. This keeps your code organized and easy to manage.
A function can take in data, called parameters, and can also return a result.
// This function takes one parameter, `name`, which is a String.
// It doesn't return any value (indicated by `Unit`, which can be omitted).
fun greet(name: String) {
println("Hello, $name!")
}
// To use the function, you "call" it:
greet("David") // Prints "Hello, David!"
Here's a function that takes two numbers and returns their sum. The : Int after the parentheses specifies that this function will return an integer value.
fun add(a: Int, b: Int): Int {
return a + b
}
val sum = add(5, 3) // The variable `sum` will now hold the value 8.
println(sum)
Classes and Objects
Kotlin is an object-oriented programming (OOP) language. This is a powerful way to structure your code by modeling things from the real world. The central idea is to bundle data and the functions that operate on that data together.
A class is like a blueprint. For example, you could create a Dog class. The blueprint would specify that every dog has properties (data) like a name and age, and can perform actions (methods, which are functions inside a class) like bark().
class Dog(val name: String, var age: Int) {
fun bark() {
println("Woof! My name is $name.")
}
}
An object is a specific instance created from that blueprint. So, you could have a dog named Fido and another named Lucy, both created from the Dog class. They are separate objects, each with their own name and age.
// Create two Dog objects from the Dog class
val fido = Dog("Fido", 3)
val lucy = Dog("Lucy", 5)
// Call the bark() method on each object
fido.bark() // Prints "Woof! My name is Fido."
lucy.bark() // Prints "Woof! My name is Lucy."
This approach of using classes and objects helps organize complex programs into understandable, reusable pieces. It's a fundamental concept you'll use constantly when building Android apps.
In Kotlin, what is the key difference between declaring a variable with val and var?
What is the primary purpose of a function in programming?
These are the essential building blocks of programming in Kotlin. With variables, control flow, functions, and classes, you have the tools to start writing your own simple programs.