Skip to content
Related Articles

Related Articles

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:






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


Output:

Character value of z is z

Program 2:




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


Output:

Character value of 5 is 5

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :