Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Type Conversion Basics | Type Inference and Conversion
Kotlin Data Types

bookType Conversion Basics

メニューを表示するにはスワイプしてください

Type conversion is a key feature in Kotlin that lets you change a value from one data type to another. You often need to convert between types like Int, Double, and String when working with user input, calculations, or displaying results. Kotlin does not perform implicit conversions between numeric types, so you must use explicit conversion functions. There are both safe and unsafe conversions. Safe conversions handle errors gracefully, while unsafe conversions can throw exceptions if the value cannot be converted.

Main.kt

Main.kt

copy

In the code above, you used several built-in conversion functions. The toDouble() function converts an Int to a Double, and toInt() converts a Double to an Int, dropping any decimal part. To convert an Int to a String, you used toString(). Converting a String to an Int is done with toInt(). However, if the string does not represent a valid integer, toInt() throws a NumberFormatException. This is an unsafe conversion. Always ensure the string contains only numeric characters before converting, or use safer alternatives like toIntOrNull() to avoid exceptions.

question mark

Which function would you use to convert a String to an Int in Kotlin?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 2.  3

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 2.  3
some-alt