Java.lang.Character.toString() Method



Description

The java.lang.Character.toString(char c) returns a String object representing the specified char. The result is a string of length 1 consisting solely of the specified char.

Declaration

Following is the declaration for java.lang.Character.toString() method

public static String toString(char c)

Parameters

c − the char to be converted

Return Value

This method returns the string representation of the specified char.

Exception

NA

Example

The following example shows the usage of lang.Character.toString() 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 );
   }
}

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