Open In App

Program to convert Java list of bytes to a String in Scala

Last Updated : 14 Jan, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

A java list of bytes can be converted to a String in Scala by utilizing toString method of Java in Scala. Here, you need to import Scala’s JavaConversions object in order to make this conversions work.
Now, lets see some examples and then discuss how it works in details.
Example:1#





Output:

[123, 13, -11]

Example:2#




// Scala program to convert Java list
// to a String in Scala
  
// Importing Scala's JavaConversions object
import scala.collection.JavaConversions._
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating list of bytes in Java
        val list = new java.util.ArrayList[Byte]()
          
        // Adding bytes to the list
        list.add(-123)
        list.add(-124)
        list.add(-125)
          
        // Converting list to a String
        val str = list.toString
          
        // Displays output
        println(str)
      
    }
}


Output:

[-123, -124, -125]


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads