Techoceanhub logo

Let’s study about operators in kotlin.Chapter-6

There are many operators in every language the same applies to Kotlin also. There are different operators in kotlin like arithmetic, assignment, comparison  operators etc.

Arithmetic operators

  1. +    —    Addition Operator
  2. –     —   Subtraction Operator
  3. \    —    Division Operator
  4. *    —    Multiplication Operator
  5. %  —    Modulus Operator
package adapter.manasvi.com.loopactivity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import java.lang.Math.log
//Arithmetic operators
class MainActivity : AppCompatActivity() {
var a:Int=8
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
a=a+1
textView.text= a.toString()
a=a-1
secondView.text=a.toString()
a=a*1
thirdView.text= a.toString()
a=a/2
fourthView.text=a.toString()
a=a%2
fifthView.text=a.toString()
}

}

Assignment Operators

Assignment Operator assigns a value to a variable. It is been denoted as =(equal) .

package adapter.manasvi.com.loopactivity

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
//Assignment Operators
class SecondActivity : AppCompatActivity() {
var a: Int = 8
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
a+=1
textView.text = a.toString()
a-= 1
secondView.text = a.toString()
a*= 1
thirdView.text = a.toString()
a/= 2
fourthView.text = a.toString()
a%= 2
fifthView.text = a.toString()
}
}

Increment and Decrement Operators

Increment or decrement operators are those operators, who are also known as pre or post-increment and pre or post decrement. (++ , –)

package adapter.manasvi.com.loopactivity

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
//Increment and Decrement Operators
class ThirdActivity : AppCompatActivity() {
var a: Int = 8
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
a++
textView.text = a.toString()
++a
secondView.text = a.toString()
--a
thirdView.text = a.toString()
a--
fourthView.text = a.toString()
}
}

Logical Operators

Logical operators are those who are also known as AND (&&), OR (||) and their way of function is defined below in terms of a simple example.

package adapter.manasvi.com.loopactivity

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class FourthActivity : AppCompatActivity() {
var firstValue: Int = 8
var secondValue:Int=8
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if(firstValue==secondValue || firstValue>=secondValue){
textView.text = "OR operator"
}
if(firstValue==secondValue && firstValue>=secondValue) {
textView.text = " And Operator"
}else{
textView.text = "default case"
}
}
}

Inequality/Equality Operators

These operators vary from >, <, <=, >=, !=, == and their way of functioning is defined below in terms of simple three examples.

package adapter.manasvi.com.loopactivity
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class FourthActivity : AppCompatActivity() {
  var firstValue: Int = 8
  var secondValue:Int=9
  override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
      if(firstValue<secondValue){
textView.text = "secondValue is greater than firstValue"
      }else if (firstValue>secondValue) {
          textView.text = "firstValue is greater than secondValue"
      }else{
          textView.text = "default case"
      }
  }
}
package adapter.manasvi.com.loopactivity
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class FourthActivity : AppCompatActivity() {
var firstValue: Int = 8
  var secondValue:Int=9
  override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
      if(firstValue<=secondValue){
          textView.text = "secondValue is greater than firstValue"
      }else if (firstValue>=secondValue) {
          textView.text = "firstValue is greater than secondValue"
      }else{
          textView.text = "default case"
      }
  }
}
package adapter.manasvi.com.loopactivity
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class FourthActivity : AppCompatActivity() {
  var firstValue: Int = 8
  var secondValue:Int=8
  override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
      if(firstValue==secondValue){
          textView.text = "secondValue is equal to firstValue"
      }else if (firstValue!=secondValue) {
          textView.text = "firstValue is not equal to secondValue"
      }else{
          textView.text = "default case"
      }
  }
}

in operator in Kotlin

“in” operator in Koltin is used to check the existence of particular variable or property in a Range or Collection and vice versa.

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class doActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
for(i in 1..10){
textView.text=i.toString()
}
}
}

Elvis operator in Kotlin

Elvis operator returns null even when the expression is null and it is also used to check the null safety of values. Moreover, this operator is represented as  “?” This operator overall is used to minimize NullPointerException.

var name: String? = null
var str: String? = "Samantha Nargis"

The above example shows how to use the Elvis operator.  It will always check whether that variable contains null or not. It performs check by using “?” .It is fancily called the null coalescing operator.

Feel free to read and comment:)

Share:

Categories

Archives

Related Posts

Array Destructuring

Array Destructuring Array destructuring is a powerful syntax introduced in ES6 that allows you to unpack elements from an array

Read More »

Explore our Affiliated partner links

hostgator website plan

Hostgator

Best Website hosting plans & Best for 24/7 support.

hostinger webhosting plan

Hostinger

Reasonable website hosting packages for small business & 24/7 Email support

Elementor pro

Design Worpdress website quickly using Elementor Builder and AI

buy elementor website templates

Buy Elementor Website Template

Buy ready made Elementor Website Template from us & Get Free Elementor pro licence for 1 year .