在java中,int和char强制转化。其中int对应的是ASCII码,char是对应的字符。
示例如下。
private static void charDemo(){
//这里打印char的ASCII码
char a = 'a';
System.out.println("a:" + (int)a);
char z = 'z';
System.out.println("z:" + (int)z);
char A = 'A';
System.out.println("A:" + (int)A);
char Z = 'Z';
System.out.println("Z:" + (int)Z);
//汉字同样转为ASCII码
char me = '我';
System.out.println("我:" + (int)me);
char jie = '戒';
System.out.println("戒:" + (int)jie);
//int型同样可以转为char型
int me_upper = (int)me + 1;
System.out.println("我+1:" + (char)me_upper);
//数字
char two = '2';
System.out.println("2:" + (int)two);
char three = '3';
System.out.println("3:" + (int)three);
//==运算符,默认转型为int型
if (a == A + 32) {
System.out.println("a==A+32");
}
String d = "asdwodfsAADAD";
//转为小写的方法,构造Locale
Locale locale = Locale.getDefault();
System.out.println("d.toLowerCase:" + d.toLowerCase(locale));
}
输出结果如下: