Difference between revisions of "KOTLIN: Boolean"

From OnnoWiki
Jump to navigation Jump to search
(Created page with "Sumber: https://www.tutorialspoint.com/kotlin/kotlin_booleans.htm Many times we come across a situation where we need to take decision in Yes or No, or may be we can say Tr...")
 
 
(One intermediate revision by the same user not shown)
Line 7: Line 7:
 
Kotlin also has a nullable counterpart Boolean? that can have the null value.
 
Kotlin also has a nullable counterpart Boolean? that can have the null value.
  
Create Boolean Variables
+
 
 +
Sering kali kita menemukan situasi di mana kita perlu mengambil keputusan dalam Ya atau Tidak, atau mungkin kita bisa mengatakan Benar atau Salah. Untuk menangani situasi seperti itu, Kotlin memiliki tipe data Boolean, yang dapat mengambil nilai benar atau salah.
 +
 
 +
Kotlin juga memiliki mitra nullable Boolean? yang dapat memiliki nilai nol.
 +
 
 +
==Create Boolean Variables==
 +
 
 
A boolean variable can be created using Boolean keyword and this variable can only take the values either true or false:
 
A boolean variable can be created using Boolean keyword and this variable can only take the values either true or false:
  
Example
+
Variabel boolean dapat dibuat menggunakan kata kunci Boolean dan variabel ini hanya dapat mengambil nilai benar atau salah:
fun main(args: Array<String>) {
+
 
  val isSummer: Boolean = true
+
===Example===
  val isCold: Boolean = false
+
 
 
+
fun main(args: Array<String>) {
  println(isSummer)
+
    val isSummer: Boolean = true
  println(isCold)
+
    val isCold: Boolean = false
 
    
 
    
}
+
    println(isSummer)
 +
    println(isCold)
 +
}
 +
 
 
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:
  
true
+
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
false
+
 
 +
true
 +
false
 +
 
 
In fact, we can create Kotlin boolean variables without using Boolean keyword and Kotlin will understand the variable type based on the assigned values either true or false
 
In fact, we can create Kotlin boolean variables without using Boolean keyword and Kotlin will understand the variable type based on the assigned values either true or false
  
Kotlin Boolean Operators
+
Sebenarnya, kita dapat membuat variabel boolean Kotlin tanpa menggunakan kata kunci Boolean dan Kotlin akan memahami jenis variabel berdasarkan nilai yang ditetapkan baik benar atau salah
 +
 
 +
==Kotlin Boolean Operators==
 +
 
 
Kotlin provides following built-in operators for boolean variables. These operators are also called Logical Operators:
 
Kotlin provides following built-in operators for boolean variables. These operators are also called Logical Operators:
  
Operator Name Description Example
+
Kotlin menyediakan operator bawaan berikut untuk variabel boolean. Operator ini juga disebut Operator Logika:
&& Logical and Returns true if both operands are true x && y
+
 
|| Logical or Returns true if either of the operands is true x || y
+
Operator Name Description Example
! Logical not Reverse the result, returns false if the operand is true !x
+
&& Logical and Returns true if both operands are true x && y
Example
+
|| 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>) {
 
  var x: Boolean = true
 
  var y:Boolean = false
 
  
  println("x && y = " +  (x && y))
+
Contoh berikut menunjukkan perhitungan yang berbeda menggunakan Operator Logika Kotlin:
  println("x || y = " +  (x || y))
+
 
  println("!y = " +  (!y))
+
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:
 
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
Kotlin Boolean Expression
+
x || y = true
 +
!y = true
 +
 
 +
==Kotlin Boolean Expression==
 +
 
 
A Boolean expression returns either true or false value and majorly used in checking the condition with if...else expressions. A boolean expression makes use of relational operators, for example >, <, >= etc.
 
A Boolean expression returns either true or false value and majorly used in checking the condition with if...else expressions. A boolean expression makes use of relational operators, for example >, <, >= etc.
  
fun main(args: Array<String>) {
+
Ekspresi Boolean mengembalikan nilai benar atau salah dan sebagian besar digunakan dalam memeriksa kondisi dengan ekspresi if...else. Ekspresi boolean menggunakan operator relasional, misalnya >, <, >= dll.
  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))
 
  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 = false
+
x > y = true
x == y = false
+
x < y = false
x != y = true
+
x >= y = true
Kotlin and()and or() Functions
+
x <= y = false
 +
x == y = false
 +
x != y = true
 +
 
 +
==Kotlin and()and or() Functions==
 +
 
 
Kotlin provides and() and or() functions to perform logical AND and logical OR operations between two boolean operands.
 
Kotlin provides and() and or() functions to perform logical AND and logical OR operations between two boolean operands.
  
 
These functions are different from && and || operators because these functions do not perform short-circuit evaluation but they always evaluate both the operands.
 
These functions are different from && and || operators because these functions do not perform short-circuit evaluation but they always evaluate both the operands.
  
fun main(args: Array<String>) {
+
Kotlin menyediakan fungsi and() dan or() untuk melakukan operasi logika AND dan logika OR antara dua operan boolean.
  val x: Boolean = true
+
 
  val y: Boolean = false
+
Fungsi-fungsi ini berbeda dari && dan || operator karena fungsi ini tidak melakukan evaluasi hubung singkat tetapi mereka selalu mengevaluasi kedua operan.
  val z: Boolean = true
+
 
 +
fun main(args: Array<String>) {
 +
    val x: Boolean = true
 +
    val y: Boolean = false
 +
    val z: Boolean = true  
 +
 +
    println("x.and(y) = " +  x.and(y))
 +
    println("x.or(y) = " +  x.or(y))
 +
    println("x.and(z) = " +  x.and(z))
 +
}
  
  println("x.and(y) = " +  x.and(y))
 
  println("x.or(y) = " +  x.or(y))
 
  println("x.and(z) = " +  x.and(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.and(y) = false
+
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
x.or(y) = true
+
 
x.and(z) = true
+
x.and(y) = false
 +
x.or(y) = true
 +
x.and(z) = true
 +
 
 
Kotlin also provides not() and xor() functions to perform Logical NOT and XOR operations respectively.
 
Kotlin also provides not() and xor() functions to perform Logical NOT and XOR operations respectively.
  
Boolean to String
+
Kotlin juga menyediakan fungsi not() dan xor() untuk melakukan operasi Logical NOT dan XOR secara berurutan.
 +
 
 +
==Boolean to String==
 +
 
 
You can use toString() function to convert a Boolean object into its equivalent string representation.
 
You can use toString() function to convert a Boolean object into its equivalent string representation.
  
 
You will need this conversion when assigning a true or false value in a String variable.
 
You will need this conversion when assigning a true or false value in a String variable.
  
fun main(args: Array<String>) {
+
Anda dapat menggunakan fungsi toString() untuk mengonversi objek Boolean menjadi representasi string yang setara.
  val x: Boolean = true
+
 
  var z: String
+
Anda akan memerlukan konversi ini saat menetapkan nilai benar atau salah dalam variabel String.
 
+
 
  z = x.toString()
+
fun main(args: Array<String>) {
 +
    val x: Boolean = true
 +
    var z: String
 +
   
 +
    z = x.toString()  
 +
 +
    println("x.toString() = " +  x.toString())
 +
    println("z = " +  z)
 +
}
  
  println("x.toString() = " +  x.toString())
 
  println("z = " +  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.toString() = true
+
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
z = true
+
 
Quiz Time (Interview & Exams Preparation)
+
x.toString() = true
 +
z = true
 +
 
 +
==Quiz Time (Interview & Exams Preparation)==
 +
 
 
Q 1 - Which of the following is true about Kotlin Boolean Data type?
 
Q 1 - Which of the following is true about Kotlin Boolean Data type?
  

Latest revision as of 10:02, 29 July 2022

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


Many times we come across a situation where we need to take decision in Yes or No, or may be we can say True or False. To handle such situation Kotlin has a Boolean data type, which can take the values either true or false.

Kotlin also has a nullable counterpart Boolean? that can have the null value.


Sering kali kita menemukan situasi di mana kita perlu mengambil keputusan dalam Ya atau Tidak, atau mungkin kita bisa mengatakan Benar atau Salah. Untuk menangani situasi seperti itu, Kotlin memiliki tipe data Boolean, yang dapat mengambil nilai benar atau salah.

Kotlin juga memiliki mitra nullable Boolean? yang dapat memiliki nilai nol.

Create Boolean Variables

A boolean variable can be created using Boolean keyword and this variable can only take the values either true or false:

Variabel boolean dapat dibuat menggunakan kata kunci Boolean dan variabel ini hanya dapat mengambil nilai benar atau salah:

Example

fun main(args: Array<String>) {
   val isSummer: Boolean = true
   val isCold: Boolean = false
  
   println(isSummer)
   println(isCold)
}

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:

true
false

In fact, we can create Kotlin boolean variables without using Boolean keyword and Kotlin will understand the variable type based on the assigned values either true or false

Sebenarnya, kita dapat membuat variabel boolean Kotlin tanpa menggunakan kata kunci Boolean dan Kotlin akan memahami jenis variabel berdasarkan nilai yang ditetapkan baik benar atau salah

Kotlin Boolean Operators

Kotlin provides following built-in operators for boolean variables. These operators are also called Logical Operators:

Kotlin menyediakan operator bawaan berikut untuk variabel boolean. Operator ini juga disebut Operator Logika:

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

Kotlin Boolean Expression

A Boolean expression returns either true or false value and majorly used in checking the condition with if...else expressions. A boolean expression makes use of relational operators, for example >, <, >= etc.

Ekspresi Boolean mengembalikan nilai benar atau salah dan sebagian besar digunakan dalam memeriksa kondisi dengan ekspresi if...else. Ekspresi boolean menggunakan operator relasional, misalnya >, <, >= dll.


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

Kotlin and()and or() Functions

Kotlin provides and() and or() functions to perform logical AND and logical OR operations between two boolean operands.

These functions are different from && and || operators because these functions do not perform short-circuit evaluation but they always evaluate both the operands.

Kotlin menyediakan fungsi and() dan or() untuk melakukan operasi logika AND dan logika OR antara dua operan boolean.

Fungsi-fungsi ini berbeda dari && dan || operator karena fungsi ini tidak melakukan evaluasi hubung singkat tetapi mereka selalu mengevaluasi kedua operan.

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

   println("x.and(y) = " +  x.and(y))
   println("x.or(y) = " +  x.or(y))
   println("x.and(z) = " +  x.and(z))
}

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.and(y) = false
x.or(y) = true
x.and(z) = true

Kotlin also provides not() and xor() functions to perform Logical NOT and XOR operations respectively.

Kotlin juga menyediakan fungsi not() dan xor() untuk melakukan operasi Logical NOT dan XOR secara berurutan.

Boolean to String

You can use toString() function to convert a Boolean object into its equivalent string representation.

You will need this conversion when assigning a true or false value in a String variable.

Anda dapat menggunakan fungsi toString() untuk mengonversi objek Boolean menjadi representasi string yang setara.

Anda akan memerlukan konversi ini saat menetapkan nilai benar atau salah dalam variabel String.

fun main(args: Array<String>) {
   val x: Boolean = true
   var z: String
   
   z = x.toString() 

   println("x.toString() = " +  x.toString())
   println("z = " +  z)
}

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.toString() = true
z = true

Quiz Time (Interview & Exams Preparation)

Q 1 - Which of the following is true about Kotlin Boolean Data type?

A - Boolean data type can have two values true and false

B - Boolean data type can have two values 0 and 1

C - We can assign Boolean value to integer variable

D - All of the above

Q 2 - What will be the output of the following program:

fun main(args: Array<String>) {

  val x: Boolean = true
  var y: String
  
  y = x

} A - This will compile successfully without error and warning

B - This will raise just a warning

C - Compilation will stop with error

D - None of the above


Referensi