[ACCEPTED]-Kotlin: What do the unary plus/minus operators do on numbers?-kotlin
Others have defined the basic meaning of 42 unaryMinus
and unaryPlus
, and in reality on numeric types they 41 may not actually even be called as functions. For 40 example, coding +x
or x.unaryPlus()
generates the same 39 bytecode (where x
is type Int
):
ILOAD 1
ISTORE 2
And the code 38 -x
or x.unaryMinus()
generates the identical bytecode:
ILOAD 1
INEG
ISTORE 2
But there is more going on that this...
So 37 why does the compiler even generate anything 36 for +x
? Some people will say that +x
and x.unaryPlus()
doesn't 35 do anything, and that -x
and x.unaryMinus()
only reverses 34 the sign. That isn't correct. In Java 33 it is more complicated because it can involve 32 widening and unboxing, see Unary Numeric Promotion which explains 31 the full consequences of these operators. This 30 has consequences for boxed values and types 29 smaller than Int
. For value of type Short
and Byte
these 28 operators will return a new unboxed value 27 widened of type Int
. And since both operators 26 have this more hidden functionality then 25 both must generate bytecode even if you 24 don't think +x
does anything. By the way, this 23 is similar to what C language does and it 22 is called Usual Arithmetic Conversions.
Therefore this code is invalid:
val x: Short = 1
val y1: Short = +x // incompatible types
val y2: Short = x.unaryPlus() // incompatible types
val z1: Short = -x // incompatible types
val z2: Short = x.unaryMinus() // incompatible types
In 21 these numeric cases on the base numeric 20 types they are just compiler magic to allow 19 for the idea of these operators to be equated 18 to operator functions that you might want 17 to overload in other classes.
For other uses such as Operator Overloading...
But they are 16 there for more than just mathematical use 15 and can be used on any class as an operator. Kotlin 14 exposes operators as functions so that you 13 can apply operator overloading on a specific set of operators 12 which include unaryMinus
and unaryPlus
.
I could use these to 11 define operators for my own or existing 10 classes. For example I have a Set<Things>
where Things
is 9 an enum class along with an unaryMinus()
operator to 8 negate the contents of the finite set of 7 options:
enum class Things {
ONE, TWO, THREE, FOUR, FIVE
}
operator fun Set<Things>.unaryMinus() = Things.values().toSet().minus(this)
And then I can negate my enum set 6 whenever I want:
val current = setOf(Things.THREE, Things.FIVE)
println(-current) // [ONE, TWO, FOUR]
println(-(-current)) // [THREE, FIVE]
Notice that I had to declare 5 my extension function with the modifier 4 operator
or this will not work. The compiler will 3 remind you if you forget this when you try 2 to use the operator:
Error:(y, x) Kotlin: 'operator' modifier 1 is required on 'unaryMinus' in 'com.my.favorite.package.SomeClass'
These operators are the signs of the integers. Here 2 are some examples:
+5
calls 5.unaryPlus()
and returns 5.
-5
calls 1 5.unaryMinus()
and returns -5.
-(-5)
calls 5.unaryMinus().unaryMinus()
and returns 5.
The purpose of those operators is to be 4 able to write:
val a = System.nanoTime()
val b = -a // a.unaryMinus()
val c = +b // b.unaryPlus()
They are not directly related 3 to ++
/inc
and --
/dec
operators however they can be 2 used in conjunction.
Notice that the following 1 expressions are different:
--a // a = a.dec()
-(-a) // a.unaryMinus().unaryMinus()
fun main(){
var a = 34
var b = 56
println("Orignal value:"+ a)
println("Orignal value:"+ b
//The value will not change using .unaryPlus() will generate bytecode
println("After unary plus:" + a.unaryPlus())
//The value will invert the sign using .unaryMinus() will generate bytecode
println("After unary minus:" + b.unaryMinus())
}
Solution:
Orignal value:34
Orignal value:56
After unary plus:35
After unary minus:-55
0
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.