Hi, Today I'm going to tell you 3 ways to convert a primitive char to a String.
This internally compiles down to :
First Method:
This one is the simplest a person can think.
String str = "" + 'c';
This internally compiles down to :
String str = new StringBuilder().append("").append('c').toString();
This one is less efficient due to StringBuilder class.
Second Method:
As we cannot invoke toString() on the primitive type char or any other primitive, so we can create a wrapper class Character and then we can eaily invoke toString() to it.
ex.
char c = 'c';
Character ch = Character.valueOf(c);
String str = ch.toString();
Third Method:
char c = 'c';
String str = String.valueOf(c);
Thanks
No comments:
Post a Comment