Java.lang.Character.valueOf()是Java中的内置方法,该方法返回代表指定char值的Character实例。如果不需要新的Character实例,则通常优先于构造方法Character(char)使用此方法,因为此方法通过缓存频繁请求的值可能会产生明显更好的空间和时间性能。此方法将始终缓存范围从['\ u0000'到'\ u007F']在内的值,并且可能会缓存此范围之外的其他值。
用法:
public static Character valueOf(char ch)
Parameter:
ch- this parameter specifies the character.
返回值:此方法返回表示ch的Character实例。
下面的程序演示了Java.lang.Character.valueOf()函数:
程序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);
}
}
输出:
Character value of z is z
程序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);
}
}
输出:
Character value of 5 is 5