Character.valueOf() in Java with examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the
// Java.lang.Character.valueOf() method
// when the assigned char is a character
  
import java.lang.*;
  
public class Gfg {
  
    public static void main(String[] args)
    {
        // Create a character object
        Character c = new Character('z');
  
        // assign the primitive value to a character
        char ch = c.charValue();
  
        System.out.println("Character value of " + ch + " is " + c);
    }
}

chevron_right


Output:

Character value of z is z

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the
// Java.lang.Character.valueOf() method
// when the assigned char is a number
  
import java.lang.*;
  
public class Gfg {
  
    public static void main(String[] args)
    {
        // Create a character object
        Character c = new Character('5');
  
        // assign the primitive value to a character
        char ch = c.charValue();
  
        System.out.println("Character value of " + ch + " is " + c);
    }
}

chevron_right


Output:

Character value of 5 is 5


My Personal Notes arrow_drop_up

Contented with little yet wishing for more

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.