package com.example.datatypesdemo
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Explicitly typed variables
val explicitInt: Int = 42
val explicitString: String = "Hello Kotlin"
val explicitBoolean: Boolean = true
val explicitDouble: Double = 3.14
// Inferred type variables
val inferredInt = 100
val inferredString = "Type Inference"
val inferredBoolean = false
val inferredDouble = 2.718
val showButton: Button = findViewById(R.id.showButton)
val explicitIntValue: TextView = findViewById(R.id.explicitIntValue)
val explicitStringValue: TextView = findViewById(R.id.explicitStringValue)
val explicitBooleanValue: TextView = findViewById(R.id.explicitBooleanValue)
val explicitDoubleValue: TextView = findViewById(R.id.explicitDoubleValue)
val inferredIntValue: TextView = findViewById(R.id.inferredIntValue)
val inferredStringValue: TextView = findViewById(R.id.inferredStringValue)
val inferredBooleanValue: TextView = findViewById(R.id.inferredBooleanValue)
val inferredDoubleValue: TextView = findViewById(R.id.inferredDoubleValue)
showButton.setOnClickListener {
explicitIntValue.text = explicitInt.toString()
explicitStringValue.text = explicitString
explicitBooleanValue.text = explicitBoolean.toString()
explicitDoubleValue.text = explicitDouble.toString()
inferredIntValue.text = inferredInt.toString()
inferredStringValue.text = inferredString
inferredBooleanValue.text = inferredBoolean.toString()
inferredDoubleValue.text = inferredDouble.toString()
}
}
}This app screen demonstrates Kotlin data types and type inference.
We declare variables with explicit types like Int, String, Boolean, and Double. Then we declare variables without specifying types, letting Kotlin infer them automatically.
When the user taps the 'Show Values' button, the app displays the values of all variables below their labels. This shows how Kotlin handles both explicit and inferred types clearly.