Character.valueOf() in Java with examples
Last Updated :
06 Dec, 2018
Java.lang.Character.valueOf() is an inbuilt method in Java that returns a Character instance representing the specified char value. If a new Character instance is not required, this method is generally used in preference to the constructor Character(char), since this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values range [‘\u0000’ to ‘\u007F’] inclusive and may cache other values outside of this range.
Syntax:
public static Character valueOf(char ch)
Parameter:
ch- this parameter specifies the character.
Returns: This method returns a Character instance representing ch.
The program below demonstrates the Java.lang.Character.valueOf() function:
Program 1:
import java.lang.*;
public class Gfg {
public static void main(String[] args)
{
Character c = new Character( 'z' );
char ch = c.charValue();
System.out.println( "Character value of " + ch + " is " + c);
}
}
|
Output:
Character value of z is z
Program 2:
import java.lang.*;
public class Gfg {
public static void main(String[] args)
{
Character c = new Character( '5' );
char ch = c.charValue();
System.out.println( "Character value of " + ch + " is " + c);
}
}
|
Output:
Character value of 5 is 5
Share your thoughts in the comments
Please Login to comment...