10

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?

4
  • What IDE are you using? Intellij has no problem with it. Commented Jun 4, 2011 at 16:06
  • Are you using 2.8 or later? Array[Byte] is byte[] as of 2.8, so if your IDE is complaining your IDE is wrong. Commented Jun 4, 2011 at 16:17
  • I am using Eclipse Helios 3.6.2 with installed Scala IDE for Eclipse 2.0.0-beta4. The project is organised in Maven modules, where both Scala and the Java module include scala-library-2.9.0 as Maven dependency. I've noticed that Scala IDE is doing strange things sometimes - like showing imaginary warnings and errors in the package explorer, but usually cleaning and rebuilding the project helps (which i tried in this case already). Commented Jun 4, 2011 at 16:27
  • 1
    Hmm, that is interesting: Calling foo(x) from Java with x of type (raw) scala.Array lets my IDE return: The method foo(byte[]) in the type Bar is not applicable for the arguments (Array). Thats what i would call contradictory behaviour. I should try to compile the code somewhere else... Commented Jun 4, 2011 at 16:41

2 Answers 2

5

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:

Java:

package stackOverflow;

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

Scala:

package stackOverflow

import stackOverflow._

class ScalaByteReceiver{

  def receive(bytes: Array[Byte]) {    
    println(bytes.length);
    // prints 4
  }
}

So Java and Scala understand each other nicely.

Sign up to request clarification or add additional context in comments.

Comments

1

I tried to reproduce your error but it ran as expected. Running with scala 2.9.0 and sbt

java code:

package stackOverflow;

public class ByteContainer {

    private byte[] bytes;

    public ByteContainer(byte[] bytes){
        this.bytes = bytes;
    }

    public byte[] getBytes() {
        return bytes;
    }

    public void setBytes(byte[] bytes) {
        this.bytes = bytes;
    }

}

scala code:

package stackOverflow

import stackOverflow._

class ScalaByte{
    val bytes:Array[Byte] = "this is my test".getBytes()
}

object ByteUser extends App{
    val b = new ByteContainer((new ScalaByte()).bytes)
    val s = b.getBytes()
    println(s)
}

output: [B@6ef38f6f

This compiles and runs. Is this not what you were asking about? feel free to comment.

1 Comment

"Is this not what you were asking about?" - Almost :) ... you pass Scala Array[Byte] to a Java (constructor) method which expects byte[], and I was looking for the solution in the other direction. But you showed successfully this works fine as well. I am convinced my IDE is behaving erroneous, in fact there seems to be no problem with the conversion. I implemented a working example which i will post in a second, which is marked with errors but compiles and yields expected results. But thanks for your code, i took the liberty to use it as a base for the working example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.