Java.lang Character
1 Java.lang Character介绍
Character类通常将所有基本类型char的值包装到一个对象中。字符类型的任何对象都可以包含一个类型为char的字段。
字符类的所有字段,方法和构造函数均由Unicode数据文件指定,该文件特别是Unicode字符数据库的一部分,并由Unicode联盟维护。
从U+0000到U+FFFF的一组字符有时被称为基本多语言平面(即BMP)。代码点大于U+FFFF的字符称为补充字符。Java语言通常使用UTF-16编码方法来表示String或String Buffer中的char数组。在这种表示形式中,辅助字符表示为一对字符,第一个字符来自高代理范围(\uD800-\uDBFF),第二个字符来自低代理范围(\uDc00-\uDBFF)。
2 Java.lang Character声明
public final class Character
extends Object
implements Serializable, Comparable<Character>
3 Java.lang Character方法
4 Java.lang Character案例1
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
import java.util.Scanner;
public class JavaCharacterExample1 {
public static void main(String[] args) {
// Ask the user for the first input.
System.out.print("Enter the first input:");
// Use the Scanner class to get the user input.
Scanner scanner = new Scanner(System.in);
// Gets the user input.
char[] value1 = scanner.nextLine().toCharArray();
int result1 = 0;
// Count the characters for a specific character.
for (char ch1 : value1) {
result1 = Character.charCount(ch1);
}
// Print the result.
System.out.print("The value comes to: "+result1+"\n");
System.out.print("Enter the second input:");
char[] value2 = scanner.nextLine().toCharArray();
for (char ch2 : value2) {
int result2 = Character.hashCode(ch2);
System.out.print("The hash code for the character '"+ch2+"' is given as:"+result2+"\n");
}
System.out.print("Enter the third input:");
char[] value3 = scanner.nextLine().toCharArray();
for (char ch3 : value3) {
boolean result3 = Character.isDigit(ch3);
if(result3){
System.out.println("The character '" + ch3 + "' is a digit. ");
}
else{
System.out.println("The character '" + ch3 + "' is not a digit.");
}
System.out.print("Enter the fourth input:");
char[] value4 = scanner.nextLine().toCharArray();
for (char ch4 : value4) {
boolean result4 = Character.isISOControl(ch4);
System.out.println("The fourth character '"+ch4+"' is an ISO Control:"+result4);
}
}
}
}
输出结果为:
Enter the first input:89
The value comes to: 1
Enter the second input:J
The hash code for the character 'J' is given as:74
Enter the third input:5
The character '5' is a digit.
Enter the fourth input:h
The fourth character 'h' is an ISO Control:false
5 Java.lang Character案例2
public class JavaCharacterExample2{
public static void main(String[] args) {
// Create four char primitives ch1, ch2, ch3 and ch4.
char ch1, ch2, ch3, ch4;
// Assign the values to ch1, ch2, ch3 and ch4.
ch1 = 'T';
ch2 = 'k';
ch3 = '\n';
ch4 = 'G';
// Create four boolean primitives b1, b2, b3 and b4.
boolean b1, b2, b3,b4;
// Assign the results to b1, b2, b3 and b4.
b1 = Character.isLetter(ch1);
b2 = Character.isLowerCase(ch2);
b3 = Character.isSpace(ch3);
b4 = Character.isDefined(ch3);
String str1 = " The first character is a letter: " + b1;
String str2 = " The second character is a lower-case: " + b2;
String str3 = " The third character is for space: " + b3;
String str4 = " The fourth character is defined in Java identifier: " + b3;
// Print the values of b1, b2, b3 and b4.
System.out.println( str1 );
System.out.println( str2 );
System.out.println( str3 );
System.out.println( str3 );
}
}
输出结果为:
The first character is a letter: true
The second character is a lower-case: true
The third character has a space: true
The third character has a space: true
热门文章
优秀文章