How do I convert a Java byte array into a Scala byte array?

mtsz :

I am new to Scala and work currently on a project involving both Java and a Scala modules. Now I'd like to call a Scala method from Java using a parameter of type byte[].

The Scala method has the signature: def foo(data: Array[Byte])

The Java call looks like this: foo(x), where x has the type byte[].

The IDE tells me its not possible:

The method foo(Array) in the type Bar is not applicable for the arguments (byte[])

As an additional constraint it is not preferred to change the Scala method. On the Java side I tried using Byte[], but this didn't solve the problem. There must exist some conversion?

mtsz :

As others pointed out, there is no problem in conversion. My IDE is behaving erroneous, and showing imaginary errors which compile without problems. At this moment the call of the receive Method in the main-method in following code is marked with the error:

The method receive(Array) from the type ScalaByteReceiver refers to the missing type Array

But this code, which exemplifies my question, compiles fine and yields the expected result:


package stackOverflow;

public class JavaByteSender {    
    public static void main(String... args) {
    new ScalaByteReceiver().receive(new byte[4]);


package stackOverflow

import stackOverflow._

class ScalaByteReceiver{

  def receive(bytes: Array[Byte]) {    
    // prints 4

So Java and Scala understand each other nicely.

Collected from the Internet

Please contact javaer1[email protected] to delete if infringement.

edited at


Login to comment


Java DataInputStream convert to byte array?

How to convert array byte to string in java

Convert ByteBuffer to byte array java

How to convert a Scala Array[Byte] to Java byte[]?

how to convert image to byte array in java?

In Java, how do I convert a byte array to a string of hex digits while keeping leading zeros?

How to convert a Java String to an ASCII byte array?

How to Convert byte array to hexString in java?

How to convert array of byte to String in Java?

How to convert a String array to a Byte array? (java)

How to convert a byte array to a hex string in Java?

How do I initialize a byte array in Java?

How do I convert a byte array with null terminating character to a String in Java?

How do I clone a java byte array?

In Java, how can I convert an InputStream into a byte array (byte[])?

How do I convert a byte slice of unknown size to a byte array?

How do I convert byte array to io. stream and convert it back to byte array?

How to convert specific byte in a byte array to integer

How to convert signed byte array to ascii in Java

How to convert byte array to Mat object in Java

How do I convert a JSONObject to a byte array and then convert this byte array to get back the original JSONObject?

How BigInteger convert a byte array to number in Java?

How do I create an empty byte array?

How do I convert separate int values to hex byte array

How do you read from an InputStream in Java and convert to byte array?

How do I convert a string array to a byte array using Array ConvertAll?

How do I convert a WWWForm that will be a parameter for UploadHandlerRaw into a byte array?

How do I convert [][]byte to []byte?

How to convert a array of bits to a byte, (not to byte string)?