Deep Kotlin tutorial covering Int, Long, Double, Boolean with examples, diagram, mistakes, and interview answers.
Basic Data Types and Type Inference
What is Basic Data Types and Type Inference?
Basic Data Types and Type Inference is an important Kotlin topic from 1.1 Kotlin Language Fundamentals. In this lesson we focus only on the concepts that belong together: Int, Long, Double, Boolean, String, type inference. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas.
Why students should learn it
- Understand Basic Data Types and Type Inference in simple language
- Use Int in real Kotlin or Android code
- Explain Basic Data Types and Type Inference in interviews with examples and trade-offs
How it works
Basic Data Types and Type Inference: Int, Long, Double, Boolean, String, type inference works through Kotlin's core syntax rules. The compiler infers types where it can, expressions such as if and when can produce a value directly instead of only running a side effect, and functions are written to be small and named so the intent is obvious from the signature without reading the body.
- Identify what problem Basic Data Types and Type Inference: Int, Long, Double, Boolean, String, type inference solves.
- Write the smallest Kotlin snippet that demonstrates it.
- Add one Android or JVM use case where it matters.
- Compare it with a similar concept so the difference is clear.
- Finish by naming one common mistake and the safer alternative.
Interview tip: keep the answer simple first, then add example, trade-off, and mistake.
Types / Classification
- Int: core concept in this lesson
- Long: core concept in this lesson
- Double: core concept in this lesson
- Boolean: core concept in this lesson
- String: core concept in this lesson
- type inference: core concept in this lesson
- Statement: performs an action without producing a value
- Expression: if/when/try can produce a value directly
Important differences
| Concept 1 | Concept 2 | How to remember it |
|---|---|---|
| if statement | if expression | A statement just runs code; an expression also produces a value you can assign or return. |
| Positional argument | Named argument | Positional relies on order; named argument is explicit and safer with many parameters. |
| Function | Lambda | A function has a name and is called by that name; a lambda is an anonymous function value you can pass around. |
Diagram
flowchart TD
A["1.1 Kotlin Language Fundamentals"] --> B["Basic Data Types and Type Inference"]
B --> C["Why it exists"]
C --> D["How it works in Kotlin"]
D --> E["Android or JVM example"]
E --> F["Mistakes and interview answer"]
Examples
Named and default parameters
fun greet(name: String, greeting: String = "Hello"): String {
return "$greeting, $name!"
}
greet(name = "Asha")
when as an expression
fun levelLabel(score: Int): String = when {
score >= 90 -> "Advanced"
score >= 60 -> "Intermediate"
else -> "Beginner"
}
When to use it
Use it when: Reach for this when it makes your code clearer, safer, or easier for the next developer to follow.
Avoid it when: Skip it when a simpler, more familiar approach already solves the problem without extra ceremony.
Common mistakes
- Writing long positional parameter lists instead of using named arguments for clarity
- Using var when the value never actually changes
- Copy-pasting if/else chains instead of a single when expression
- Ignoring type inference and writing redundant explicit types everywhere
- Writing a multi-line lambda where a named function would read better
Interview questions
Read each answer in four parts: simple answer, code example, trade-off, and common mistake.
1. What is Basic Data Types and Type Inference in 1.1 Kotlin Language Fundamentals?
Answer: Basic Data Types and Type Inference is an important Kotlin topic from 1.1 Kotlin Language Fundamentals. In this lesson we focus only on the concepts that belong together: Int, Long, Double, Boolean, String, type inference. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas.
fun greet(name: String, greeting: String = "Hello"): String {
return "$greeting, $name!"
}
greet(name = "Asha")- Trade-off: if statement and if expression solve related but different problems: A statement just runs code; an expression also produces a value you can assign or return.
- Common mistake: Writing long positional parameter lists instead of using named arguments for clarity
2. How does Basic Data Types and Type Inference work in Kotlin or Android code?
Answer: Basic Data Types and Type Inference: Int, Long, Double, Boolean, String, type inference works through Kotlin's core syntax rules. The compiler infers types where it can, expressions such as if and when can produce a value directly instead of only running a side effect, and functions are written to be small and named so the intent is obvious from the signature without reading the body.
fun levelLabel(score: Int): String = when {
score >= 90 -> "Advanced"
score >= 60 -> "Intermediate"
else -> "Beginner"
}- Trade-off: if statement and if expression solve related but different problems: A statement just runs code; an expression also produces a value you can assign or return.
- Common mistake: Using var when the value never actually changes
3. What is one common mistake with Basic Data Types and Type Inference?
Answer: A common mistake is: Writing long positional parameter lists instead of using named arguments for clarity Another is: Using var when the value never actually changes
fun greet(name: String, greeting: String = "Hello"): String {
return "$greeting, $name!"
}
greet(name = "Asha")- Trade-off: if statement and if expression solve related but different problems: A statement just runs code; an expression also produces a value you can assign or return.
- Common mistake: Writing long positional parameter lists instead of using named arguments for clarity
4. How would you explain Basic Data Types and Type Inference with a small example?
Answer: Basic Data Types and Type Inference is an important Kotlin topic from 1.1 Kotlin Language Fundamentals. In this lesson we focus only on the concepts that belong together: Int, Long, Double, Boolean, String, type inference. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas. Here is a short example that shows it in practice.
fun greet(name: String, greeting: String = "Hello"): String {
return "$greeting, $name!"
}
greet(name = "Asha")- Trade-off: if statement and if expression solve related but different problems: A statement just runs code; an expression also produces a value you can assign or return.
- Common mistake: Using var when the value never actually changes
5. What is the trade-off of using Basic Data Types and Type Inference?
Answer: if statement and if expression solve related but different problems: A statement just runs code; an expression also produces a value you can assign or return.
fun levelLabel(score: Int): String = when {
score >= 90 -> "Advanced"
score >= 60 -> "Intermediate"
else -> "Beginner"
}- Trade-off: if statement and if expression solve related but different problems: A statement just runs code; an expression also produces a value you can assign or return.
- Common mistake: Writing long positional parameter lists instead of using named arguments for clarity
Practice task
Write one Kotlin example for Basic Data Types and Type Inference. Then explain it using: what is it, how it works, where it is used, mistake to avoid, and one interview answer.