Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaCharacter.valueOf() in Java with examples

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:




// 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
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments