

{"id":115613,"date":"2023-07-03T18:00:24","date_gmt":"2023-07-03T12:30:24","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=115613"},"modified":"2023-07-03T18:22:02","modified_gmt":"2023-07-03T12:52:02","slug":"kotlin-data-types-and-type-conversion","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/kotlin-data-types-and-type-conversion\/","title":{"rendered":"Kotlin Data Types and Type Conversion"},"content":{"rendered":"<p>Data types play a vital role in programming languages as they define the nature of data that can be stored and manipulated. Kotlin, a statically typed programming language, assigns a specific data type to each variable. This article explores the various data types available in Kotlin and discusses type conversion methods. Let&#8217;s start!!!<\/p>\n<h3>1. Kotlin Numeric Data Types:<\/h3>\n<p>Kotlin offers multiple numeric data types, such as integers, floating-point numbers, and characters.<\/p>\n<p><strong>For Example<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">val age: Int = 25\r\nval pop_songs: Long = 80000000L\r\nval pi: Double = 3.14159\r\nval weight: Float = 68.5f\r\nval grade: Char = 'M'\r\n<\/pre>\n<h4>a. Int:<\/h4>\n<p>The &#8216;Int&#8217; type represents signed integers, ranging from approximately -2 billion to 2 billion.<\/p>\n<p><strong>Example: <\/strong>`val age: Int = 25`<\/p>\n<h4>b. Long:<\/h4>\n<p>The &#8216;Long&#8217; type stores large integers, with a wider range compared to &#8216;Int&#8217;.<\/p>\n<p><strong>Example: <\/strong>`val pop_songs: Long = 80000000L`<\/p>\n<h4>c. Double:<\/h4>\n<p>The &#8216;Double&#8217; type handles floating-point numbers, supporting decimal places.<\/p>\n<p><strong>Example: <\/strong>`val pi: Double = 3.14159`<\/p>\n<h4>d. Float:<\/h4>\n<p>The &#8216;Float&#8217; type manages floating-point numbers with lower precision than &#8216;Double&#8217;.<\/p>\n<p><strong>Example: <\/strong>`val weight: Float = 68.5f`<\/p>\n<h4>e. Char:<\/h4>\n<p>The &#8216;Char&#8217; type represents a single character enclosed in single quotes.<\/p>\n<p><strong>Example: <\/strong>`val grade: Char = &#8216;M&#8217;`<\/p>\n<h3>2. Boolean Data Type in kotlin<\/h3>\n<p>The &#8216;Boolean&#8217; type allows variables to have only two possible values: <strong>&#8216;true&#8217; or &#8216;false&#8217;.<\/strong><\/p>\n<p><strong>For Example<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">val isStudent: Boolean = true\r\nval hasPassedExam = false\r\n<\/pre>\n<h3>3. Kotlin String Data Type:<\/h3>\n<p>The &#8216;String&#8217; type stores a sequence of characters.<\/p>\n<p><strong>Example: `val name: String = <\/strong>&#8220;Mugdha Hazra&#8221;`<\/p>\n<h3>Type Conversion in Kotlin<\/h3>\n<p>Type conversion, or typecasting, refers to converting a value from one data type to another. Kotlin provides implicit and explicit type conversion methods.<\/p>\n<h4>1. Implicit Conversion in Kotlin:<\/h4>\n<p>Implicit conversion occurs <strong>automatically<\/strong> when assigning a value of one type to a variable of another compatible type.<\/p>\n<p><strong>For example<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">val intValue: Int = 10\r\nval doubleValue: Double = intValue.toDouble()\r\n<\/pre>\n<p>In this implicit conversion example, an Int variable intValue is assigned the value 10. Then, the toDouble() function is called on intValue to convert it to a Double. The result is stored in the doubleValue variable.<\/p>\n<h4>2. Explicit Conversion in\u00a0 Kotlin:<\/h4>\n<p>Explicit conversion is necessary when converting a value of one type to another type that is not directly compatible. Kotlin offers various conversion functions for this purpose.<\/p>\n<p>Here&#8217;s an example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">val doubleValue: Double = 3.14\r\nval intValue: Int = doubleValue.toInt()\r\n\r\nval longValue: Long = 100L\r\nval floatValue: Float = longValue.toFloat()\r\n\r\nval charValue: Char = 'X'\r\nval intValue: Int = charValue.toInt()\r\n<\/pre>\n<p>In these explicit conversion examples, different numeric values are converted from one data type to another. doubleValue is explicitly converted from Double to Int using the toInt() function. Similarly, longValue is converted from Long to Float, and charValue is converted from Char to Int using the toInt() function.<\/p>\n<p>In the above code snippets, you can observe the declaration and usage of different data types in various scenarios. Furthermore, type conversion is demonstrated both implicitly and explicitly.<\/p>\n<h3>Conclusion:<\/h3>\n<p>Understanding data types and type conversion is essential for effective programming in Kotlin. By utilizing appropriate data types and employing implicit or explicit type conversion when necessary, you can ensure that your code operates correctly and efficiently.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Data types play a vital role in programming languages as they define the nature of data that can be stored and manipulated. Kotlin, a statically typed programming language, assigns a specific data type to&#46;&#46;&#46;<\/p>\n","protected":false},"author":581,"featured_media":115615,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27775],"tags":[27810,27811],"class_list":["post-115613","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-kotlin-tutorials","tag-kotlin-data-types","tag-kotlin-type-conversion"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Kotlin Data Types and Type Conversion - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn about various data types in kotlin like numeric, boolean and string with examples. See implicit and explicit type conversion in kotlin.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/kotlin-data-types-and-type-conversion\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Kotlin Data Types and Type Conversion - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn about various data types in kotlin like numeric, boolean and string with examples. See implicit and explicit type conversion in kotlin.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/kotlin-data-types-and-type-conversion\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-03T12:30:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-07-03T12:52:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/06\/kotlin-data-type-type-conversion.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Kotlin Data Types and Type Conversion - DataFlair","description":"Learn about various data types in kotlin like numeric, boolean and string with examples. See implicit and explicit type conversion in kotlin.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/kotlin-data-types-and-type-conversion\/","og_locale":"en_US","og_type":"article","og_title":"Kotlin Data Types and Type Conversion - DataFlair","og_description":"Learn about various data types in kotlin like numeric, boolean and string with examples. See implicit and explicit type conversion in kotlin.","og_url":"https:\/\/data-flair.training\/blogs\/kotlin-data-types-and-type-conversion\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-07-03T12:30:24+00:00","article_modified_time":"2023-07-03T12:52:02+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/06\/kotlin-data-type-type-conversion.webp","type":"image\/webp"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/kotlin-data-types-and-type-conversion\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/kotlin-data-types-and-type-conversion\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"Kotlin Data Types and Type Conversion","datePublished":"2023-07-03T12:30:24+00:00","dateModified":"2023-07-03T12:52:02+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/kotlin-data-types-and-type-conversion\/"},"wordCount":444,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/kotlin-data-types-and-type-conversion\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/06\/kotlin-data-type-type-conversion.webp","keywords":["Kotlin Data Types","Kotlin Type COnversion"],"articleSection":["Kotlin Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/kotlin-data-types-and-type-conversion\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/kotlin-data-types-and-type-conversion\/","url":"https:\/\/data-flair.training\/blogs\/kotlin-data-types-and-type-conversion\/","name":"Kotlin Data Types and Type Conversion - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/kotlin-data-types-and-type-conversion\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/kotlin-data-types-and-type-conversion\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/06\/kotlin-data-type-type-conversion.webp","datePublished":"2023-07-03T12:30:24+00:00","dateModified":"2023-07-03T12:52:02+00:00","description":"Learn about various data types in kotlin like numeric, boolean and string with examples. See implicit and explicit type conversion in kotlin.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/kotlin-data-types-and-type-conversion\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/kotlin-data-types-and-type-conversion\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/kotlin-data-types-and-type-conversion\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/06\/kotlin-data-type-type-conversion.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/06\/kotlin-data-type-type-conversion.webp","width":1200,"height":628,"caption":"kotlin data type & type conversion"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/kotlin-data-types-and-type-conversion\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Kotlin Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/kotlin-tutorials\/"},{"@type":"ListItem","position":3,"name":"Kotlin Data Types and Type Conversion"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam6\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/115613","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=115613"}],"version-history":[{"count":3,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/115613\/revisions"}],"predecessor-version":[{"id":115778,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/115613\/revisions\/115778"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/115615"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=115613"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=115613"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=115613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}