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

后端 未结 2 825
深忆病人
深忆病人 2021-02-08 16:54

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

2条回答
  •  清歌不尽
    2021-02-08 17:38

    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.

提交回复
热议问题