# Difference between ByteArray and Array\<Byte\>

**URL:** <https://discuss.kotlinlang.org/t/difference-between-bytearray-and-array-byte/1273>\
**Category:** Uncategorized\
**Created:** [February 27, 2012, 10:23pm UTC](https://discuss.kotlinlang.org/t/difference-between-bytearray-and-array-byte/1273 "2012-02-27T22:23:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chammers](https://avatars.discourse-cdn.com/v4/letter/c/bc8723/32.png) [@chammers](https://discuss.kotlinlang.org/u/chammers)\
**Post date:** [February 27, 2012, 10:23pm UTC](https://discuss.kotlinlang.org/t/difference-between-bytearray-and-array-byte/1273/1 "2012-02-27T22:23:35Z")

</div>

I don't understand why e.g. the java.security.MessageDigest.digest() method which is declared as returning "byte[]" in Java returns a "ByteArray" in Kotlin although the Kotlin examples usually use "Array\<Byte\>".

E.g. the following does not work:

> fun main(args : Array\<String\>) {
> 
> &nbsp;&nbsp;val md = java.security.MessageDigest.getInstance(“SHA”)
> 
> &nbsp;&nbsp;if (md == null) throw NullPointerException()
> 
> &nbsp;&nbsp;val result : Array\<Byte\>? = md.digest()
> 
> }
> 
> Type mismatch: inferred type is ByteArray? but Array? was expected

How should I covert those two?

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [February 28, 2012, 5:35am UTC](https://discuss.kotlinlang.org/t/difference-between-bytearray-and-array-byte/1273/2 "2012-02-28T05:35:58Z")

</div>

> I don't understand why e.g. the java.security.MessageDigest.digest() method which is declared as returning "byte[]" in Java returns a "ByteArray" in Kotlin although the Kotlin examples usually use "Array\<Byte\>".

Please, see my answer here: [http://stackoverflow.com/questions/9457942/difference-between-bytearray-and-arraybyte-in-kotlin/9462835#9462835](http://stackoverflow.com/questions/9457942/difference-between-bytearray-and-arraybyte-in-kotlin/9462835#9462835)

On the conversion part: we should add conversion functions to the standard library. Do you mind filing an issue about it? Thanks.

---

<div class="post-metadata">

**Author:** ![chirino](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/chirino/32/1630_2.png) [@chirino](https://discuss.kotlinlang.org/u/chirino)\
**Post date:** [March 7, 2012, 8:05pm UTC](https://discuss.kotlinlang.org/t/difference-between-bytearray-and-array-byte/1273/3 "2012-03-07T20:05:20Z")

</div>

The type system would be easier to deal with if you only had to work with Array\<Byte\> types.&nbsp;&nbsp;I would be nice if the compiler auto converted those to the specialized types and hid that from the user.&nbsp;&nbsp;If the user really wanted a non-specialized, boxed array type then he should be using nullable type perhaps like Array\<Byte?\>.

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [March 7, 2012, 8:06pm UTC](https://discuss.kotlinlang.org/t/difference-between-bytearray-and-array-byte/1273/4 "2012-03-07T20:06:58Z")

</div>

This is what we started with. Unfortunately, it doesn't play well with generics.

---

<div class="post-metadata">

**Author:** ![chirino](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/chirino/32/1630_2.png) [@chirino](https://discuss.kotlinlang.org/u/chirino)\
**Post date:** [March 7, 2012, 8:13pm UTC](https://discuss.kotlinlang.org/t/difference-between-bytearray-and-array-byte/1273/5 "2012-03-07T20:13:04Z")

</div>

Yeah guess you'd have to be able generate specialized implemenations for all generic classes too automatically if you also want to avoid autoboxing in those cases also.&nbsp;&nbsp;Is that what your refering too?

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [March 7, 2012, 8:14pm UTC](https://discuss.kotlinlang.org/t/difference-between-bytearray-and-array-byte/1273/6 "2012-03-07T20:14:37Z")

</div>

Basically, yes.
