0
0
Kotlinprogramming~10 mins

Int, Long, Float, Double number types in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Int, Long, Float, Double number types
Start
Declare variable with type
Assign value
Use variable in expression
Check type limits
Perform operations
End
This flow shows how Kotlin variables of types Int, Long, Float, and Double are declared, assigned, and used in operations while respecting their value limits.
Execution Sample
Kotlin
val a: Int = 100
val b: Long = 10000000000L
val c: Float = 3.14F
val d: Double = 3.1415926535
println(a)
println(b)
This code declares variables of different number types and prints two of them.
Execution Table
StepActionVariableValueTypeOutput
1Declare and assigna100Int
2Declare and assignb10000000000Long
3Declare and assignc3.14Float
4Declare and assignd3.1415926535Double
5Print aa100Int100
6Print bb10000000000Long10000000000
7End
💡 All variables declared and printed successfully, program ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
aundefined100100100100100
bundefinedundefined10000000000100000000001000000000010000000000
cundefinedundefinedundefined3.143.143.14
dundefinedundefinedundefinedundefined3.14159265353.1415926535
Key Moments - 3 Insights
Why do we add 'L' after the number when declaring a Long?
In Kotlin, adding 'L' tells the compiler the number is a Long type, not an Int. See execution_table step 2 where '10000000000L' is assigned to b.
Why do we add 'F' after the number when declaring a Float?
Adding 'F' tells Kotlin the number is a Float, not a Double. Without 'F', the number is treated as Double by default. See execution_table step 3.
Can we assign a decimal number to an Int variable?
No, Int only holds whole numbers. Decimal numbers must use Float or Double. See execution_table steps 1 and 3 for difference.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2. What is the value and type of variable 'b'?
A10000000000L, Int
B10000000000, Int
C10000000000, Long
D10000000000F, Float
💡 Hint
Check the 'Value' and 'Type' columns at step 2 in execution_table.
At which step is the Float variable 'c' assigned a value?
AStep 3
BStep 1
CStep 4
DStep 2
💡 Hint
Look for variable 'c' and its value assignment in execution_table.
If we remove the 'F' suffix from the Float declaration, what will happen?
AIt will be treated as Int.
BIt will be treated as Double and cause a type mismatch error.
CIt will still be Float without any issue.
DIt will cause a syntax error.
💡 Hint
Refer to key_moments about Float declaration and suffix usage.
Concept Snapshot
Kotlin number types:
- Int: 32-bit whole numbers
- Long: 64-bit whole numbers (use 'L' suffix)
- Float: 32-bit decimal numbers (use 'F' suffix)
- Double: 64-bit decimal numbers (default for decimals)
Use suffixes to specify type explicitly.
Full Transcript
This visual execution shows how Kotlin handles number types Int, Long, Float, and Double. Variables are declared with explicit types and assigned values. Long numbers require an 'L' suffix to distinguish from Int. Float numbers require an 'F' suffix to distinguish from Double, which is the default for decimal numbers. The execution table traces each step: declaring variables, assigning values, and printing outputs. The variable tracker shows how each variable's value is set and remains unchanged. Key moments clarify why suffixes are needed and why decimal numbers cannot be assigned to Int. The quiz tests understanding of variable types, assignments, and suffix usage. The snapshot summarizes the key rules for Kotlin number types.