java – Get String from Object which is a char array (NOT an Object[])

java – Get String from Object which is a char array (NOT an Object[])

If you have a char[] and you want to convert it to a String simply use the constructor

public String(char[] value)

From javadoc:

Allocates a new String so that it represents the sequence of characters currently contained in the character array argument. The contents of the character array are copied; subsequent modification of the character array does not affect the newly created string.


Note

If you have an Object that is a char[] here is what you have:

char[] x = ....;
Object objectIsACharArray = x;
String s = new String((char[]) x);

If the Object is not a char[] but is something that represent or hold a char array you need a custom code to convert it to a String.

java – Get String from Object which is a char array (NOT an Object[])

Leave a Reply

Your email address will not be published. Required fields are marked *