Byte Arrays in JRuby

  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.

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:

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

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.

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.

BTW, on

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

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

Ah, I think I found the answer.

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


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[]