# unaryPlus/Minus and Byte and Short

**URL:** <https://discuss.kotlinlang.org/t/unaryplus-minus-and-byte-and-short/4669>\
**Category:** Support\
**Created:** [September 20, 2017, 7:01pm UTC](https://discuss.kotlinlang.org/t/unaryplus-minus-and-byte-and-short/4669 "2017-09-20T19:01:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philip-Trettner](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/philip-trettner/32/4060_2.png) [@Philip-Trettner](https://discuss.kotlinlang.org/u/Philip-Trettner)\
**Post date:** [September 20, 2017, 7:01pm UTC](https://discuss.kotlinlang.org/t/unaryplus-minus-and-byte-and-short/4669/1 "2017-09-20T19:01:44Z")

</div>

Why do `unaryPlus` and `unaryMinus` return `Int` instead of `Byte`/`Short` for `Byte`/`Short`?  
(E.g. [unaryMinus - Kotlin Programming Language](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-short/unary-minus.html))

I understand it for plus/minus/etc. but why for unaryPlus/Minus?

```
fun foo(b: Byte) = +b

```

will never overflow/underflow the Byte.

For reference, `inc` and `dec` return Byte/Short even though they might actually over/underflow.

---

<div class="post-metadata">

**Author:** ![alanfo](https://avatars.discourse-cdn.com/v4/letter/a/bbe5ce/32.png) [@alanfo](https://discuss.kotlinlang.org/u/alanfo)\
**Post date:** [September 20, 2017, 9:25pm UTC](https://discuss.kotlinlang.org/t/unaryplus-minus-and-byte-and-short/4669/2 "2017-09-20T21:25:24Z")

</div>

It will be for consistency with Java whose unary operators behave in exactly the same way.

I’m not sure about the reason for this though you would have a problem applying unary negation to a Byte value of -128 or a Short value of -32768 given that there’s no corresponding positive value within the range of those types.

---

<div class="post-metadata">

**Author:** ![Philip-Trettner](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/philip-trettner/32/4060_2.png) [@Philip-Trettner](https://discuss.kotlinlang.org/u/Philip-Trettner)\
**Post date:** [September 21, 2017, 4:43am UTC](https://discuss.kotlinlang.org/t/unaryplus-minus-and-byte-and-short/4669/3 "2017-09-21T04:43:08Z")

</div>

Uh right I totally missed the `-BYTE_MIN` and `-SHORT_MIN` cases. The same objection doesn’t really hold for `unaryPlus` but I guess then it’s a matter of consistency.

---

<div class="post-metadata">

**Author:** ![CNife](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/cnife/32/2746_2.png) [@CNife](https://discuss.kotlinlang.org/u/CNife)\
**Post date:** [September 21, 2017, 7:24am UTC](https://discuss.kotlinlang.org/t/unaryplus-minus-and-byte-and-short/4669/4 "2017-09-21T07:24:08Z")

</div>

This is a JVM feature, Kotlin has to obey it.  
**JVM provides no bytecode to apply arithmatic operations to `byte` and `short` type.**  
In fact, JVM can only do arithmatic operations on four primitive types: `int`, `long`, `float` and `double`.

Before arithmatic operation on other primitive types, Java compiler or JVM will extend it to `int` type number:

- `byte` & `short` → **Signed-Extend** → `int`
- `char` & `boolean` → **Zero-Extend** → `int`

Because of this , `int` is the **Computational Type** of `byte`, `short`, `char` and `boolean`.

Example:

```nohighlight
// Java code
short s = 200; // 200 to avoid iconst_* and bipush
System.out.println(-s);

```

bytecode:

```auto
sipush 200 -> Push short number 200 to stack's top
istore_1 -> store this number as int into slot 1
getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream
iload_1 -> load 200 as int from slot 1
ineg -> apply unary minus on 200
invokevirtual #3 // Method java/io/PrintStream.println:(I)V

```

It’s clear that before do unary minus on `short` number 200, 200 is extended to `int`.
