Java - Character toString() Method



Description

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.

Getting String representation of Character Object Example

The following example shows the usage of Java Character toString() method. In this program, we've created two Character variables and assigned them two Character Objects and then using toString(), we've retrieved the string representation and values are printed.

package com.tutorialspoint;

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

Getting String representation of char Example

The following example shows the usage of Java Character toString() method. In this program, we've created two char variables and assigned them values and then using toString(), we've retrieved the string representation and values are printed.

package com.tutorialspoint;

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

Getting String representation of char as symbols Example

The following example shows the usage of Java Character toString() method. In this program, we've created a char variable and assigned it a symbol as value and then using toString(), we've retrieved the string representation and result is printed.

package com.tutorialspoint;

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 %

Getting String representation of char as digit Example

The following example shows the usage of Java Character toString() method. In this program, we've created a char variable and assigned it a digit as value and then using toString(), we've retrieved the string representation and result is printed.

package com.tutorialspoint;

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
java_lang_character.htm
Advertisements