當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Character.charValue()用法及代碼示例


Java.lang.Character.charValue()是Java中的內置方法,該方法返回此字符對象的值。此方法將Character對象轉換為其原始數據類型char。

用法:

public char charValue() 

The function does not accepts any parameter. 

返回類型:此方法返回此對象表示的原始char值。


以下程序說明了Java.lang.Character.charValue()方法:

示例1:

// 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


相關用法


注:本文由純淨天空篩選整理自Twinkl Bajaj大神的英文原創作品 Character.charValue() in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。