Search code examples
javastringcharliteralsgarbage

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?


Solution

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