Numbers
Part of the Fundamentals section of Coddy's Kotlin journey. Lesson 7 of 93.
Use an Int for whole numbers and a Double for fractional numeric values. Kotlin infers these types from the literal:
val seats = 6
val unitPrice = 2.75
println(seats) // 6
println(unitPrice) // 2.75An Int has a limited range. Use Long for larger whole numbers, with an L suffix on the literal:
val distance: Long = 3000000000L
println(distance) // 3000000000Numeric literals have no quotes. A decimal point gives a fractional numeric type, while quotes would create text. Choose a name that describes the number and use val when you do not need reassignment.
Challenge
BeginnerWrite code that initializes a value named myVar with the value 5.
- Replace the
?with the correct value - Lines starting with
//are comments. They explain what to do but don't affect your code - Only change the line that says
val myVar = ?
Try it yourself
fun main() {
// Type your code below
val myVar = ?
// Don't change the line below
println("myVar = $myVar")
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorAugmented AssignmentComparison OperatorsRecap - Simple Math7Basic IO
Println FunctionString TemplatesReadLine InputType ConversionRecap - Years Until RetirementRecap - True or False10Functions
Declare A FunctionParameters And ArgumentsReturn ValuesNamed ArgumentsDefault ValuesSingle Expression FunctionsRecap - Sigma FunctionRecap - Validation Function2Variables
Val vs VarType InferenceNumbersStringBooleanNaming ConventionsRecap - Initialize Variables5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Logical Operators Part 3Ternary With If ExpressionRecap - Simple Logic8Bill Split Calculator
Welcome MessageGetting Input3Nullability
What Is Null SafetyNullable TypesSafe Call OperatorElvis OperatorFunction Challenge BasicsNot Null AssertionRecap - Safe Access6Decision Making
If StatementIf - ElseIf As An ExpressionWhen ExpressionWhen With RangesRecap - Simple Calculator9Loops
For LoopWhile LoopDo-While LoopBreakContinueRanges In LoopsNested LoopRecap - FactorialRecap - Dynamic InputPractice on your own: Kotlin playground