Java中的字符.charValue() 和示例
Java.lang。 字符.charValue()是Java中的一个内置方法,它返回此字符对象的值。此方法将字符对象转换为其原始数据类型 char。
句法:
public char charValue()
The function does not accepts any parameter.
返回类型:此方法返回此对象表示的原始 char 值。
下面的程序说明了Java.lang。字符.charValue() 方法:
方案一:
// Java program to demonstrate the
// Java.lang.Character.charValue() method
// when the assigned char is a character
import java.lang.*;
public class Gfg1 {
public static void main(String[] args)
{
// Create a character object
Character x = new Character('z');
// assign the primitive value to a character
char ch = x.charValue();
System.out.println("Primitive char value is " + ch);
}
}
输出:
Primitive char value is z
程序 2:当我们用任何数字分配 x 的值时:
// Java program to demonstrate the
// function when the assigned value is
// a number
import java.lang.*;
public class Gfg {
public static void main(String[] args)
{
// create a Character object x
Character x = new Character('9');
// assign value is a number
// assign the primitive value to a character
char ch = x.charValue();
// print the primitive char value
System.out.println("Primitive char value is " + ch);
}
}
输出:
Primitive char value is 9