

{"id":115610,"date":"2023-06-27T09:00:04","date_gmt":"2023-06-27T03:30:04","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=115610"},"modified":"2023-06-27T11:29:30","modified_gmt":"2023-06-27T05:59:30","slug":"operators-in-kotlin","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/operators-in-kotlin\/","title":{"rendered":"Operators in Kotlin"},"content":{"rendered":"<p>Kotlin provides programmers with a diverse set of operators to manipulate and perform various operations on data. Operators are symbols or keywords that enable developers to carry out computations, comparisons, and other operations within their code. This article will dive into the different types of operators available in Kotlin.<\/p>\n<h3>Arithmetic Operators in Kotlin<\/h3>\n<p>Arithmetic operators in Kotlin enable programmers to perform fundamental mathematical operations, including addition, subtraction, multiplication, division, and modulus. These operators provide the ability to manipulate numeric values and perform calculations within Kotlin programs.<\/p>\n<p><strong>For Example<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">fun main() {\r\n    var a = 10\r\n    var b = 5\r\n\r\n    val sum = a + b\r\n    println(\"Sum: $sum\")\r\n\r\n    val difference = a - b\r\n    println(\"Difference: $difference\")\r\n\r\n    val product = a * b\r\n    println(\"Product: $product\")\r\n\r\n    val quotient = a \/ b\r\n    println(\"Quotient: $quotient\")\r\n\r\n    val remainder = a % b\r\n    println(\"Remainder: $remainder\")\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Sum: 15<br \/>\nDifference: 5<br \/>\nProduct: 50<br \/>\nQuotient: 2<br \/>\nRemainder: 0<\/div>\n<h4>Explanation:<\/h4>\n<p>1. The addition operator (+) combines two numbers and returns their sum.<\/p>\n<p>2. The subtraction operator (-) subtracts the second number from the first and yields the difference.<\/p>\n<p>3. The multiplication operator (*) performs multiplication on two numbers and produces their product.<\/p>\n<p>4. The division operator (\/) divides the first number by the second and provides the quotient as the result.<\/p>\n<p>5. The modulus operator (%) calculates the remainder when the first number is divided by the second.<\/p>\n<h3>Assignment Operators in Kotlin<\/h3>\n<p>Assignment operators are utilized to assign values to variables, combining the assignment operation with other operators.<\/p>\n<p>Like a+=b means a = a+b, a-=b means a=a-b etc.<\/p>\n<p><strong>For Example<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">fun main() {\r\n    var a = 10\r\n\r\n    a += 5\r\n    println(\"Addition Assignment: $a\")\r\n\r\n    a -= 3\r\n    println(\"Subtraction Assignment: $a\")\r\n\r\n    a *= 2\r\n    println(\"Multiplication Assignment: $a\")\r\n\r\n    a \/= 4\r\n    println(\"Division Assignment: $a\")\r\n\r\n    a %= 3\r\n    println(\"Modulus Assignment: $a\")\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Addition Assignment: 15<br \/>\nSubtraction Assignment: 12<br \/>\nMultiplication Assignment: 24<br \/>\nDivision Assignment: 6<br \/>\nModulus Assignment: 0<\/div>\n<h4>Explanation:<\/h4>\n<p>1. The compound assignment operator `+=` combines the value on the right-hand side with the existing value of the left-hand variable and updates the variable with the result.<\/p>\n<p>2. The compound assignment operator `-=` subtracts the value on the right-hand side from the current value of the left-hand variable and assigns the new result to the variable.<\/p>\n<p>3. The compound assignment operator `*=` multiplies the value on the right-hand side with the current value of the left-hand variable and stores the updated result in the same variable.<\/p>\n<p>4. The compound assignment operator `\/=` divides the current value of the left-hand variable by the value on the right-hand side and assigns the quotient to the variable.<\/p>\n<p>5. The compound assignment operator `%=` calculates the remainder when the current value of the left-hand variable is divided by the value on the right-hand side, and assigns the remainder to the variable.<\/p>\n<p>These compound assignment operators provide a concise way to perform an operation and update the value of a variable in a single step, improving code readability and efficiency.<\/p>\n<h3>Comparison Operators in Kotlin<\/h3>\n<p>Comparison operators are employed to compare values, returning a Boolean result indicating the relationship between them.<\/p>\n<p><strong>For Example<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">fun main() {\r\n    val a = 10\r\n    val b = 5\r\n\r\n    val isEqual = (a == b)\r\n    println(\"Equal: $isEqual\")\r\n\r\n    val isNotEqual = (a != b)\r\n    println(\"Not Equal: $isNotEqual\")\r\n\r\n    val isGreater = (a &gt; b)\r\n    println(\"Greater Than: $isGreater\")\r\n\r\n    val isLess = (a &lt; b)\r\n    println(\"Less Than: $isLess\")\r\n\r\n    val isGreaterOrEqual = (a &gt;= b)\r\n    println(\"Greater Than or Equal: $isGreaterOrEqual\")\r\n\r\n    val isLessOrEqual = (a &lt;= b)\r\n    println(\"Less Than or Equal: $isLessOrEqual\")\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Equal: false<br \/>\nNot Equal: true<br \/>\nGreater Than: true<br \/>\nLess Than: false<br \/>\nGreater Than or Equal: true<br \/>\nLess Than or Equal: false<\/div>\n<h4>Explanation:<\/h4>\n<p>1. The == operator checks if the two operands are equal.<\/p>\n<p>2. The != operator checks if the two operands are not equal.<\/p>\n<p>3. The &gt; operator verifies if the left-hand operand is greater than the right-hand operand.<\/p>\n<p>4. The &lt; operator checks if the left-hand operand is less than the right-hand operand.<\/p>\n<p>5. The &gt;= operator checks if the left-hand operand is greater than or equal to the right-hand operand.<\/p>\n<p>6. The &lt;= operator checks if the left-hand operand is less than or equal to the right-hand operand.<\/p>\n<h3>Conclusion:<\/h3>\n<p>In this article, we have explored the various operators available in Kotlin. We covered arithmetic operators for basic mathematical operations, assignment operators for combining assignments with other operations, and comparison operators for comparing values. Understanding and effectively utilizing these operators empower Kotlin developers to write concise and expressive code that can perform a wide range of operations within their applications.<\/p>\n<p>Happy Coding! \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Kotlin provides programmers with a diverse set of operators to manipulate and perform various operations on data. Operators are symbols or keywords that enable developers to carry out computations, comparisons, and other operations within&#46;&#46;&#46;<\/p>\n","protected":false},"author":581,"featured_media":115612,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27775],"tags":[27807,27808,27809,27806],"class_list":["post-115610","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-kotlin-tutorials","tag-kotlin-arithmetic-operators","tag-kotlin-assignment-operators","tag-kotlin-comparison-operators","tag-operators-in-kotlin"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Operators in Kotlin - DataFlair<\/title>\n<meta name=\"description\" content=\"See operators in Kotlin with examples. See arithmetic operators, assignment operators and comparison operators with syntax and examples.\" \/>\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\/operators-in-kotlin\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Operators in Kotlin - DataFlair\" \/>\n<meta property=\"og:description\" content=\"See operators in Kotlin with examples. See arithmetic operators, assignment operators and comparison operators with syntax and examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/operators-in-kotlin\/\" \/>\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-06-27T03:30:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-06-27T05:59:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/06\/kotlin-operators.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=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Operators in Kotlin - DataFlair","description":"See operators in Kotlin with examples. See arithmetic operators, assignment operators and comparison operators with syntax and examples.","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\/operators-in-kotlin\/","og_locale":"en_US","og_type":"article","og_title":"Operators in Kotlin - DataFlair","og_description":"See operators in Kotlin with examples. See arithmetic operators, assignment operators and comparison operators with syntax and examples.","og_url":"https:\/\/data-flair.training\/blogs\/operators-in-kotlin\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-06-27T03:30:04+00:00","article_modified_time":"2023-06-27T05:59:30+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/06\/kotlin-operators.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":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/operators-in-kotlin\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/operators-in-kotlin\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"Operators in Kotlin","datePublished":"2023-06-27T03:30:04+00:00","dateModified":"2023-06-27T05:59:30+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/operators-in-kotlin\/"},"wordCount":570,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/operators-in-kotlin\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/06\/kotlin-operators.webp","keywords":["kotlin arithmetic operators","kotlin assignment operators","kotlin comparison operators","Operators in Kotlin"],"articleSection":["Kotlin Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/operators-in-kotlin\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/operators-in-kotlin\/","url":"https:\/\/data-flair.training\/blogs\/operators-in-kotlin\/","name":"Operators in Kotlin - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/operators-in-kotlin\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/operators-in-kotlin\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/06\/kotlin-operators.webp","datePublished":"2023-06-27T03:30:04+00:00","dateModified":"2023-06-27T05:59:30+00:00","description":"See operators in Kotlin with examples. See arithmetic operators, assignment operators and comparison operators with syntax and examples.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/operators-in-kotlin\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/operators-in-kotlin\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/operators-in-kotlin\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/06\/kotlin-operators.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/06\/kotlin-operators.webp","width":1200,"height":628,"caption":"kotlin operators"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/operators-in-kotlin\/#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":"Operators in Kotlin"}]},{"@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\/115610","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=115610"}],"version-history":[{"count":3,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/115610\/revisions"}],"predecessor-version":[{"id":115776,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/115610\/revisions\/115776"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/115612"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=115610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=115610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=115610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}