參考http://how2j.cn/k/number-string/number-string-character/323.htmlhtml
package character; public class TestChar { public static void main(String[] args) { char c1 = 'a'; char c2 = '1';//字符1,而非數字1 char c3 = '中';//漢字字符 //char c4 = 'ab'; //錯誤,只能放一個字符 } }
char對應的封裝類是Charactergit
package character; public class TestChar { public static void main(String[] args) { char c1 = 'a'; Character c = c1; //自動裝箱 c1 = c;//自動拆箱 } }
package character; public class TestChar { public static void main(String[] args) { System.out.println(Character.isLetter('a'));//判斷是否爲字母 System.out.println(Character.isDigit('a')); //判斷是否爲數字 System.out.println(Character.isWhitespace(' ')); //是不是空白 System.out.println(Character.isUpperCase('a')); //是不是大寫 System.out.println(Character.isLowerCase('a')); //是不是小寫 System.out.println(Character.toUpperCase('a')); //轉換爲大寫 System.out.println(Character.toLowerCase('A')); //轉換爲小寫
//String a = 'a'; //不可以直接把一個字符轉換成字符串
String a2 = Character.toString('a'); //轉換爲字符串
System.out.println("a2="+a2);
char c='a';
String a3=Character.valueOf(c).toString();
System.out.println("a3="+a3);
} }
執行結果spa
package character; public class TestChar { public static void main(String[] args) { System.out.println("使用空格沒法達到對齊的效果"); System.out.println("abc def"); System.out.println("ab def"); System.out.println("a def"); System.out.println("使用\\t製表符能夠達到對齊的效果"); System.out.println("abc\tdef"); System.out.println("ab\tdef"); System.out.println("a\tdef"); System.out.println("一個\\t製表符長度是8"); System.out.println("12345678def"); System.out.println("換行符 \\n"); System.out.println("abc\ndef"); System.out.println("單引號 \\'"); System.out.println("abc\'def"); System.out.println("雙引號 \\\""); System.out.println("abc\"def"); System.out.println("反斜槓自己 \\"); System.out.println("abc\\def"); } }
執行結果code