char,Character,int,字符及编码日记
public class Test {
public static void main(String[] args) {
char c = 'a';
Character ch = new Character(c);
int code = c;
System.out.print(c + " ");//打印出字符
System.out.print(ch + " ");//打印出字符
System.out.print(ch.charValue() + " ");//打印出字符
System.out.println(code);//打印出编码
}
}
这个代码在IDE中编译运行没有问题,如果在cmd下,会出错:编码GBK的不可映射字符。这个时候在编译时需要加上-encoding utf-8参数。
如果字符+1,可以变成下一个字符,编码和字符显示都是正确的,代码如下:
public class Test {
public static void main(String[] args) {
char a = 'a';
int code = a;//不需要强制转换
char c = (char)(code + 1);//需要强制转换
Character ch = new Character(c);
System.out.print(c + " ");//打印出字符
System.out.print(ch + " ");//打印出字符
System.out.print(ch.charValue() + " ");//打印出字符
System.out.println(code);//打印出编码
}
}
如果想吧字符‘0’~‘9’加密位新的数字,每位字符+1,比如‘8’变‘9’,‘9’变‘0’,代码如下:
public class Test {
public static void main(String[] args) {
char a = '0';
int code = a;
int encode = (code + 1 - 48) % 10 + 48;
char c = (char)encode;
System.out.print(c + " ");//打印出字符
System.out.println(encode);//打印出编码
}
}