0
0
Kotlinprogramming~15 mins

Explicit type declaration in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Explicit type declaration
📖 Scenario: You are working on a simple Kotlin program that stores information about a book. You want to practice explicitly declaring variable types to make your code clear and easy to understand.
🎯 Goal: Create variables with explicit type declarations for a book's title, author, and number of pages. Then print these details.
📋 What You'll Learn
Create a variable title with type String and value "The Hobbit"
Create a variable author with type String and value "J.R.R. Tolkien"
Create a variable pages with type Int and value 310
Print the book details in the format: Title: The Hobbit, Author: J.R.R. Tolkien, Pages: 310
💡 Why This Matters
🌍 Real World
Explicit type declarations help make code easier to read and maintain, especially in larger projects where knowing the exact type of data is important.
💼 Career
Many Kotlin jobs require clear and maintainable code. Knowing how to declare explicit types is a fundamental skill for writing professional Kotlin applications.
Progress0 / 4 steps
1
Create variables with explicit types
Create a variable called title with type String and value "The Hobbit".
Kotlin
Need a hint?

Use val to declare a read-only variable and specify the type after a colon.

2
Add more variables with explicit types
Create a variable called author with type String and value "J.R.R. Tolkien".
Create a variable called pages with type Int and value 310.
Kotlin
Need a hint?

Remember to declare the type explicitly for both variables.

3
Create a formatted string with explicit type
Create a variable called details with type String that combines title, author, and pages into the format: "Title: The Hobbit, Author: J.R.R. Tolkien, Pages: 310".
Kotlin
Need a hint?

Use string templates with $ to insert variable values inside the string.

4
Print the book details
Print the variable details to display the book information.
Kotlin
Need a hint?

Use println(details) to show the book details on the screen.