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
- + — Addition Operator
- – — Subtraction Operator
- \ — Division Operator
- * — Multiplication Operator
- % — 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.