char is 16 bit unsigned data type in Java, which is used to store characters and String value is an immutable array of char. In Java we cannot cast a primitive char element to String.
tl;dr
Use String.valueOf(char)
6 Ways to do it
Below I have given five 6 methods to convert a char to String. Also I have included common mistakes that gives compile time errors.
The toString methods in java.lang.Character class calls the String.valueOf(char) method.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package java.lang.Character;
...
public String toString(){
char buf[] = {value};
return String.valueOf(buf);
}
...
publicstatic String toString(char c){
return String.valueOf(c);
}
And here is what String.valueOf(char[]) and String.valueOf(char) methods looks like.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package java.lang.String;
...
/**
* Returns the string representation of the {@code char} array
* argument. The contents of the character array are copied; subsequent
* modification of the character array does not affect the returned
* string.
*
* @param data the character array.
* @return a {@code String} that contains the characters of the
* character array.
*/
publicstatic String valueOf(char data[]){
returnnew String(data);
}
...
/**
* Returns the string representation of the {@code char}
* argument.
*
* @param c a {@code char}.
* @return a string of length {@code 1} containing
* as its single character the argument {@code c}.
*/
publicstatic String valueOf(char c){
char data[] = {c};
returnnew String(data, true); // package private
}
Here are the constructors that is called in the above methods.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package java.lang.String;
...
/**
* Allocates a new {@code 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.
*
* @param value
* The initial value of the string
*/
publicString(char value[]){
this.value = Arrays.copyOf(value, value.length);
}
...
/*
* Package private constructor which shares value array for speed.
* this constructor is always expected to be called with share==true.
* a separate constructor is needed because we already have a public
* String(char[]) constructor that makes a copy of the given char[].
*/
String(char[] value, boolean share) {
// assert share : "unshared not supported";
this.value = value;
}
To summarise when you use String.valueOf or Charactor.toString methods, the constructor public String(char value[]) is being invoked. You may as well call it directly and reduce one hop.
Most Efficient Method
String.valueOf(char value) invokes the following package private constructor.
1
2
3
4
String(char[] value, boolean share) {
// assert share : "unshared not supported";
this.value = value;
}
String.valueOf(char) seems to be most efficient method for converting char to String.
Side note
Remember String objects are immutable and they can be shared.
For example:
1
String str = "abc";
is equivalent to:
1
2
char data[] = {'a', 'b', 'c'};
String str = new String(data);
I wonder why Java doesn’t include String constructor that accept a single char as argument.
Java does’nt include String constructor that accepts a single char, because there already is a constructor that accepts a char[].