Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade Teachers Spaces Get Certified Upgrade Teachers Spaces
   ❮   
     ❯   

KotlinBooleans


Kotlin Booleans

Very often, in programming, you will need a data type that can only have one of two values, like:

  • YES / NO
  • ON / OFF
  • TRUE / FALSE

For this, Kotlin has aBoolean data type, which can take the valuestrue orfalse.


Boolean Values

A boolean type can be declared with theBoolean keyword and can only take the valuestrue orfalse:

Example

val isKotlinFun: Boolean = trueval isFishTasty: Boolean = falseprintln(isKotlinFun)   // Outputs trueprintln(isFishTasty)   // Outputs false
Try it Yourself »

Just like you have learned with other data types in the previous chapters, the example above can also be written without specifying the type, as Kotlin is smart enough to understand that the variables are Booleans:

Example

val isKotlinFun = trueval isFishTasty = falseprintln(isKotlinFun)   // Outputs trueprintln(isFishTasty)   // Outputs false
Try it Yourself »


Boolean Expression

A Boolean expressionreturnsa Boolean value:true orfalse.

You can use a comparison operator, such as thegreater than (>) operator to find out if an expression (or a variable) is true:

Example

val x = 10val y = 9
println(x > y) // Returns true, because 10 is greater than 9
Try it Yourself »

Or even easier:

Example

println(10 > 9) // Returns true, because 10 is greater than 9
Try it Yourself »

In the examples below, we use theequal to (==) operator to evaluate an expression:

Example

val x = 10;println(x == 10); // Returns true, because the value of x is equal to 10
Try it Yourself »

Example

println(10 == 15); // Returns false, because 10 is not equal to 15
Try it Yourself »

The Boolean value of an expression is the basis for all Kotlin comparisons and conditions.

You will learn more about conditions in the next chapter.




×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookies andprivacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.


[8]ページ先頭

©2009-2025 Movatter.jp