1.构造方法:
Character ch1 = new Character('A');
以char型变量作为参数创建Character对象;
2.常用方法:
Character类提供了许多方法完成对字符的操作:
①charValue(): 返回值类型:char;
功能说明:返回Character对象的值。
②compareTo(Character anotherCharacter):返回值类型:int
功能说明:比较两个Character对象,若相等返回0,若调用对象小于anotherCharacter对象返回负值,
否则返回正值
③equals(Character anotherCharacter):返回值类型:boolean
功能说明:比较两个Character对象,若相等返回true,否则返回false。
④toUpperCase(char ch):将字符参数转化为大写(要求必须有参数)
⑤toLowerCase(char ch):将字符参数转化为小写()
⑥toString():返回值类型:String
功能说明:有三种调用方式:
1.String str = Character.toString('A');
2.Character ch = Character.valueOf('A');
String str = Character.toString(ch);
3.Character ch = Character.valueOf('A');
String str = ch.toString();
⑦isUpperCase(char ch):返回值类型:boolean
功能说明:判断指定字符是否是大写(要求必须有参数)
⑧isLowerCase(char ch):返回类型:boolean
功能说明:判断指定字符是否是小写(要求必须有参数)
⑨valueOf(char ch);返回值类型:Character对象
功能说明:返回值为ch的Character对象。
注意:参数只能为char类型,而不能为String类型。
eg:
<span style="font-size: 14px;">package Number;<br/>public class IntFunction{<br/> public static void main (String []args)<br/> {<br/> Character ch1 = Character.valueOf('A');<br/> Character ch2 = new Character('A');<br/> Character ch3 = Character.valueOf('C');<br/> char c1 = ch1.charValue();<br/> char c2 = ch2.charValue();<br/> char c3 = ch3.charValue();<br/> System.out.println("ch1:" + c1 + ", ch2:" + c2 + ", ch3:" + c3);<br/> int a1 = ch1.compareTo(ch2);<br/> int a2 = ch1.compareTo(ch3);<br/> System.out.println("ch1.compareTo(ch2):" + a1 + ", ch1.compareTo(ch3):" + a2);<br/> boolean bool1 = ch1.equals(ch2);<br/> boolean bool2 = ch1.equals(ch3);<br/> System.out.println("ch1.equals(ch2): " + bool1 + ", ch1.equals(ch3): " + bool2);<br/> boolean bool3 = Character.isUpperCase(ch1);<br/> boolean bool4 = Character.isUpperCase('s');<br/> System.out.println("bool3:" + bool3 + ", bool4:" + bool4);<br/> char c4 = Character.toUpperCase('s');<br/> Character c5 = Character.toLowerCase(ch1);<br/> System.out.println("c4:" + c4 + ", c5:" + c5);<br/> } <br/>}<br/>/*运行结果:<br/>ch1:A, ch2:A, ch3:C<br/>ch1.compareTo(ch2):0, ch1.compareTo(ch3):-2<br/>ch1.equals(ch2): true, ch1.equals(ch3): false<br/>bool3:true, bool4:false<br/>c4:S, c5:a<br/>*/<br/></span>
【相关推荐】
1. Java免费视频教程
以上是关于包装类Character的实例解析的详细内容。更多信息请关注PHP中文网其他相关文章!