本篇文章帮大家学习java字符数据类型,包含了Java字符数据类型使用方法、操作技巧、实例演示和注意事项,有一定的学习价值,大家可以用来参考。
Character类的一种对象包装一个char值的包装类。Character类包含isLetter()和isDigit()方法来检查字符是否为字母或数字。
toUpperCase()和toLowerCase()方法将字符转换为大写和小写。该类提供了一个构造函数和一个工厂valueOf()方法来从char类型值创建对象。charValue()方法返回对象包装的char类型值。
示例
以下代码显示了如何创建Character对象以及如何使用它的一些方法:
public class Main {
public static void main(String[] args) {
// Using the constructor
Character c1 = new Character('A');
// Using the factory method - preferred
Character c2 = Character.valueOf('2');
Character c3 = Character.valueOf('-');
// Getting the wrapped char values
char cc1 = c1.charValue();
char cc2 = c2.charValue();
char cc3 = c3.charValue();
System.out.println("c1 = " + c1);
System.out.println("c2 = " + c2);
System.out.println("c3 = " + c3);
// Using some Character class methods on c1
System.out.println("isLowerCase c1 = " + Character.isLowerCase(cc1));
System.out.println("isDigit c1 = " + Character.isDigit(cc1));
System.out.println("isLetter c1 = " + Character.isLetter(cc1));
System.out.println("Lowercase of c1 = " + Character.toLowerCase(cc1));
// Using some Character class methods on c2
System.out.println("isLowerCase c2 = " + Character.isLowerCase(cc2));
System.out.println("isDigit c2 = " + Character.isDigit(cc2));
System.out.println("isLetter c2 = " + Character.isLetter(cc2));
System.out.println("Lowercase of c2 = " + Character.toLowerCase(cc2));
System.out.println("Uppercase of c3 = " + Character.toUpperCase(cc3));
}
}
上面的代码生成以下结果。
c1 = A
c2 = 2
c3 = -
isLowerCase c1 = false
isDigit c1 = false
isLetter c1 = true
Lowercase of c1 = a
isLowerCase c2 = false
isDigit c2 = true
isLetter c2 = false
Lowercase of c2 = 2
Uppercase of c3 = -