Unary Operators (Sign, Inverts, Increment, and Decrement) in Kotlin
Unary Operator Types
Kotlin has 5 Unary Operators similar to Java. They are
- Unary Plus Operator (+)
- Unary Minus Operator (-)
- Unary Inversion Operator (!)
- Increment Operator (++)
- Decrement Operator (--)
Syntax
1. Unary Plus Operator
A number without a sign is positive number in general and hence adding a "+" will not change the value of a number
var variable_one = +variable_two
For example,
var sum = +2
2. Unary Minus Operator
This operator changes the sign of value.
var variable_one = -variable_two
For example,
var sub = -5
3. Unary Inversion Operator
var variable_one = !variable_two
For example,
var mul = !value
4. Increment Operator
- Increment Operator can be a pre-Increment Operator or post_Increment Operator.
- Pre-Increment Operator is mentioned by '++a'. When this operation is done, the value of a variable is incremented by one abruptly.
- Post-Increment Operator is mentioned by 'a++'. When this operation is done, the value of a variable is incremented by one after the operation is done. ie., Only while performing the next operation, the incremented value will be used.
//Pre-Increment Operator
var variable_one = ++variable_two
//Post-Increment Operator
var variable_one = variable_two++
For example,
//Pre-Increment Operator
var num = ++10
//Post-Increment Operator
var num = 10++
5. Decrement Operator
- Decrement Operator can be a pre-Decrement Operator or post_Decrement Operator.
- Pre-Decrement Operator is mentioned by '++a'. When this operation is done, the value of a variable is decremented by one abruptly.
- Post-Decrement Operator is mentioned by 'a++'. When this operation is done, the value of a variable is decremented by one after the operation is done. ie., Only while performing the next operation, the decremented value will be used.
//Pre-Decrement Operator
var variable_one = --variable_two
//Post-Decrement Operator
var variable_one = variable_two--
For example,
//Pre-Decrement Operator
var num = --10
//Post-Decrement Operator
var num = 10--
Unary Operator Example Program in Kotlin
//Unary Operator Example Program in Kotlin
//Operator Kotlin Programs, Basic Kotlin Program
fun main(args: Array<String>) {
var number = 50
var boolValue = true
var num1 = 10;
var num2 = 100;
var num3 = 1000;
var num4 = 10000;
//Unary Minus Operation
val minus = -(number)
println("The value $number after Unary Minus Operation is $minus")
//Unary Inversion Operation
val value = !boolValue
println("The value $boolValue after Unary Minus Operation is $value")
//Post-Increment Operation
println("\nPost-Increment Operation")
println("Value to be used for Post-Increment Operation is $num1")
//This will print the same as original value as the value will be incremented only during the next operation
println("Value after Post-Increment Operation is ${num1++}")
//Pre-Increment Operation
println("\nPre-Increment Operation")
println("Value to be used for Pre-Increment Operation is $num2")
//This will print the incremented value as the operation takes place abruptly
println("Value after Post-Increment Operation is ${++num2}")
//Post-Decrement Operation
println("\nPost-Decrement Operation")
println("Value to be used for Post-Decrement Operation is $num3")
//This will print the same as original value as the value will be decremented only during the next operation
println("Value after Post-Decrement Operation is ${num3--}")
//Pre-Decrement Operation
println("\nPre-Decrement Operation")
println("Value to be used for Pre-Decrement Operation is $num4")
//This will print the decremented value as the operation takes place abruptly
println("Value after Post-Decrement Operation is ${--num4}")
}
Sample Output
The value 50 after Unary Minus Operation is -50
The value true after Unary Minus Operation is false
Post-Increment Operation
Value to be used for Post-Increment Operation is 10
Value after Post-Increment Operation is 10
Pre-Increment Operation
Value to be used for Pre-Increment Operation is 100
Value after Post-Increment Operation is 101
Post-Decrement Operation
Value to be used for Post-Decrement Operation is 1000
Value after Post-Decrement Operation is 1000
Pre-Decrement Operation
Value to be used for Pre-Decrement Operation is 10000
Value after Post-Decrement Operation is 9999
Kotlin Operators
- Kotlin Operators Overview
- Arithmetic Operators (Mathematical Operators) in Kotlin
- Assignment Operators and Augmented Assignment Operators in Kotlin
- Unary Operators (Sign, Inverts, Increment, and Decrement) in Kotlin
- Logical Operators in Kotlin
- Equality Operators (==, !=) and Referential equality Operators (===, !==) in Kotlin
- Comparison Operators in Kotlin
- In Operator (in and !in) in Kotlin
- Is Operator (is and !is) in Kotlin
- Indexed Access Operator [, ] in Kotlin
- Not Null Assertion Operator in Kotlin
- Safe Call Operator (?.) in Kotlin
- Elvis Operator (?:) in Kotlin
- Range 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