Jenis-jenis Operator pada Kotlin (Kotlin Operators)

Pada tutorial kali ini kita akan membahas tentang Kotlin Operators (bagaimana jenis-jenis operator pada kotlin). Operator pada Kotlin terdiri dari Operator Aritmatic, Assignment Operator, Increment/Decrement, Comparison and Equality Operators, Logical Operators dan operator lainnya.

1. Aritmatic Operators

fun main(args: Array<String>) {
 
    val number1 = 12.5
    val number2 = 3.5
    var result: Double
 
    result = number1 + number2
    println("number1 + number2 = $result")
 
    result = number1 - number2
    println("number1 - number2 = $result")
 
    result = number1 * number2
    println("number1 * number2 = $result")
 
    result = number1 / number2
    println("number1 / number2 = $result")
 
    result = number1 % number2
    println("number1 % number2 = $result")
}

Output:

number1 + number2 = 16.0
number1 - number2 = 9.0
number1 * number2 = 43.75
number1 / number2 = 3.5714285714285716
number1 % number2 = 2.0

Operator + juga digunakan untuk menggabungkan beberapa String. Contoh:

fun main(args: Array<String>) {
 
val start = "Talk is cheap. "
 val middle = "Show me the code. "
 val end = "- Linus Torvalds"
 
val result = start + middle + end
 println(result)
}

Output:

Talk is cheap. Show me the code. - Linus Torvalds

2. Assignment Operators

Assignment operator digunakan untuk memberi nilai sebuah operator. Secara default menggunakan operator = (sama dengan).

Expression Equivalent to Translates to
a +=b a = a + b a.plusAssign(b)
a += b a = a + b a.minusAssign(b)
a *= b a = a * b a.timesAssign(b)
a /= b a = a / b a.divAssign(b)
a %= b a = a % b a.modAssign(b)

Contoh Assignment Operator:

fun main(args: Array<String>) {
    var number = 12
 
    number *= 5   // number = number*5
    println("number  = $number")
}

Output:

number = 60

3. Unary prefix and Increment / Decrement Operators

Operator Meaning Expression Translates to
+ Unary plus +a a.unaryPlus()
Unary minus (inverts sign) -a a.unaryMinus()
! not (inverts value) !a a.not()
++ Increment: increases value by1 ++a a.inc()
Decrement: decreases value by 1 –a a.dec()

Contoh:

fun main(args: Array<String>) {
    val a = 1
    val b = true
    var c = 1
 
    var result: Int
    var booleanResult: Boolean
 
    result = -a
    println("-a = $result")
 
    booleanResult = !b
    println("!b = $booleanResult")
 
    --c
    println("--c = $c")
}

Output:

 -a = -1
 !b = false
 --c = 0

4. Comparison and Equality Operators

Operator Meaning Expression Translates to
> greater than a > b a.compareTo(b) > 0
< less than a < b a.compareTo(b) < 0
>= greater than or equals to a >= b a.compareTo(b) >= 0
<= less than or equals to a < = b a.compareTo(b) <= 0
== is equal to a == b a?.equals(b) ?: (b === null)
!= not equal to a != b !(a?.equals(b) ?: (b === null))

Contoh:

fun main(args: Array<String>) {
 
    val a = -12
    val b = 12
 
    // use of greater than operator
    val max = if (a > b) {
        println("a is larger than b.")
        a
    } else {
        println("b is larger than a.")
        b
    }
 
    println("max = $max")
}

Output:

b is larger than a.
max = 12

5. Logical Operators

fun main(args: Array<String>) {
 
    val a = 10
    val b = 9
    val c = -1
    val result: Boolean
 
    // result is true is a is largest
    result = (a>b) &amp;&amp; (a>c) // result = (a>b) and (a>c)
    println(result)
}

Output:

True

6. IN Operator

Operator Expression Translates to
in a in b b.contains(a)
!in a !in b !b.contains(a)

Contoh:

fun main(args: Array<String>) {
 
    val numbers = intArrayOf(1, 4, 42, -3)
 
    if (4 in numbers) {
        println("numbers array contains 4.")
    }
}

Output:

numbers array contains 4.

7. Index Access Operator

Expression Translated to
a[i] a.get(i)
a[i, n] a.get(i, n)
a[i1, i2, ..., in] a.get(i1, i2, ..., in)
a[i] = b a.set(i, b)
a[i, n] = b a.set(i, n, b)
a[i1, i2, ..., in] = b a.set(i1, i2, ..., in, b)

Contoh:

fun main(args: Array<String>) {
 
    val a  = intArrayOf(1, 2, 3, 4, - 1)
    println(a[1])   
    a[1]= 12
    println(a[1])
}

Output:

2
12

Refrence

Kotlin Operators

Sharethis

 

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

one × 5 =

This site uses Akismet to reduce spam. Learn how your comment data is processed.