6 ways to convert char to String inwards Java - Examples

If yous receive got a char value e.g. 'a' as well as yous desire to convert it into equivalent String e.g. "a" as well as then yous tin move utilization whatsoever of the next 6 methods to convert a primitive char value into String inwards Java :

1) String concatenation
2) String.valueOf()
3) Character.toString()
4) Character wrapper degree + toString
5) String constructor amongst char array
6) String.valueOf(char [])

In this article, nosotros volition run across examples of each approach as well as larn a piffling fleck to a greater extent than virtually it. Actually, in that place is lot of overlap betwixt each method every bit to a greater extent than or less of them internally calls String.valueOf(), which eventually calls to a String constructor which accepts char array as well as creates a String object containing primitive char value amongst length 1. Once yous know, how they operate internally, it tardily to create upwards one's hear which 1 is to a greater extent than efficient for purpose.



Char to String using concatenation

This is the easiest agency to convert a char to String object inwards Java. All yous require to create is concatenate given grapheme amongst empty String, every bit shown inwards the next example:

char apple tree = 'a'; String aStr = "" + apple; System.out.println("char to String using concatenation : " + aStr);   // a

Remark: This is the simplest method to convert a char value to String but it's less efficient as well as takes to a greater extent than retention than required. Compile interpret String concatenation into the next code:
new StringBuilder().append(x).append("").toString();
This is less efficient because the StringBuilder is backed past times a char[] of size 16, which is a waste materials of retention if yous simply converting 1 character. This array is as well as then defensively copied past times the resulting String



Character to String using String.valueOf()

This is the criterion agency to convert a char primitive type into String object. If yous remember, nosotros receive got already used the valueOf() method to convert String to int as well as String to double inwards Java. Here is the instance of using this method to convert char to String:

char man child = 'b'; String bStr = String.valueOf(boy); System.out.println("char to String using String.valueOf() : " + bStr); // "b"

Remark: String.valueOf(char) is the most efficient method for converting char to String inwards Java, because it simply uses a grapheme array of size 1 to roll given grapheme as well as transcend it to String constructor, every bit shown below (from JDK) :


Char to String conversion amongst Character.toString()

This is the 3rd instance to convert a char primitive value to a String object inwards Java. In this example, nosotros receive got used Character.toString() method.

char truthful cat = 'c'; String cStr = Character.toString(cat); System.out.println("char to String using Character.toString() : " + cStr);

Remark: This method returns a String object representing the given char. It returns a String of length 1 consisting alone of the specified char. Internally it likewise calls String.valueOf(char c) method.

BTW, hither is prissy summary of all the six ways to convert a char value to String object inwards Java:

 as well as yous desire to convert it into equivalent String e 6 ways to convert char to String inwards Java - Examples


Char to String using Character wrapper class

This is the fourth instance of converting a char value to String object inwards Java. This time, nosotros volition utilization java.lang.Character class, which is a wrapper for char primitive type.

char domestic dog = 'd'; String dStr = new Character(dog).toString(); System.out.println("char to String using novel Character() + toString : " + dStr);

Remark: This method returns the String equivalent of Character object's value of length 1. Internally this method wraps the encapsulated char value into a char array as well as passed to String.valueOf(char[]) method.


String constructor amongst char array

Here is 1 to a greater extent than agency to convert a char value to String object inwards Java. In this example, nosotros receive got wrapped the unmarried grapheme into a grapheme array as well as passed it to a String constructor which accepts a char array every bit shown below:

String fStr = new String(new char[]{'f'}); System.out.println("char to String using novel String(char array) : " + fStr);

Remark: This is the method which is internally called past times String.valueOf() method for char to String conversion. This constructor allocates a novel String object to stand upwards for a sequence of characters passed via char array argument. The contents of the grapheme array are defensively copied thus that subsequent change of the grapheme array doesn't comport upon the newly created String.


char to String using String.valueOf(char[])

This the 6th as well as final agency to perform char to String conversion inwards our article, hither is the sample code:
String gStr = String.valueOf(new char[]{'g'}); System.out.println("char to String using String.valueOf(char array) : " + gStr);

Remark: String.valueOf() is overloaded to receive got both char as well as char[]. Since the valueOf() method which receive got a char, internally roll that char into a grapheme array it brand feel to utilization this overloaded method to straight transcend a char array, which inwards plough passed to String constructor which accepts a grapheme array.


That's all virtually how to convert a char value to String inwards Java. You tin move guide whatsoever of the to a higher house methods, which suits your attempt out but remember, String concatenation is non efficient as well as tin move waste materials a lot of memories if used inwards the loop. String.valueOf() is to a greater extent than readable as well as efficient, thus should locomote your default choice. All of the to a higher house methods internally uses new String(char[]) though.

Subscribe to receive free email updates:

0 Response to "6 ways to convert char to String inwards Java - Examples"

Posting Komentar