![]() ![]() This rule also applies to the other arithmetic operators. In Kotlin, you can apply the * operator to mixed types. Kotlin will allow you to multiply these values without any conversion, like so: val totalCost: Double = hourlyRate * hoursWorked In this example, hoursWorked is explicitly converted to a Double, to match the type of hourlyRate. Val totalCost: Double = hourlyRate * hoursWorked.toDouble() You might think you have to do something like this: val hourlyRate: Double = 19.5 But what if you have an integer that you want to multiply by a double? So far, you’ve only seen operators acting independently on integers or doubles. Kotlin wants to make sure you know what you’re doing and that you may end up losing data by performing the type conversion. This is why it’s important to be explicit. Note: In this case, assigning the decimal value to the integer results in a loss of precision: The integer variable ends up with the value 12 instead of 12.5. The assignment now tells Kotlin unequivocally that you want to convert from the original type, Double, to the new type, Int. You do it like so: integer = decimal.toInt() Instead of simply assigning, you need to explicitly say that you want to convert the type. If you want the conversion to happen, you have to say so! In Kotlin, that includes being explicit about type conversions. Remember, computers rely on us programmers to tell them what to do. Kotlin disallows you from assigning a value of one type to another and avoids these issues. Experience shows this kind of automatic conversion is a source of software bugs and often hurts performance. Some programming languages aren’t as strict and will perform conversions like this automatically. Kotlin will complain if you try to do this and will spit out the following error: Type mismatch. The naïve way to attempt this would be like so: var integer: Int = 100 Sometimes you’ll have data in one format and need to convert it to another. You’ll also learn about Pair and Triple, which allow you to make your own types made up of two or three values of any type, respectively.įinally, you’ll learn about the important types Any, Unit, and Nothing. You’ll learn about converting between types and you’ll also be introduced to type inference which makes your life as a programmer a lot simpler. ![]() In this chapter, you’ll learn about handling different types, including strings which allow you to represent text. Formally, a type describes a set of values and the operations that can be performed on them. Now that you know how to perform basic operations and manipulate data using operations, it’s time to learn more about types. Kotlin/Java Interoperabilityġ9.3 Making your Kotlin Code Java-friendlyġ9.5 “Static” values and functions from KotlinĢ0.5 Difference between Java and Kotlin exceptionsĢ1.3 First-class and higher-order functions Section IV: Intermediate Topics Section 4: 9 chapters Show chapters Hide chapters Section III: Building Your Own Types Section 3: 8 chapters Show chapters Hide chaptersġ1.3 Understanding state and side effectsġ8.1 Anatomy of standard library generic typesġ8.2 Extension functions on types with generic constraintsġ8.3 Creating your own generic constraintsġ8.6 Generic type variance (a.k.a., in and out declarations) Section I: Kotlin Basics Section 1: 7 chapters Show chapters Hide chapters ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |