- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - Character toString() Method
The Java Character toString() method is used to produce a String representation of a Character Object value. The result is a string of length 1 whose sole component is the primitive char value represented by this Character object.
While printing any object, the Java compiler internally invokes the toString() method present in the Object class on it. Therefore, to acquire the desired output, we can override this method in Object class according to our implementation.
This method occurs in two polymorphic forms: with same return type String but with parameter difference.
Syntax
Following is the syntax for Java Character toString() method
public String toString() (or) public static String toString(char c)
Parameters
c − the char to be converted
Return Value
This method returns a string representation of this object.
Example
The following example shows the usage of Java Character toString() method.
package com.tutorialspoint; import java.lang.*; public class CharacterDemo { public static void main(String[] args) { // create 2 Character objects c1, c2 Character c1, c2; // assign values to c1, c2 c1 = new Character('h'); c2 = new Character('a'); // create 2 String objects s1, s2 String s1, s2; // assign String values of c1, c2 to s1, s2 s1 = c1.toString(); s2 = c2.toString(); String str1 = "String value of " + c1 + " is " + s1; String str2 = "String value of " + c2 + " is " + s2; // print s1, s2 values System.out.println( str1 ); System.out.println( str2 ); } }
Output
Let us compile and run the above program, this will produce the following result −
String value of h is h String value of a is a
Example
The following example shows the usage of Java Character toString(char c) method.
package com.tutorialspoint; import java.lang.*; public class CharacterDemo { public static void main(String[] args) { // create 2 char primitives ch1, ch2 char ch1, ch2; // assign values to ch1, ch2 ch1 = 'V'; ch2 = 115; // create 2 String objects s1, s2 String s1, s2; // assign String values of ch1, ch2 to s1, s2 s1 = Character.toString(ch1); s2 = Character.toString(ch2); String str1 = "String value of " + ch1 + " is " + s1; String str2 = "String value of " + ch2 + " is " + s2; // print s1, s2 values System.out.println( str1 ); System.out.println( str2 ); } }
Output
Let us compile and run the above program, this will produce the following result −
String value of V is V String value of s is s
Example
When we pass symbols as arguments to the method, the return value will be the string representation of the given symbol.
import java.lang.*; public class StringDemo { public static void main(String[] args) { char ch; String result; ch = '%'; result = Character.toString(ch); System.out.println("String of " + ch + " is " + result); } }
Output
Compile and run the program above to obtain the output as follows −
String of % is %
Example
Similar to the previous example, when we pass digits as arguments to the method, the return value will be the string representation of the given symbol.
import java.lang.*; public class StringDemo { public static void main(String[] args) { char ch; String result; ch = '5'; result = Character.toString(ch); System.out.println("String of " + ch + " is " + result); } }
Output
Compile and run the program above to obtain the output as follows −
String of 5 is 5