Java.lang.Character.toString() Method


Advertisements


Description

The java.lang.Character.toString() returns a String object representing this Character's value. The result is a string of length 1 whose sole component is the primitive char value represented by this Character object.

Declaration

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

public String toString()

Overrides

toString in class Object

Parameters

NA

Return Value

This method returns a string representation of this object.

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 Character objects c1, c2
      Character c1, c2;

      // assign values to c1, c2
      c1 = new Character('h');
      c2 = new Character('5');

      // 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 );
   }
}

Let us compile and run the above program, this will produce the following result −

String value of h is h
String value of 5 is 5

java_lang_character.htm

Advertisements