While writing unit tests for a function returning boolean that takes two Strings, and I needed to test every character of the alphabet ('a'-'z') in sequence as one of the parameters, one-by-one, so I wrote this to do that:
for(char c = 'a'; c <= 'z'; c++)
{
assertTrue(MyClass.MyFunction(testSubject, new String(c));
}
I would have thought this was permissible, but it wasn't, so I just did it like this instead:
for(char c = 'a'; c <= 'z'; c++)
{
assertTrue(MyClass.MyFunction(testSubject, ((Character) c).toString());
}
Is this a reliable method to convert a char to a String in Java? Is it the preferred way? I don't know a whole lot about Java so would like some clarification on this.