关于包装类Character的实例解析
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:
package Number;
public class IntFunction{
public static void main (String []args)
{
Character ch1 = Character.valueOf('A');
Character ch2 = new Character('A');
Character ch3 = Character.valueOf('C');
char c1 = ch1.charValue();
char c2 = ch2.charValue();
char c3 = ch3.charValue();
System.out.println("ch1:" + c1 + ", ch2:" + c2 + ", ch3:" + c3);
int a1 = ch1.compareTo(ch2);
int a2 = ch1.compareTo(ch3);
System.out.println("ch1.compareTo(ch2):" + a1 + ", ch1.compareTo(ch3):" + a2);
boolean bool1 = ch1.equals(ch2);
boolean bool2 = ch1.equals(ch3);
System.out.println("ch1.equals(ch2): " + bool1 + ", ch1.equals(ch3): " + bool2);
boolean bool3 = Character.isUpperCase(ch1);
boolean bool4 = Character.isUpperCase('s');
System.out.println("bool3:" + bool3 + ", bool4:" + bool4);
char c4 = Character.toUpperCase('s');
Character c5 = Character.toLowerCase(ch1);
System.out.println("c4:" + c4 + ", c5:" + c5);
}
}
/*运行结果:
ch1:A, ch2:A, ch3:C
ch1.compareTo(ch2):0, ch1.compareTo(ch3):-2
ch1.equals(ch2): true, ch1.equals(ch3): false
bool3:true, bool4:false
c4:S, c5:a
*/
【相关推荐】
1. Java免费视频教程
以上就是关于包装类Character的实例解析的详细内容,更多请关注其它相关文章!