What is a "[Ljava.lang.String", and how to print its content human readably?


I think this problem stems from my lack of understanding of Java.
I’d like to print the chars of a string which I’m guessing is some sort of funny java string.

I was given an object of type InputStream an needed to read the lines and parse them by a particular rule.
I used

val lines = Source.createBufferedSource(s).getLines()

to apparently get an Iterator for the lines.
Next, I used the following to get what IntelliJ thinks is an object of type Iterator[Array[String]]

val plists = lines.map(line => line.split(",").map(_.trim)).filter(list => ! list.contains(""))

When I try to print this with println, it prints as something I don’t understand.


The output is the following.


Can someone tell me what this means? I notice the opening [ is never closed. Is the type name really “[Ljava.lang.string”? Is there a way to print the actual strings of characters?


Could be that that question is really a red herring, although I’d still love to know the answer.

What would also be helpful is to know how to map/flatMap a function across all the lines (as strings) given an object of type InputStream.


It’s just what the toString method of a JVM array returns.
The default implementation of toString that gets inherited from java.lang.Object is more or less equal to this:

def toString(): String = this.getClass.getName + "@" + this.hashCode.toHexString

The class name of a String array is [Ljava.lang.String;. [ means it’s an array, L means it can contain references to objects, and java.lang.String means all those objects should be instances of java.lang.String. The ; is just because Java loves its semicolons, I suppose.

If you want to pretty print an array you can use java.util.Arrays.toString.

val list = List(Array(1,2),Array(3,4))
println( list.map(java.util.Arrays.toString) )  // List([1, 2], [3, 4])

When you play around with arrays in the Scala REPL, most of the time you won’t see things like [Ljava.lang.String;@49c7b90e. That’s because the REPL contains special cases for when the results of expressions contain arrays.


Note that the Scala repl has some magic to identify arrays when printing results, but it doesn’t work with arrays nested in other data structures:

scala> Array(1, 2)
res0: Array[Int] = Array(1, 2)

scala> Some(Array(1, 2))
res1: Some[Array[Int]] = Some([I@6056232d)


For some reason it does work for arrays nested in standard library collections.


Are you sure java.util.Arrays.toString is correct? When I try the following:


I get an error "cannot resolve symbol toString"

the type of words is Array[String]

I get the same error if I try to convert it to a list first.



You’re right, it doesn’t work for Array[String]. There is only a method toString for Array[Object] because in Java arrays are covariant…

Then better to ignore java.util.Arrays.toString and do something like

words.map(_.mkString("[", ", ", "]"))


Another option, if it’s just for debugging (easier to type but runs
slower), is to to .toList.toString on it.