Deersfeet Deersfeet - 7 months ago 22
Java Question

String literal with toCharArray() producing garbage in Java

I wanted to create a char array of the alphabet. I looked at this post:

Better way to generate array of all letters in the alphabet

which said this:

char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();


So in my code I have:

public class Alphabet {

private char[] letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();

public String availableLetters(){

return letters.toString();
}

}


When I call the function availableLetters() from main() and printit to the console, it outputs this garbage:

[C@15db9742


What am I doing wrong?

Answer

You can pass the char array to the String constructor or the static method String.valueOf() and return that instead.

Comments