# Byte Arrays in JRuby

**URL:** <https://www.ruby-forum.com/t/byte-arrays-in-jruby/243653>\
**Category:** JRuby\
**Created:** [December 16, 2015, 1:37am UTC](https://www.ruby-forum.com/t/byte-arrays-in-jruby/243653 "2015-12-16T01:37:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafa\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Rafa\_F](https://www.ruby-forum.com/u/Rafa_F)\
**Post date:** [December 16, 2015, 1:37am UTC](https://www.ruby-forum.com/t/byte-arrays-in-jruby/243653/1 "2015-12-16T01:37:37Z")

</div>

1. Does Java allow for creating byte arrays (each element is a single  
8-bit addressable element)?

2. If so, can JRuby create and access them like regular Ruby class Array  
objects? (Ex: myarray = BArray.new; 10.times {|i| myarray \<\< i}

3. Is there an existing Java/JRuby library/gem that provides this for  
JRuby?

I’m doing numerical processing where I really need boolean arrays (an  
array of bits), but since most cpus are byte addressable I guess the  
best I can get, and still perform as regular Ruby arrays, is byte  
addressable arrays.

---

<div class="post-metadata">

**Author:** ![jabari\_z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@jabari\_z](https://www.ruby-forum.com/u/jabari_z)\
**Post date:** [January 5, 2016, 10:21am UTC](https://www.ruby-forum.com/t/byte-arrays-in-jruby/243653/2 "2016-01-05T10:21:37Z")

</div>

Jabari Zakiya wrote in post #1179970:

> 1. Does Java allow for creating byte arrays (each element is a single  
> 8-bit addressable element)?

You have the type java.lang.Byte:

> **[Byte (Java Platform SE 7 )](https://docs.oracle.com/javase/7/docs/api/java/lang/Byte.html)**

> 1. If so, can JRuby create and access them like regular Ruby class Array  
> objects? (Ex: myarray = BArray.new; 10.times {|i| myarray \<\< i}

Can’t you just create a normal Ruby array, make sure that you stuff into  
it java.lang.Byte objects, and when you finally need it on the Java  
side, apply the ‘to\_java’ method on it?

Ronald

---

<div class="post-metadata">

**Author:** ![jabari\_z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@jabari\_z](https://www.ruby-forum.com/u/jabari_z)\
**Post date:** [January 11, 2016, 7:20pm UTC](https://www.ruby-forum.com/t/byte-arrays-in-jruby/243653/3 "2016-01-11T19:20:23Z")

</div>

Thanks for your reply.

I am not a Java programmer (have done no “real” Java program) so I know  
virtually nothing of manipulating the language.

I’ll look at the links you supplied and see if they help me.

I’m trying to extend usable memory when running the same gem in MRI vs  
JRuby. JRuby runs out of memory much faster on my apps than MRI does on  
certain use cases, making it more of a niche language for me than a  
general go to Ruby implementation.

---

<div class="post-metadata">

**Author:** ![jabari\_z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@jabari\_z](https://www.ruby-forum.com/u/jabari_z)\
**Post date:** [January 12, 2016, 7:33am UTC](https://www.ruby-forum.com/t/byte-arrays-in-jruby/243653/4 "2016-01-12T07:33:31Z")

</div>

I don’t quite see, how working with byte arrays might help you. If I  
remember right, there is an option when starting Java, where you set the  
maximum memory to be used. If you don’t find it in the Java docs, I  
suggest you ask this in a Java forum.

---

<div class="post-metadata">

**Author:** ![jabari\_z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@jabari\_z](https://www.ruby-forum.com/u/jabari_z)\
**Post date:** [January 11, 2016, 4:30pm UTC](https://www.ruby-forum.com/t/byte-arrays-in-jruby/243653/5 "2016-01-11T16:30:40Z")

</div>

BTW, on

> **[FAQs](https://github.com/jruby/jruby/wiki/FAQs)**
>
> JRuby, an implementation of Ruby on the JVM. Contribute to jruby/jruby development by creating an account on GitHub.

have a look at the section which says “How do I create a primitive Java  
array in JRuby?”

---

<div class="post-metadata">

**Author:** ![jabari\_z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@jabari\_z](https://www.ruby-forum.com/u/jabari_z)\
**Post date:** [January 12, 2016, 9:06pm UTC](https://www.ruby-forum.com/t/byte-arrays-in-jruby/243653/6 "2016-01-12T21:06:10Z")

</div>

You can read why I would like to do this from the document below:

> **[Primes Utils Handbook | PDF | Prime Number | Ruby (Programming Language)](https://www.scribd.com/document/266461408/Primes-Utils-Handbook)**
>
> Official handbook for the primes-utils rubygem.

---

<div class="post-metadata">

**Author:** ![jabari\_z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@jabari\_z](https://www.ruby-forum.com/u/jabari_z)\
**Post date:** [January 12, 2016, 9:29pm UTC](https://www.ruby-forum.com/t/byte-arrays-in-jruby/243653/7 "2016-01-12T21:29:08Z")

</div>

Ah, I think I found the answer.

This is how to do it, according to this site:

> **[FAQs](https://github.com/jruby/jruby/wiki/FAQs)**
>
> JRuby, an implementation of Ruby on the JVM. Contribute to jruby/jruby development by creating an account on GitHub.

* * *

How do I create a primitive Java array in JRuby?

You can convert existing ruby arrays to primitive Java arrays very  
simply:

```
[1,2,3].to_java # makes an object array
[1,2,3].to_java :byte # makes a byte array
[1,2,3].to_java :String # makes a String array

```

To create empty arrays:

```
Java::byte[12].new # makes a new byte[]
java.lang.String[12].new # makes a new String[]

```
