Character 类的 valueOf(char c) 方法返回表示给定 char 值的 Character 实例。如果不需要 Character 的新对象,则应优先使用该方法而不是构造函数 Character(char)。
上述方法很可能提供更好的空间和时间性能。此方法将缓存 '\u0000' 到 '\u007F' 范围内的值,并且可能存在缓存范围之外的其他值的一些可能性。
用法
public static Character valueOf(char c)
参数
c: 是需要测试的字符值。
返回值
valueOf(char c) 方法返回 Character 的对象。
例子1
public class JavaCharactervalueOfExample1 {
public static void main(String[] args) {
// Create two character primitives c1 and c2.
Character c1, c2;
// Create two char primitives and assign the values.
char ch1 = 'A';
char ch2 = 'h';
// Assign the character values of ch1 and ch2 to c1 and c2.
c1 = Character.valueOf(ch1);
c2 = Character.valueOf(ch2);
String str1 = "The character value of the character '" + ch1 + "' is given as:" + c1;
String str2 = "The character value of the character '" + ch2 + "' is given as:" + c2;
// Print the values of ch1 and ch2.
System.out.println( str1 );
System.out.println( str2 );
}
}
输出:
The character value of the character 'A' is given as:A The character value of the character 'h' is given as:h
例子2
public class JavaCharactervalueOfExample2 {
public static void main(String[] args) {
// Declare two string values.
String str1 = "Hello";
String str2 = "Everyone";
// Convert string value to character array.
System.out.println("The word 'Hello' can be represented as:");
char[] values1 = str1.toCharArray();
for(char ch1:values1){
// Print the value.
System.out.println(String.valueOf(ch1));
}
System.out.println("The word 'Everyone' can be represented as:");
char[] values2 = str2.toCharArray();
for(char ch2:values2){
// Print the value.
System.out.println(String.valueOf(ch2));
}
}
}
输出:
The word 'Hello' can be represented as: H e l l o The word 'Everyone' can be represented as: E v e r y o n e
相关用法
- Java Character isLetter()用法及代码示例
- Java Character isAlphabetic()用法及代码示例
- Java Character isValidCodePoint()用法及代码示例
- Java Character codePointCount()用法及代码示例
- Java Character isISOControl()用法及代码示例
- Java Character getType()用法及代码示例
- Java Character getNumericValue()用法及代码示例
- Java Character isSpace()用法及代码示例
- Java Character toTitleCase()用法及代码示例
- Java Character isMirrored()用法及代码示例
- Java Character isBmpCodePoint()用法及代码示例
- Java Character toUpperCase()用法及代码示例
- Java Character isIdentifierIgnorable()用法及代码示例
- Java Character isDigit()用法及代码示例
- Java Character digit()用法及代码示例
- Java Character toChars()用法及代码示例
- Java Character compare()用法及代码示例
- Java Character isLowerCase()用法及代码示例
- Java Character isJavaIdentifierPart()用法及代码示例
- Java Character getType(char ch)用法及代码示例
注:本文由纯净天空筛选整理自 Java Character valueOf() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。