Difference between revisions of "KOTLIN: Operator"

From OnnoWiki
Jump to navigation Jump to search
(Created page with "Sumber: https://www.tutorialspoint.com/kotlin/kotlin_operators.htm An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations...")
 
 
(One intermediate revision by the same user not shown)
Line 5: Line 5:
 
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. Kotlin is rich in built-in operators and provide the following types of operators:
 
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. Kotlin is rich in built-in operators and provide the following types of operators:
  
Arithmetic Operators
+
* Arithmetic Operators
 +
* Relational Operators
 +
* Assignment Operators
 +
* Unary Operators
 +
* Logical Operators
 +
* Bitwise Operations
  
Relational Operators
+
Now let's look into these Kotlin Operators one by one.
  
Assignment Operators
+
Operator adalah simbol yang memberitahu compiler untuk melakukan manipulasi matematika atau logika tertentu. Kotlin kaya akan operator bawaan dan menyediakan jenis operator berikut:
  
Unary Operators
+
* Operator Aritmatika
 +
* Operator Relasional
 +
* Operator Penugasan
 +
* Operator Unary
 +
* Operator Logika
 +
* Operasi Bitwise
  
Logical Operators
+
Sekarang mari kita lihat Operator Kotlin ini satu per satu.
  
Bitwise Operations
 
  
Now let's look into these Kotlin Operators one by one.
+
==(a) Kotlin Arithmetic Operators==
  
(a) Kotlin Arithmetic Operators
 
 
Kotlin arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication and division etc.
 
Kotlin arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication and division etc.
  
Operator Name Description Example
+
Operator aritmatika Kotlin digunakan untuk melakukan operasi matematika dasar seperti penambahan, pengurangan, perkalian dan pembagian dll.
+ Addition Adds together two values x + y
+
 
- Subtraction Subtracts one value from another x - y
+
Operator Name Description Example
* Multiplication Multiplies two values x * y
+
+ Addition Adds together two values x + y
/ Division Divides one value by another x / y
+
- Subtraction Subtracts one value from another x - y
% Modulus Returns the division remainder x % y
+
* Multiplication Multiplies two values x * y
Example
+
/ Division Divides one value by another x / y
 +
% Modulus Returns the division remainder x % y
 +
 
 +
===Example===
 +
 
 
Following example shows different calculations using Kotlin Arithmetic Operators:
 
Following example shows different calculations using Kotlin Arithmetic Operators:
  
fun main(args: Array<String>) {
+
Contoh berikut menunjukkan perhitungan yang berbeda menggunakan Operator Aritmatika Kotlin:
  val x: Int = 40
+
 
  val y: Int = 20
+
 
 +
fun main(args: Array<String>) {
 +
    val x: Int = 40
 +
    val y: Int = 20
 +
 +
    println("x + y = " +  (x + y))
 +
    println("x - y = " +  (x - y))
 +
    println("x / y = " +  (x / y))
 +
    println("x * y = " +  (x * y))
 +
    println("x % y = " +  (x % y))
 +
}
  
  println("x + y = " +  (x + y))
 
  println("x - y = " +  (x - y))
 
  println("x / y = " +  (x / y))
 
  println("x * y = " +  (x * y))
 
  println("x % y = " +  (x % y))
 
}
 
 
When you run the above Kotlin program, it will generate the following output:
 
When you run the above Kotlin program, it will generate the following output:
  
x + y = 60
+
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
x - y = 20
+
 
x / y = 2
+
x + y = 60
x * y = 800
+
x - y = 20
x % y = 0
+
x / y = 2
(b) Kotlin Relational Operators
+
x * y = 800
 +
x % y = 0
 +
 
 +
==(b) Kotlin Relational Operators==
 +
 
 
Kotlin relational (comparison) operators are used to compare two values, and returns a Boolean value: either true or false.
 
Kotlin relational (comparison) operators are used to compare two values, and returns a Boolean value: either true or false.
  
Operator Name Example
+
Operator relasional (perbandingan) Kotlin digunakan untuk membandingkan dua nilai, dan mengembalikan nilai Boolean: benar atau salah.
> greater than x > y
+
 
< less than x < y
+
Operator Name Example
>= greater than or equal to x >= y
+
> greater than x > y
<= less than or equal to x <= y
+
< less than x < y
== is equal to x == y
+
>= greater than or equal to x >= y
!= not equal to x != y
+
<= less than or equal to x <= y
Example
+
== is equal to x == y
 +
!= not equal to x != y
 +
 
 +
===Example===
 +
 
 
Following example shows different calculations using Kotlin Relational Operators:
 
Following example shows different calculations using Kotlin Relational Operators:
  
fun main(args: Array<String>) {
 
  val x: Int = 40
 
  val y: Int = 20
 
  
  println("x > y = " +  (x > y))
+
Contoh berikut menunjukkan perhitungan yang berbeda menggunakan Operator Relasional Kotlin:
  println("x < y = " +  (x < y))
+
 
  println("x >= y = " +  (x >= y))
+
fun main(args: Array<String>) {
  println("x <= y = " +  (x <= y))
+
    val x: Int = 40
  println("x == y = " +  (x == y))
+
    val y: Int = 20
  println("x != y = " +  (x != y))
+
}
+
    println("x > y = " +  (x > y))
 +
    println("x < y = " +  (x < y))
 +
    println("x >= y = " +  (x >= y))
 +
    println("x <= y = " +  (x <= y))
 +
    println("x == y = " +  (x == y))
 +
    println("x != y = " +  (x != y))
 +
}
 +
 
 
When you run the above Kotlin program, it will generate the following output:
 
When you run the above Kotlin program, it will generate the following output:
  
x > y = true
+
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
x < y = false
+
 
x >= y = true
+
x > y = true
x <= y = false
+
x < y = false
x == y = false
+
x >= y = true
x != y = true
+
x <= y = false
(c) Kotlin Assignment Operators
+
x == y = false
 +
x != y = true
 +
 
 +
==(c) Kotlin Assignment Operators==
 +
 
 
Kotlin assignment operators are used to assign values to variables.
 
Kotlin assignment operators are used to assign values to variables.
  
 
Following is an example where we used assignment operator = to assign a values into two variables:
 
Following is an example where we used assignment operator = to assign a values into two variables:
  
fun main(args: Array<String>) {
+
Operator penugasan Kotlin digunakan untuk menetapkan nilai ke variabel.
  val x: Int = 40
+
 
  val y: Int = 20
+
Berikut adalah contoh di mana kami menggunakan operator penugasan = untuk menetapkan nilai ke dalam dua variabel:
 
+
 
  println("x = " +  x)
+
fun main(args: Array<String>) {
  println("y = " +  y)
+
    val x: Int = 40
}
+
    val y: Int = 20
 +
   
 +
    println("x = " +  x)
 +
    println("y = " +  y)
 +
}
 +
 
 
When you run the above Kotlin program, it will generate the following output:
 
When you run the above Kotlin program, it will generate the following output:
  
x = 40
+
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
y = 20
+
 
 +
x = 40
 +
y = 20
 +
 
 
Following is one more example where we used assignment operator += to add the value of self variable and assign it back into the same variable:
 
Following is one more example where we used assignment operator += to add the value of self variable and assign it back into the same variable:
  
fun main(args: Array<String>) {
+
Berikut ini adalah satu contoh lagi di mana kami menggunakan operator penugasan += untuk menambahkan nilai variabel diri dan menetapkannya kembali ke variabel yang sama:
  var x: Int = 40
+
 
 +
fun main(args: Array<String>) {
 +
    var x: Int = 40  
 +
 +
    x += 10
 +
     
 +
    println("x = " +  x)
 +
}
  
  x += 10
 
     
 
  println("x = " +  x)
 
}
 
 
When you run the above Kotlin program, it will generate the following output:
 
When you run the above Kotlin program, it will generate the following output:
  
x = 50
+
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
 +
 
 +
x = 50
 +
 
 
Following is a list of all assignment operators:
 
Following is a list of all assignment operators:
  
Operator Example Expanded Form
+
Berikut adalah daftar semua operator penugasan:
= x = 10 x = 10
+
 
+= x += 10 x = x - 10
+
Operator Example Expanded Form
-= x -= 10 x = x - 10
+
= x = 10 x = 10
*= x *= 10 x = x * 10
+
+= x += 10 x = x - 10
/= x /= 10 x = x / 10
+
-= x -= 10 x = x - 10
%= x %= 10 x = x % 10
+
*= x *= 10 x = x * 10
Example
+
/= x /= 10 x = x / 10
 +
%= x %= 10 x = x % 10
 +
 
 +
==Example==
 +
 
 
Following example shows different calculations using Kotlin Assignment Operators:
 
Following example shows different calculations using Kotlin Assignment Operators:
  
fun main(args: Array<String>) {
 
  var x: Int = 40
 
  
  x += 5
+
Contoh berikut menunjukkan perhitungan yang berbeda menggunakan Kotlin Assignment Operators:
  println("x += 5 = " + x )
+
 
 +
fun main(args: Array<String>) {
 +
    var x: Int = 40
 +
 +
    x += 5
 +
    println("x += 5 = " + x )
 +
   
 +
    x = 40;
 +
    x -= 5
 +
    println("x -= 5 = " + x)
 +
   
 +
    x = 40
 +
    x *= 5
 +
    println("x *= 5 = " + x)
 
    
 
    
  x = 40;
+
    x = 40
  x -= 5
+
    x /= 5
  println("x -= 5 = " +  x)
+
    println("x /= 5 = " +  x)
 
+
   
  x = 40
+
    x = 43
  x *= 5
+
    x %= 5
  println("x *= 5 = " +  x)
+
    println("x %= 5 = " + x)
 
+
}
  x = 40
+
 
  x /= 5
 
  println("x /= 5 = " +  x)
 
 
 
  x = 43
 
  x %= 5
 
  println("x %= 5 = " + x)
 
}
 
 
When you run the above Kotlin program, it will generate the following output:
 
When you run the above Kotlin program, it will generate the following output:
  
x += 5 = 45
+
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
x -= 5 = 35
+
 
x *= 5 = 200
+
x += 5 = 45
x /= 5 = 8
+
x -= 5 = 35
x %= 5 = 3
+
x *= 5 = 200
(d) Kotlin Unary Operators
+
x /= 5 = 8
 +
x %= 5 = 3
 +
 
 +
==(d) Kotlin Unary Operators==
 +
 
 
The unary operators require only one operand; they perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean.
 
The unary operators require only one operand; they perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean.
  
 
Following is the list of Kotlin Unary Operators:
 
Following is the list of Kotlin Unary Operators:
  
Operator Name Example
+
Operator unary hanya membutuhkan satu operan; mereka melakukan berbagai operasi seperti menambah/mengurangi nilai satu per satu, meniadakan ekspresi, atau membalikkan nilai boolean.
+ unary plus +x
+
 
- unary minus -x
+
Berikut daftar Operator Unary Kotlin:
++ increment by 1 ++x
+
 
-- decrement by 1 --x
+
Operator Name Example
! inverts the value of a boolean !x
+
+ unary plus +x
Example
+
- unary minus -x
 +
++ increment by 1 ++x
 +
-- decrement by 1 --x
 +
! inverts the value of a boolean !x
 +
 
 +
===Example===
 +
 
 
Following example shows different calculations using Kotlin Unary Operators:
 
Following example shows different calculations using Kotlin Unary Operators:
  
fun main(args: Array<String>) {
+
Contoh berikut menunjukkan perhitungan yang berbeda menggunakan Kotlin Unary Operators:
  var x: Int = 40
+
 
  var b:Boolean = true
+
fun main(args: Array<String>) {
 +
    var x: Int = 40
 +
    var b:Boolean = true
 +
 +
    println("+x = " +  (+x))
 +
    println("-x = " +  (-x))
 +
    println("++x = " +  (++x))
 +
    println("--x = " +  (--x))
 +
    println("!b = " +  (!b))
 +
}
  
  println("+x = " +  (+x))
 
  println("-x = " +  (-x))
 
  println("++x = " +  (++x))
 
  println("--x = " +  (--x))
 
  println("!b = " +  (!b))
 
}
 
 
When you run the above Kotlin program, it will generate the following output:
 
When you run the above Kotlin program, it will generate the following output:
  
+x = 40
+
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
-x = -40
+
 
++x = 41
+
+x = 40
--x = 40
+
-x = -40
!b = false
+
++x = 41
 +
--x = 40
 +
!b = false
 +
 
 
Here increment (++) and decrement (--) operators can be used as prefix as ++x or --x as well as suffix as x++ or x--. The only difference between the two forms is that in case we use them as prefix then operator will apply before expression is executed, but if use them as suffix then operator will apply after the expression is executed.
 
Here increment (++) and decrement (--) operators can be used as prefix as ++x or --x as well as suffix as x++ or x--. The only difference between the two forms is that in case we use them as prefix then operator will apply before expression is executed, but if use them as suffix then operator will apply after the expression is executed.
  
(e) Kotlin Logical Operators
+
 
 +
Di sini operator increment (++) dan decrement (--) dapat digunakan sebagai awalan sebagai ++x atau --x serta akhiran sebagai x++ atau x--. Satu-satunya perbedaan antara kedua bentuk adalah bahwa jika kita menggunakannya sebagai awalan maka operator akan berlaku sebelum ekspresi dieksekusi, tetapi jika menggunakannya sebagai akhiran maka operator akan berlaku setelah ekspresi dieksekusi.
 +
 
 +
==(e) Kotlin Logical Operators==
 +
 
 
Kotlin logical operators are used to determine the logic between two variables or values:
 
Kotlin logical operators are used to determine the logic between two variables or values:
  
 
Following is the list of Kotlin Logical Operators:
 
Following is the list of Kotlin Logical Operators:
  
Operator Name Description Example
+
 
&& Logical and Returns true if both operands are true x && y
+
Operator logika Kotlin digunakan untuk menentukan logika antara dua variabel atau nilai:
|| Logical or Returns true if either of the operands is true x || y
+
 
! Logical not Reverse the result, returns false if the operand is true !x
+
Berikut daftar Operator Logika Kotlin:
Example
+
 
 +
 
 +
Operator Name Description Example
 +
&& Logical and Returns true if both operands are true x && y
 +
|| Logical or Returns true if either of the operands is true x || y
 +
! Logical not Reverse the result, returns false if the operand is true !x
 +
 
 +
===Example===
 +
 
 
Following example shows different calculations using Kotlin Logical Operators:
 
Following example shows different calculations using Kotlin Logical Operators:
  
fun main(args: Array<String>) {
+
Contoh berikut menunjukkan perhitungan yang berbeda menggunakan Operator Logika Kotlin:
  var x: Boolean = true
+
 
  var y:Boolean = false
+
fun main(args: Array<String>) {
 +
    var x: Boolean = true
 +
    var y:Boolean = false
 +
 +
    println("x && y = " +  (x && y))
 +
    println("x || y = " +  (x || y))
 +
    println("!y = " +  (!y))
 +
}
  
  println("x && y = " +  (x && y))
 
  println("x || y = " +  (x || y))
 
  println("!y = " +  (!y))
 
}
 
 
When you run the above Kotlin program, it will generate the following output:
 
When you run the above Kotlin program, it will generate the following output:
  
x && y = false
+
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
x || y = true
+
 
!y = true
+
x && y = false
(e) Kotlin Bitwise Operations
+
x || y = true
 +
!y = true
 +
 
 +
==(e) Kotlin Bitwise Operations==
 +
 
 
Kotlin does not have any bitwise operators but Kotlin provides a list of helper functions to perform bitwise operations.
 
Kotlin does not have any bitwise operators but Kotlin provides a list of helper functions to perform bitwise operations.
  
 
Following is the list of Kotlin Bitwise Functions:
 
Following is the list of Kotlin Bitwise Functions:
  
Function Description Example
+
 
shl (bits) signed shift left x.shl(y)
+
Kotlin tidak memiliki operator bitwise, tetapi Kotlin menyediakan daftar fungsi pembantu untuk melakukan operasi bitwise.
shr (bits) signed shift right x.shr(y)
+
 
ushr (bits) unsigned shift right x.ushr(y)
+
Berikut adalah daftar Fungsi Bitwise Kotlin:
and (bits) bitwise and x.and(y)
+
 
or (bits) bitwise or x.or(y)
+
Function Description Example
xor (bits) bitwise xor x.xor(y)
+
shl (bits) signed shift left x.shl(y)
inv() bitwise inverse x.inv()
+
shr (bits) signed shift right x.shr(y)
Example
+
ushr (bits) unsigned shift right x.ushr(y)
 +
and (bits) bitwise and x.and(y)
 +
or (bits) bitwise or x.or(y)
 +
xor (bits) bitwise xor x.xor(y)
 +
inv() bitwise inverse x.inv()
 +
 
 +
===Example===
 +
 
 
Following example shows different calculations using Kotlin bitwise functions:
 
Following example shows different calculations using Kotlin bitwise functions:
  
fun main(args: Array<String>) {
+
Contoh berikut menunjukkan perhitungan yang berbeda menggunakan fungsi bitwise Kotlin:
  var x:Int = 60   // 60 = 0011 1100   
+
 
  var y:Int = 13   // 13 = 0000 1101
+
fun main(args: Array<String>) {
  var z:Int
+
    var x:Int = 60   // 60 = 0011 1100   
 +
    var y:Int = 13   // 13 = 0000 1101
 +
    var z:Int
 +
 +
    z = x.shl(2)      // 240 = 1111 0000
 +
    println("x.shl(2) = " +  z)
 +
   
 +
    z = x.shr(2)      // 15 = 0000 1111
 +
    println("x.shr(2) = " +  z)
 +
   
 +
    z = x.and(y)      // 12 = 0000 1100
 +
    println("x.and(y)  = " +  z)
 +
   
 +
    z = x.or(y)        // 61 = 0011 1101
 +
    println("x.or(y)  = " +  z)
 +
   
 +
    z = x.xor(y)      // 49 = 0011 0001
 +
    println("x.xor(y)  = " +  z)
 +
   
 +
    z = x.inv()        // -61 = 1100 0011
 +
    println("x.inv()  = " +  z)
 +
}
  
  z = x.shl(2)      // 240 = 1111 0000
 
  println("x.shl(2) = " +  z)
 
 
 
  z = x.shr(2)      // 15 = 0000 1111
 
  println("x.shr(2) = " +  z)
 
 
 
  z = x.and(y)      // 12 = 0000 1100
 
  println("x.and(y)  = " +  z)
 
 
 
  z = x.or(y)        // 61 = 0011 1101
 
  println("x.or(y)  = " +  z)
 
 
 
  z = x.xor(y)      // 49 = 0011 0001
 
  println("x.xor(y)  = " +  z)
 
 
 
  z = x.inv()        // -61 = 1100 0011
 
  println("x.inv()  = " +  z)
 
}
 
 
When you run the above Kotlin program, it will generate the following output:
 
When you run the above Kotlin program, it will generate the following output:
  
x.shl(2) = 240
+
Ketika Anda menjalankan program Kotlin di atas, maka akan menghasilkan output sebagai berikut:
x.shr(2) = 15
+
 
x.and(y) = 12
+
 
x.or(y) = 61
+
x.shl(2) = 240
x.xor(y) = 49
+
x.shr(2) = 15
x.inv() = -61
+
x.and(y) = 12
Quiz Time (Interview & Exams Preparation)
+
x.or(y) = 61
 +
x.xor(y) = 49
 +
x.inv() = -61
 +
 
 +
==Quiz Time (Interview & Exams Preparation)==
 +
 
 
Q 1 - What does the Kotlin operator % do?
 
Q 1 - What does the Kotlin operator % do?
  

Latest revision as of 09:44, 29 July 2022

Sumber: https://www.tutorialspoint.com/kotlin/kotlin_operators.htm


An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. Kotlin is rich in built-in operators and provide the following types of operators:

  • Arithmetic Operators
  • Relational Operators
  • Assignment Operators
  • Unary Operators
  • Logical Operators
  • Bitwise Operations

Now let's look into these Kotlin Operators one by one.

Operator adalah simbol yang memberitahu compiler untuk melakukan manipulasi matematika atau logika tertentu. Kotlin kaya akan operator bawaan dan menyediakan jenis operator berikut:

  • Operator Aritmatika
  • Operator Relasional
  • Operator Penugasan
  • Operator Unary
  • Operator Logika
  • Operasi Bitwise

Sekarang mari kita lihat Operator Kotlin ini satu per satu.


(a) Kotlin Arithmetic Operators

Kotlin arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication and division etc.

Operator aritmatika Kotlin digunakan untuk melakukan operasi matematika dasar seperti penambahan, pengurangan, perkalian dan pembagian dll.

Operator	Name	Description	Example
+	Addition	Adds together two values	x + y
-	Subtraction	Subtracts one value from another	x - y
*	Multiplication	Multiplies two values	x * y
/	Division	Divides one value by another	x / y
%	Modulus	Returns the division remainder	x % y

Example

Following example shows different calculations using Kotlin Arithmetic Operators:

Contoh berikut menunjukkan perhitungan yang berbeda menggunakan Operator Aritmatika Kotlin:


fun main(args: Array<String>) {
   val x: Int = 40
   val y: Int = 20

   println("x + y = " +  (x + y))
   println("x - y = " +  (x - y))
   println("x / y = " +  (x / y))
   println("x * y = " +  (x * y))
   println("x % y = " +  (x % y))
}

When you run the above Kotlin program, it will generate the following output:

Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:

x + y = 60
x - y = 20
x / y = 2
x * y = 800
x % y = 0

(b) Kotlin Relational Operators

Kotlin relational (comparison) operators are used to compare two values, and returns a Boolean value: either true or false.

Operator relasional (perbandingan) Kotlin digunakan untuk membandingkan dua nilai, dan mengembalikan nilai Boolean: benar atau salah.

Operator	Name	Example
>	greater than	x > y
<	less than	x < y
>=	greater than or equal to	x >= y
<=	less than or equal to	x <= y
==	is equal to	x == y
!=	not equal to	x != y

Example

Following example shows different calculations using Kotlin Relational Operators:


Contoh berikut menunjukkan perhitungan yang berbeda menggunakan Operator Relasional Kotlin:

fun main(args: Array<String>) {
   val x: Int = 40
   val y: Int = 20

   println("x > y = " +  (x > y))
   println("x < y = " +  (x < y))
   println("x >= y = " +  (x >= y))
   println("x <= y = " +  (x <= y))
   println("x == y = " +  (x == y))
   println("x != y = " +  (x != y))
}

When you run the above Kotlin program, it will generate the following output:

Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:

x > y = true
x < y = false
x >= y = true
x <= y = false
x == y = false
x != y = true

(c) Kotlin Assignment Operators

Kotlin assignment operators are used to assign values to variables.

Following is an example where we used assignment operator = to assign a values into two variables:

Operator penugasan Kotlin digunakan untuk menetapkan nilai ke variabel.

Berikut adalah contoh di mana kami menggunakan operator penugasan = untuk menetapkan nilai ke dalam dua variabel:

fun main(args: Array<String>) {
   val x: Int = 40
   val y: Int = 20
   
   println("x = " +  x)
   println("y = " +  y)
}

When you run the above Kotlin program, it will generate the following output:

Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:

x = 40
y = 20

Following is one more example where we used assignment operator += to add the value of self variable and assign it back into the same variable:

Berikut ini adalah satu contoh lagi di mana kami menggunakan operator penugasan += untuk menambahkan nilai variabel diri dan menetapkannya kembali ke variabel yang sama:

fun main(args: Array<String>) {
   var x: Int = 40 

   x += 10
      
   println("x = " +  x)
}

When you run the above Kotlin program, it will generate the following output:

Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:

x = 50

Following is a list of all assignment operators:

Berikut adalah daftar semua operator penugasan:

Operator	Example	Expanded Form
=	x = 10	x = 10
+=	x += 10	x = x - 10
-=	x -= 10	x = x - 10
*=	x *= 10	x = x * 10
/=	x /= 10	x = x / 10
%=	x %= 10	x = x % 10

Example

Following example shows different calculations using Kotlin Assignment Operators:


Contoh berikut menunjukkan perhitungan yang berbeda menggunakan Kotlin Assignment Operators:

fun main(args: Array<String>) {
   var x: Int = 40 

   x += 5
   println("x += 5 = " + x )
   
   x = 40;
   x -= 5
   println("x -= 5 = " +  x)
   
   x = 40
   x *= 5
   println("x *= 5 = " +  x)
  
   x = 40
   x /= 5
   println("x /= 5 = " +  x)
   
   x = 43
   x %= 5
   println("x %= 5 = " + x)
}

When you run the above Kotlin program, it will generate the following output:

Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:

x += 5 = 45
x -= 5 = 35
x *= 5 = 200
x /= 5 = 8
x %= 5 = 3

(d) Kotlin Unary Operators

The unary operators require only one operand; they perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean.

Following is the list of Kotlin Unary Operators:

Operator unary hanya membutuhkan satu operan; mereka melakukan berbagai operasi seperti menambah/mengurangi nilai satu per satu, meniadakan ekspresi, atau membalikkan nilai boolean.

Berikut daftar Operator Unary Kotlin:

Operator	Name	Example
+	unary plus	+x
-	unary minus	-x
++	increment by 1	++x
--	decrement by 1	--x
!	inverts the value of a boolean	!x

Example

Following example shows different calculations using Kotlin Unary Operators:

Contoh berikut menunjukkan perhitungan yang berbeda menggunakan Kotlin Unary Operators:

fun main(args: Array<String>) {
   var x: Int = 40
   var b:Boolean = true

   println("+x = " +  (+x))
   println("-x = " +  (-x))
   println("++x = " +  (++x))
   println("--x = " +  (--x))
   println("!b = " +  (!b))
}

When you run the above Kotlin program, it will generate the following output:

Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:

+x = 40
-x = -40
++x = 41
--x = 40
!b = false

Here increment (++) and decrement (--) operators can be used as prefix as ++x or --x as well as suffix as x++ or x--. The only difference between the two forms is that in case we use them as prefix then operator will apply before expression is executed, but if use them as suffix then operator will apply after the expression is executed.


Di sini operator increment (++) dan decrement (--) dapat digunakan sebagai awalan sebagai ++x atau --x serta akhiran sebagai x++ atau x--. Satu-satunya perbedaan antara kedua bentuk adalah bahwa jika kita menggunakannya sebagai awalan maka operator akan berlaku sebelum ekspresi dieksekusi, tetapi jika menggunakannya sebagai akhiran maka operator akan berlaku setelah ekspresi dieksekusi.

(e) Kotlin Logical Operators

Kotlin logical operators are used to determine the logic between two variables or values:

Following is the list of Kotlin Logical Operators:


Operator logika Kotlin digunakan untuk menentukan logika antara dua variabel atau nilai:

Berikut daftar Operator Logika Kotlin:


Operator	Name	Description	Example
&&	Logical and	Returns true if both operands are true	x && y
||	Logical or	Returns true if either of the operands is true	x || y
!	Logical not	Reverse the result, returns false if the operand is true	!x

Example

Following example shows different calculations using Kotlin Logical Operators:

Contoh berikut menunjukkan perhitungan yang berbeda menggunakan Operator Logika Kotlin:

fun main(args: Array<String>) {
   var x: Boolean = true
   var y:Boolean = false

   println("x && y = " +  (x && y))
   println("x || y = " +  (x || y))
   println("!y = " +  (!y))
}

When you run the above Kotlin program, it will generate the following output:

Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:

x && y = false
x || y = true
!y = true

(e) Kotlin Bitwise Operations

Kotlin does not have any bitwise operators but Kotlin provides a list of helper functions to perform bitwise operations.

Following is the list of Kotlin Bitwise Functions:


Kotlin tidak memiliki operator bitwise, tetapi Kotlin menyediakan daftar fungsi pembantu untuk melakukan operasi bitwise.

Berikut adalah daftar Fungsi Bitwise Kotlin:

Function	Description	Example
shl (bits)	signed shift left	x.shl(y)
shr (bits)	signed shift right	x.shr(y)
ushr (bits)	unsigned shift right	x.ushr(y)
and (bits)	bitwise and	x.and(y)
or (bits)	bitwise or	x.or(y)
xor (bits)	bitwise xor	x.xor(y)
inv()	bitwise inverse	x.inv()

Example

Following example shows different calculations using Kotlin bitwise functions:

Contoh berikut menunjukkan perhitungan yang berbeda menggunakan fungsi bitwise Kotlin:

fun main(args: Array<String>) {
   var x:Int = 60	  // 60 = 0011 1100  
   var y:Int = 13	  // 13 = 0000 1101
   var z:Int

   z = x.shl(2)       // 240 = 1111 0000
   println("x.shl(2) = " +  z)
   
   z = x.shr(2)       // 15 = 0000 1111
   println("x.shr(2) = " +  z)
   
   z = x.and(y)       // 12 = 0000 1100
   println("x.and(y)  = " +  z)
   
   z = x.or(y)        // 61 = 0011 1101
   println("x.or(y)  = " +  z)
   
   z = x.xor(y)       // 49 = 0011 0001
   println("x.xor(y)  = " +  z)
   
   z = x.inv()        // -61 = 1100 0011
   println("x.inv()  = " +  z)
}

When you run the above Kotlin program, it will generate the following output:

Ketika Anda menjalankan program Kotlin di atas, maka akan menghasilkan output sebagai berikut:


x.shl(2) = 240
x.shr(2) = 15
x.and(y) = 12
x.or(y) = 61
x.xor(y) = 49
x.inv() = -61

Quiz Time (Interview & Exams Preparation)

Q 1 - What does the Kotlin operator % do?

A - It is used to divide a number by another number.

B - Kotlin does not support any such operator

C - This is bitwise XOR operator

D - This is called modulus operator and returns the division remainder.

Q 2 - Kotlin supports a good number of bitwise operators

A - Correct

B - Incorrect

Q 3 - What does Kotlin operator ++ do?

A - It is used to add two values

B - There is no any such operators like ++ in Kotlin

C - This is called unary increment operator

D - None of the above

Q 4 - Which of the following function will do bitwise right shift operation?

A - x.ushr(y)

B - x.shr(y)

C - x.shl(y)

D - None of the above

Q 5 - Which of the following is a logical inverse operator:

A - inv()

B - !

C - &&

D - ||

Q 6 - What will be the output of the following Kotlin code:

fun main(args: Array<String>) {

  var x: Int = 40
  x += 10
  println(x)

} A - 40

B - Syntax Error

C - 50

D None of the above

Q 7 - What will be the output of the following Kotlin code:

fun main(args: Array<String>) {

  var x: Int = 60
  println(x.shr(2))

} A - 15

B - Syntax Error

C - 50

D None of the above


Referensi