Char Data type Usage and Type Conversion in Kotlin
Definition
- Char data type is represented by single apostrophe just like Java in Kotlin.
- In JVM, the characteristics of this "Char" variable is derived from the characteristics of primitive type "char" of Java which has a non-nullable default value.
Syntax and declaration
A Char value can be declared by 3 ways as mentioned below.
1. Mentioning type explicitly
val variable_name : Char = 'v'
for example,
val name : Char = 'r'
2. Type is automatically inferred
val variable_name = 'v'
for example,
val name = 'r'
3. Declaring and Initializing separately
var variable_name : Char
variable_name = ''
for example
//Declaring and Initializing separately
var name:Char
name = 'r'
Char Data type Example Program in Kotlin
// Char Datatype Kotlin example program
// Data type Kotlin Programs, Basic Kotlin Programs
fun main(args:Array<String>) {
//Assigned value is inferred automatically by compiler
val name = 'm'
println("The character is: $c")
}
Sample Output
The character is: m
Kotlin Data Types
- Int Data type Usage and Type Conversion in Kotlin
- Float Data type Usage and Type Conversion in Kotlin
- Double Data type Usage and Type Conversion in Kotlin
- Byte Data type Usage and Type Conversion in Kotlin
- Short Data type Usage and Type Conversion in Kotlin
- Long Data type Usage and Type Conversion in Kotlin
- Char Data type Usage and Type Conversion in Kotlin
- Boolean Data type Usage and Type Conversion in Kotlin
- Number Data type Usage and Type Conversion in Kotlin
- String Data type Usage and Type Conversion in Kotlin
- Elvis Operator in Kotlin
Read More Articles
- Declare Variables In Kotlin
- Read Data Input using Scanner in Kotlin
- print and println Data Output in Kotlin
- Double Data type Usage and Type Conversion in Kotlin
- Arithmetic Operators (Mathematical Operators) in Kotlin
- Unary Operators (Sign, Inverts, Increment, and Decrement) in Kotlin
- Equality Operators (==, !=) and Referential equality Operators (===, !==) in Kotlin
- Printing Variables and Values in Kotlin
- Float Data type Usage and Type Conversion in Kotlin
- Long Data type Usage and Type Conversion in Kotlin
- Comparison Operators in Kotlin
- Byte Data type Usage and Type Conversion in Kotlin
- Is Operator (is and !is) in Kotlin
- In Operator (in and !in) in Kotlin
- Char Data type Usage and Type Conversion in Kotlin
- Read Data Input from Command Line in Kotlin
- Assignment Operators and Augmented Assignment Operators in Kotlin
- Indexed Access Operator [, ] in Kotlin
- Read String Data Input in Kotlin
- Short Data type Usage and Type Conversion in Kotlin
- Boolean Data type Usage and Type Conversion in Kotlin
- Logical Operators in Kotlin
- Elvis Operator (?:) in Kotlin
- Repeat and Its Usage in Kotlin
- Safe Call Operator (?.) in Kotlin